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

Detect missing files with trailing space on Windows #14145

Merged

Conversation

bruchar1
Copy link
Member

@bruchar1 bruchar1 commented Jan 16, 2025

On Windows, if you accidently add a space at the end of a file name, like files('myfile.txt '), the file is not reported as missing, because of the normalization performed by the OS. However, ninja will reference it with the trailing space, and will fail because the file does not exist.

One solution is to prepend the file name with \\?\ to prevent the normalization of the name.

See python/cpython#115104 for reference.

@bruchar1 bruchar1 requested a review from jpakkane as a code owner January 16, 2025 16:09
@bruchar1 bruchar1 added the OS:windows Winodows OS specific issues label Jan 16, 2025
@bruchar1 bruchar1 force-pushed the detect-trailing-spaces-on-windows branch 5 times, most recently from 78dcdaf to fd201e8 Compare January 16, 2025 18:54
@jpakkane
Copy link
Member

Is there ever a case where we would need to support spaces at the end of paths? IOW could we just check for it and error out if a file name ends in a space (as it is almost certainly a bug)?

@bruchar1 bruchar1 force-pushed the detect-trailing-spaces-on-windows branch from fd201e8 to 20d974c Compare January 20, 2025 13:09
@bruchar1
Copy link
Member Author

Is there ever a case where we would need to support spaces at the end of paths? IOW could we just check for it and error out if a file name ends in a space (as it is almost certainly a bug)?

Good idea. All my other attempts failed. This is probably simpler and safer to just detect trailing spaces.

@bruchar1 bruchar1 force-pushed the detect-trailing-spaces-on-windows branch from 20d974c to 4d8677b Compare January 20, 2025 13:24
@@ -664,6 +664,9 @@ def func_import(self, node: mparser.BaseNode, args: T.Tuple[str],
@typed_pos_args('files', varargs=str)
@noKwargs
def func_files(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'TYPE_kwargs') -> T.List[mesonlib.File]:
for filename in args[0]:
if filename.endswith(' '):
raise MesonException(f'{filename!r} ends with a space. This is probably an error.', file=node.filename, lineno=node.lineno, colno=node.colno)
Copy link
Member

Choose a reason for hiding this comment

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

Should we mention why it's an error, e.g. that it breaks on Windows?

Are we worried about files passed as strings to a function that internally calls source_strings_to_files, but isn't the meson.build DSL function files() itself?

Copy link
Member Author

@bruchar1 bruchar1 Jan 20, 2025

Choose a reason for hiding this comment

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

I wasn't sure how many details should be put in the error message. Having a file name ending with a space will always be confusing, anyway.

Yes, that's probably a good idea to move the check to source_strings_to_files, and it prevents adding a loop, which is a good thing.

On Windows, if you accidently add a space at the end of a file name, like
`files('myfile.txt ')`, the file is not reported as missing, because of
the normalization performed by the OS. However, ninja will reference it
with the trailing space, and will fail because the file does not exist.

See python/cpython#115104 for reference.
@bruchar1 bruchar1 force-pushed the detect-trailing-spaces-on-windows branch from 4d8677b to 830d53c Compare January 20, 2025 13:52
@bruchar1 bruchar1 added this to the 1.7.1 milestone Jan 24, 2025
@jpakkane jpakkane merged commit 57c5d00 into mesonbuild:master Jan 28, 2025
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS:windows Winodows OS specific issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants