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

[red-knot] Use a distinct type for module search paths in the module resolver #12379

Merged
merged 2 commits into from
Jul 22, 2024

Conversation

AlexWaygood
Copy link
Member

Summary

This PR cleans up some of the settings validation and types in the module resolver. Namely, at various points in the crate, we use a type alias, a ModuleResolutionPathBuf or an Arc<ModuleResolutionPathBuf> to refer to a module search path. This PR goes some way to cleaning that up by introducing a newtype wrapper in path.rs for module-resolution paths that specifically represent search paths.

The main motivation for this change is because module search paths and other module-resolution paths have different invariants, so should be validated differently. This PR has been split off from #12376, which bundled the validation changes in as the same PR, to ease review.

Copying from the PR description of #12376:

This PR goes halfway to addressing @carljm's review comments in #12141 (comment). We now have a distinct type for representing search paths specifically outside of path.rs. However, internally, path.rs still doesn't make much distinction between search paths and other kinds of paths: it doesn't have to, because I implemented Deref for the new ModuleSearchPath struct; all methods available on ModuleResolutionPathBuf are implicitly available on ModuleSearchPath. This is somewhat lazy and unprincipled of me, and I've left a TODO comment to get rid of it. I spent much of yesterday trying to get to a more principled solution, but it ends up being a massive diff that rewrites much of path.rs, and I don't think it's a priority for now. I think this is a nice cleanup that clarifies the types and improves settings validation in the meantime, without being a massive rewrite that would be time-consuming to write and review.

Test Plan

cargo test -p red_knot_module_resolver

@AlexWaygood AlexWaygood added the red-knot Multi-file analysis & type inference label Jul 18, 2024
Copy link
Contributor

github-actions bot commented Jul 18, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I let @carljm approve this change. I like that some Arc<ModuleSearchPath> are gone. I'm otherwise not quiet sure what the benefit is.

Nit: We can reduce the visibility of ModuleSearchPathBuf and Ref to pub(super).

Comment on lines 732 to 752
pub(crate) fn extra(path: SystemPathBuf) -> Option<Self> {
Some(Self(Arc::new(ModuleResolutionPathBuf::extra(path)?)))
}

pub(crate) fn first_party(path: SystemPathBuf) -> Option<Self> {
Some(Self(Arc::new(ModuleResolutionPathBuf::first_party(path)?)))
}

pub(crate) fn custom_stdlib(path: &SystemPath) -> Option<Self> {
Some(Self(Arc::new(ModuleResolutionPathBuf::standard_library(
FilePath::System(path.join("stdlib")),
)?)))
}

pub(crate) fn vendored_stdlib() -> Self {
Self(Arc::new(ModuleResolutionPathBuf(
ModuleResolutionPathBufInner::StandardLibrary(FilePath::vendored("stdlib")),
)))
}

pub(crate) fn site_packages(path: SystemPathBuf) -> Option<Self> {
Some(Self(Arc::new(ModuleResolutionPathBuf::site_packages(
path,
)?)))
}

pub(crate) fn editable(system: &dyn System, path: SystemPathBuf) -> Option<Self> {
Some(Self(Arc::new(
ModuleResolutionPathBuf::editable_installation_root(system, path)?,
)))
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we should inline the

#[must_use]
pub(crate) fn extra(path: impl Into<SystemPathBuf>) -> Option<Self> {
let path = path.into();
path.extension()
.map_or(true, |ext| matches!(ext, "py" | "pyi"))
.then_some(Self(ModuleResolutionPathBufInner::Extra(path)))
}
#[must_use]
pub(crate) fn first_party(path: impl Into<SystemPathBuf>) -> Option<Self> {
let path = path.into();
path.extension()
.map_or(true, |ext| matches!(ext, "pyi" | "py"))
.then_some(Self(ModuleResolutionPathBufInner::FirstParty(path)))
}
#[must_use]
pub(crate) fn standard_library(path: FilePath) -> Option<Self> {
path.extension()
.map_or(true, |ext| ext == "pyi")
.then_some(Self(ModuleResolutionPathBufInner::StandardLibrary(path)))
}
#[must_use]
pub(crate) fn site_packages(path: impl Into<SystemPathBuf>) -> Option<Self> {
let path = path.into();
path.extension()
.map_or(true, |ext| matches!(ext, "pyi" | "py"))
.then_some(Self(ModuleResolutionPathBufInner::SitePackages(path)))
}
#[must_use]
pub(crate) fn editable_installation_root(
system: &dyn System,
path: impl Into<SystemPathBuf>,
) -> Option<Self> {
let path = path.into();
// TODO: Add Salsa invalidation to this system call:
system
.is_directory(&path)
.then_some(Self(ModuleResolutionPathBufInner::EditableInstall(path)))
}

and remove the definitions from ModuleResolutionPathBuf. The methods on ModuleResolutionPathBuf are only used in tests and I suspect that the tests should probably use ModuleResolutionPath instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

This makes sense but I'm going to postpone it for now. The idea of this PR was that it would be a small, incremental change that would be easy to review. Addressing this would require lots of changes to tests, and I think this PR is an improvement over the status quo as it stands.

crates/red_knot_module_resolver/src/path.rs Outdated Show resolved Hide resolved
@AlexWaygood
Copy link
Member Author

I'm otherwise not quiet sure what the benefit is.

Yeah, it's harder to see the benefit when the validation changes are decoupled from the change to use a new type. This is partly why I originally put them together in one PR, in #12376.

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

This looks like a good start that brings a bit of extra naming clarity, but doesn't do the real refactor yet, just as advertised.

I agree with both of Micha's comments.

I'm fine landing this and holding off on any more refactor for now, until/unless there are actual problems caused by the current structure.

crates/red_knot_module_resolver/src/path.rs Outdated Show resolved Hide resolved
@AlexWaygood AlexWaygood force-pushed the search-path-consistent-type-2 branch from dc8ae36 to 245606a Compare July 22, 2024 19:40
@AlexWaygood AlexWaygood changed the title [red-knot] Use a distinct type for module search paths in the module resolver (v2) [red-knot] Use a distinct type for module search paths in the module resolver Jul 22, 2024
@AlexWaygood AlexWaygood enabled auto-merge (squash) July 22, 2024 19:40
@AlexWaygood AlexWaygood merged commit 2a8f95c into main Jul 22, 2024
19 checks passed
@AlexWaygood AlexWaygood deleted the search-path-consistent-type-2 branch July 22, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants