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

avoid false positive PermissionError in declare_dependency #14342

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bonzini
Copy link
Collaborator

@bonzini bonzini commented Mar 7, 2025

If a directory that is passed to declare_dependency is not accessible (a parent has the x permission cleared), this can result in an OSError:

ERROR: Unhandled python OSError. This is probably not a Meson bug, but an issue with your build environment.

This can cause false positives, for example, if the directory is under /var and root-owned. Do the is_dir() test last, once it's known that the directory is related to the source directory, to avoid the false positives.

Fixes: #13584

@bonzini bonzini added the bug label Mar 7, 2025
@bonzini bonzini marked this pull request as ready for review March 7, 2025 12:06
@bonzini bonzini requested a review from jpakkane as a code owner March 7, 2025 12:06
@bruchar1 bruchar1 added this to the 1.7.1 milestone Mar 7, 2025
Do the is_dir() test last, once it's known that the directory is
related to the source directory.

Signed-off-by: Paolo Bonzini <[email protected]>
Comment on lines +725 to +732
# Note that p.is_dir() can raise a PermissionError if a parent does not have
# the executable permission set. Test it last, and only after checking that
# v is within the project's source directory, to avoid false positives for
# e.g. root owned directories under /var.
if p.is_absolute() \
and (self.is_subproject() or srcdir / self.subproject_dir not in p.parents) \
and srcdir / self.root_subdir in [p] + list(Path(os.path.abspath(p)).parents) \
and p.is_dir():
Copy link
Member

Choose a reason for hiding this comment

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

Generally speaking, the issue here is that pathlib "concrete path" ops are bad and should not be used, use os.path.*() functions instead (and just pass pathlib "rich path" objects to it).

Basically, try to use pathlib.PurePath whenever possible and shun any function calls that are only available in Path and missing from PurePath.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, so to understand you're asking me to get rid of Path completely?

Copy link
Member

Choose a reason for hiding this comment

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

It would probably be easier to do something meaningful by merging #14110 first and building on top of it...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's also a possibility, but (for what it's worth) I'm not sure that the OSError is an undesirable behavior—having an inaccessible directory inside the source tree is quite a bad idea after all, and Meson is able to report the error as a likely problematic build environment. And there are reasons in favor of both returning false like os.path.isdir() and letting the exception surface like Path.is_dir() (not the least: the latter can always be turned into the former).

So I still prefer to start with this patch to minimize the impact outside fixing the bug.

Copy link
Member

Choose a reason for hiding this comment

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

How does one end up with an inaccessible directory inside the source tree? And should we similarly add special handling if subdir/meson.build is inaccessible/unreadable? (By which I mean to say, any code at all that attempts to care about the distinction, rather than treating it as "the user broke it and now gets to keep both pieces".)

Keep in mind that the "error case" here is,

someone attempts to export and then use a declare_dependency() directory variable, and the accessing code fails to be special cased but instead treats it as a setup-time error to try to utilize this (unreadable!) directory owned by a subproject.

Again, I think it's overall a systematic mistake to use pathlib.Path for e.g. checking filesystem availability because the stdlib is buggy and badly designed.

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

Successfully merging this pull request may close these issues.

declare_dependency variable's value cannot be an unstatable file
3 participants