fix(libraries): don't list in-project Cargo path dependencies as External Libraries (#7316)

cargo metadata in a sub-crate's own workspace reported the project root (a path dependency such as vpn-core = { path = ".." }) as an external package, so the whole project became a read-only library root. Cargo library roots now drop path dependencies inside the open project, reusing is_project_source as Go modules do, so vendor copies stay libraries.

Co-authored-by: stalexxx <[email protected]>
Цей коміт міститься в:
Alexanderтаstalexxx authored and GitHub committed 2026-12-02 11:46:56 +08:00
1 джерело f4236a0ea1
коміт f87fb35be5
2 files changed
+35 -4

No files matched your search

+19 -4
Переглянути файл
@@ -147,7 +147,7 @@ pub fn project_libraries_stream(
// `exclude`) without its own keeps none, as RustRover leaves crates it doesn't attach.
let locked = workspace.canonicalize().unwrap_or_else(|_| workspace.clone()) != project
&& !workspace.join("Cargo.lock").is_file();
let batch = rust_groups(&workspace, &settings, locked, cancel);
let batch = rust_groups(&workspace, &project, &settings, locked, cancel);
if !cancel.is_cancelled() && !batch.is_empty() {
on_groups(batch.clone());
groups.extend(batch);
@@ -867,7 +867,13 @@ pub(crate) fn package_dependencies(package_json: &str) -> Vec<String> {
// ------------------------------------------------------------------ rust
/// `locked`: `cargo metadata --locked`, which fails instead of writing a Cargo.lock.
fn rust_groups(root: &Path, s: &Settings, locked: bool, cancel: &CancellationToken) -> Vec<LibraryGroup> {
fn rust_groups(
root: &Path,
project: &Path,
s: &Settings,
locked: bool,
cancel: &CancellationToken,
) -> Vec<LibraryGroup> {
let Some(files) = manifests(root, CARGO_MANIFESTS) else { return Vec::new() };
if !root.join("Cargo.toml").is_file() {
return Vec::new();
@@ -897,7 +903,7 @@ fn rust_groups(root: &Path, s: &Settings, locked: bool, cancel: &CancellationTok
args.push("--locked");
}
let json = query_in_cancellable(&cargo, &args, root, &[], GRAPH_TIMEOUT, cancel)?;
let roots = parse_cargo_metadata(&json)?;
let roots = outside_project(parse_cargo_metadata(&json)?, project);
Some(group("Cargo dependencies", "cargo", named_roots(roots)).into_iter().collect())
});
// An edited manifest that names crates not downloaded yet fails offline until the next
@@ -950,6 +956,13 @@ fn cargo_lock_roots(root: &Path, home: &Path) -> Vec<(String, PathBuf)> {
.collect()
}
/// Drops the path dependencies that lie inside the open project: a sibling crate of a separate
/// workspace (`vpn-core = { path = ".." }`) is the project's own code, not an external library.
/// A `vendor` copy stays a library, as for Go modules.
pub(crate) fn outside_project(roots: Vec<(String, PathBuf)>, project: &Path) -> Vec<(String, PathBuf)> {
roots.into_iter().filter(|(_, dir)| !is_project_source(project, dir)).collect()
}
/// `(name version, source dir)` of the non-workspace packages in `cargo metadata` output, by name.
pub(crate) fn parse_cargo_metadata(json: &str) -> Option<Vec<(String, PathBuf)>> {
#[derive(Deserialize)]
@@ -1331,7 +1344,9 @@ mod go_cache_tests {
let mut settings = Settings::default();
settings.languages.rust.toolchain = "tools".into();
assert!(project_libraries(root, &settings, &[]).is_empty());
let source = root.join("local");
// Outside the project: a path dependency inside it is the project's own code.
let external = tempfile::tempdir().unwrap();
let source = external.path().join("local");
std::fs::create_dir(&source).unwrap();
let json = serde_json::json!({"packages": [{"id":"dep", "name":"dep", "version":"1.0.0", "source":null,
"manifest_path":source.join("Cargo.toml")}], "workspace_members":[]});
+16
Переглянути файл
@@ -2475,6 +2475,22 @@ fn cargo_metadata_lists_registry_and_non_workspace_path_dependencies() {
assert!(crate::libraries::parse_cargo_metadata("error: no Cargo.toml").is_none());
}
#[test]
fn cargo_path_dependencies_inside_the_project_are_not_libraries() {
let tmp = tempfile::tempdir().unwrap();
let project = tmp.path().canonicalize().unwrap();
let roots = vec![
("vpn-core 0.3.37".to_owned(), project.clone()),
("sibling 0.1.0".to_owned(), project.join("crates/sibling")),
("serde 1.0.200".to_owned(), project.parent().unwrap().join("registry/serde-1.0.200")),
("vendored 1.0.0".to_owned(), project.join("vendor/vendored")),
("other 0.1.0".to_owned(), std::path::PathBuf::from(format!("{}-other", project.display()))),
];
let kept = crate::libraries::outside_project(roots, &project);
let names: Vec<_> = kept.iter().map(|(n, _)| n.as_str()).collect();
assert_eq!(names, ["serde 1.0.200", "vendored 1.0.0", "other 0.1.0"]);
}
#[test]
fn go_modules_skip_the_main_module_and_missing_downloads() {
let out = r#"{"Path":"example.com/app","Main":true,"Dir":"/p"}