Skip to content

Commit

Permalink
[red-knot] Make the site-packages search-path setting a vector of p…
Browse files Browse the repository at this point in the history
…aths
  • Loading branch information
AlexWaygood committed Aug 2, 2024
1 parent 9aa43d5 commit 0666e67
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion crates/red_knot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn main() -> anyhow::Result<()> {
extra_paths,
workspace_root: workspace_metadata.root().to_path_buf(),
custom_typeshed: custom_typeshed_dir,
site_packages: None,
site_packages: vec![],
},
};

Expand Down
12 changes: 6 additions & 6 deletions crates/red_knot/tests/file_watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ where
extra_paths: vec![],
workspace_root: workspace_path.to_path_buf(),
custom_typeshed: None,
site_packages: None,
site_packages: vec![],
})
}

Expand Down Expand Up @@ -697,7 +697,7 @@ fn search_path() -> anyhow::Result<()> {
extra_paths: vec![],
workspace_root: workspace_path.to_path_buf(),
custom_typeshed: None,
site_packages: Some(root_path.join("site_packages")),
site_packages: vec![root_path.join("site_packages")],
}
})?;

Expand Down Expand Up @@ -734,7 +734,7 @@ fn add_search_path() -> anyhow::Result<()> {

// Register site-packages as a search path.
case.update_search_path_settings(|settings| SearchPathSettings {
site_packages: Some(site_packages.clone()),
site_packages: vec![site_packages.clone()],
..settings.clone()
});

Expand All @@ -757,14 +757,14 @@ fn remove_search_path() -> anyhow::Result<()> {
extra_paths: vec![],
workspace_root: workspace_path.to_path_buf(),
custom_typeshed: None,
site_packages: Some(root_path.join("site_packages")),
site_packages: vec![root_path.join("site_packages")],
}
})?;

// Remove site packages from the search path settings.
let site_packages = case.root_path().join("site_packages");
case.update_search_path_settings(|settings| SearchPathSettings {
site_packages: None,
site_packages: vec![],
..settings.clone()
});

Expand Down Expand Up @@ -1175,7 +1175,7 @@ mod unix {
extra_paths: vec![],
workspace_root: workspace.to_path_buf(),
custom_typeshed: None,
site_packages: Some(workspace.join(".venv/lib/python3.12/site-packages")),
site_packages: vec![workspace.join(".venv/lib/python3.12/site-packages")],
},
)?;

Expand Down
17 changes: 12 additions & 5 deletions crates/red_knot_module_resolver/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,18 @@ fn try_resolve_module_resolution_settings(
SearchPath::vendored_stdlib()
});

if let Some(site_packages) = site_packages {
files.try_add_root(db.upcast(), site_packages, FileRootKind::LibrarySearchPath);
for site_packages_dir in site_packages {
files.try_add_root(
db.upcast(),
site_packages_dir,
FileRootKind::LibrarySearchPath,
);

static_search_paths.push(SearchPath::site_packages(system, site_packages.clone())?);
};
static_search_paths.push(SearchPath::site_packages(
system,
site_packages_dir.clone(),
)?);
}

// TODO vendor typeshed's third-party stubs as well as the stdlib and fallback to them as a final step

Expand Down Expand Up @@ -1180,7 +1187,7 @@ mod tests {
extra_paths: vec![],
workspace_root: src.clone(),
custom_typeshed: Some(custom_typeshed.clone()),
site_packages: Some(site_packages.clone()),
site_packages: vec![site_packages],
};

Program::new(&db, TargetVersion::Py38, search_paths);
Expand Down
7 changes: 5 additions & 2 deletions crates/red_knot_module_resolver/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub(crate) struct TestCase<T> {
pub(crate) db: TestDb,
pub(crate) src: SystemPathBuf,
pub(crate) stdlib: T,
// Most test cases only ever need a single `site-packages` directory,
// so this is a single directory instead of a `Vec` of directories,
// like it is in `ruff_db::Program`.
pub(crate) site_packages: SystemPathBuf,
pub(crate) target_version: TargetVersion,
}
Expand Down Expand Up @@ -223,7 +226,7 @@ impl TestCaseBuilder<MockedTypeshed> {
extra_paths: vec![],
workspace_root: src.clone(),
custom_typeshed: Some(typeshed.clone()),
site_packages: Some(site_packages.clone()),
site_packages: vec![site_packages.clone()],
},
);

Expand Down Expand Up @@ -276,7 +279,7 @@ impl TestCaseBuilder<VendoredTypeshed> {
extra_paths: vec![],
workspace_root: src.clone(),
custom_typeshed: None,
site_packages: Some(site_packages.clone()),
site_packages: vec![site_packages.clone()],
},
);

Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/semantic_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mod tests {
SearchPathSettings {
extra_paths: vec![],
workspace_root: SystemPathBuf::from("/src"),
site_packages: None,
site_packages: vec![],
custom_typeshed: None,
},
);
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ mod tests {
SearchPathSettings {
extra_paths: Vec::new(),
workspace_root: SystemPathBuf::from("/src"),
site_packages: None,
site_packages: vec![],
custom_typeshed: None,
},
);
Expand All @@ -1532,7 +1532,7 @@ mod tests {
SearchPathSettings {
extra_paths: Vec::new(),
workspace_root: SystemPathBuf::from("/src"),
site_packages: None,
site_packages: vec![],
custom_typeshed: Some(SystemPathBuf::from(typeshed)),
},
);
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_workspace/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ mod tests {
SearchPathSettings {
extra_paths: Vec::new(),
workspace_root,
site_packages: None,
site_packages: vec![],
custom_typeshed: None,
},
);
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_workspace/tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn setup_db(workspace_root: SystemPathBuf) -> anyhow::Result<RootDatabase> {
extra_paths: vec![],
workspace_root,
custom_typeshed: None,
site_packages: None,
site_packages: vec![],
};
let settings = ProgramSettings {
target_version: TargetVersion::default(),
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_benchmark/benches/red_knot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn setup_case() -> Case {
search_paths: SearchPathSettings {
extra_paths: vec![],
workspace_root: workspace_root.to_path_buf(),
site_packages: None,
site_packages: vec![],
custom_typeshed: None,
},
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_db/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ pub struct SearchPathSettings {
pub custom_typeshed: Option<SystemPathBuf>,

/// The path to the user's `site-packages` directory, where third-party packages from ``PyPI`` are installed.
pub site_packages: Option<SystemPathBuf>,
pub site_packages: Vec<SystemPathBuf>,
}

0 comments on commit 0666e67

Please sign in to comment.