Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support input files containing spaces #442

Merged
merged 7 commits into from
Nov 21, 2022
Merged

support input files containing spaces #442

merged 7 commits into from
Nov 21, 2022

Conversation

anthrotype
Copy link
Member

Part of fixing #435

@anthrotype anthrotype marked this pull request as draft November 18, 2022 13:18
@anthrotype anthrotype force-pushed the path-with-spaces branch 2 times, most recently from 6ffca47 to 20afd2d Compare November 18, 2022 15:16
@anthrotype anthrotype marked this pull request as ready for review November 18, 2022 15:21
@anthrotype
Copy link
Member Author

anthrotype commented Nov 18, 2022

The problem here was that ninja user-defined build variables (e.g. --target_font $target_font in glue_together rule) don't get shell-quoted automatically when they are expanded, only $in and $out are (hence the latter at least are fine when inputs/outputs contain spaces).
If the other variables happen to refer to paths with spaces or other special characters, ninja passes them through unchanged and the underlying shell will split the command line arguments incorrectly.
So we need to enquote them following the shell's own rules, which differ between POSIX and Windows, as usual.
Unfortunately the stdlib's shlex module's quote() and split() commands don't work well with Windows, so I had to resort to a third-party module called https://github.com/smoofra/mslex to accomplish this (it's only installed if sys.platform=="win32").
Now, subprocess also has a similar method called list2cmdline which follows Windows MS C runtime rules, but.. it's private and undocumented, and I also need the reverse of that in order to split a str into shell-compatible list of arguments (we need to split inside nanoemoji.util.expand_ninja_response_files to emulate the @-prefixed so-called response files used by ninja to work around maximum-args limits).

Add conditional dependency on mslex, third party module that adheres to Windows shell quoting rules, unlike shlex which is POSIX only.

This lets us correctly support input files with spaces or other special characters on Windows in a way that is compatible with ninja.
the Config.output_file is used to define the filenames of temporary build files which are passed to ninja rule commands as variables, this makes sure things work even when output_file contains spaces
@rsheeter
Copy link
Collaborator

https://github.com/smoofra/mslex looks like a tiny dead one-man project. I don't think this is a good dependency. Since Python can do things like run windows processes I'd have thought it must have this built-in but if not perhaps we should just write our own little function?

@anthrotype
Copy link
Member Author

ok, I removed the dependency on mslex, and went back to using subprocess.list2cmdline for shell quoting on Windows. Whereas for splitting a command line string to list of args, I am now using ctypes to wrap a call into Windows' own CommandLineToArgvW method (which works only on Windows of course, but that's ok). I copied the tests from cpyton's test_subprocess.py to confirm that both quoting and splitting back do roundtrip as intended on Windows.

@rsheeter
Copy link
Collaborator

I'm mildly amazed where this has taken us haha.

Copy link
Collaborator

@rsheeter rsheeter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants