about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Macleod <alex@macleod.io>2021-10-14 15:40:57 +0100
committerAlex Macleod <alex@macleod.io>2021-10-14 16:20:11 +0100
commita6eeb75120876e968f7d5adabbc7fcd6cc000c98 (patch)
treedc5b16a49b99278f4891b5029ccedd95ca9aceb8
parenta30941e2a553156ef891fa38d6241535eac3ce6f (diff)
downloadrust-a6eeb75120876e968f7d5adabbc7fcd6cc000c98.tar.gz
rust-a6eeb75120876e968f7d5adabbc7fcd6cc000c98.zip
Use workspace cargo to fetch rust source's metadata
Previously the detected cargo is the global one, as it uses the
directory of the rust source which doesn't pick up the local override

This fixes the case in clippy where the local rust toolchain is a recent
nightly that has a 2021 edition Cargo.toml. The global (stable) cargo
returns an error attempting to parse it

Fixes #10445
-rw-r--r--crates/project_model/src/cargo_workspace.rs3
-rw-r--r--crates/project_model/src/workspace.rs34
2 files changed, 24 insertions, 13 deletions
diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs
index 739f29b014d..13ff20c8c67 100644
--- a/crates/project_model/src/cargo_workspace.rs
+++ b/crates/project_model/src/cargo_workspace.rs
@@ -251,6 +251,7 @@ struct PackageMetadata {
 impl CargoWorkspace {
     pub fn fetch_metadata(
         cargo_toml: &ManifestPath,
+        current_dir: &AbsPath,
         config: &CargoConfig,
         progress: &dyn Fn(String),
     ) -> Result<cargo_metadata::Metadata> {
@@ -275,7 +276,7 @@ impl CargoWorkspace {
                 meta.features(CargoOpt::SomeFeatures(config.features.clone()));
             }
         }
-        meta.current_dir(cargo_toml.parent().as_os_str());
+        meta.current_dir(current_dir.as_os_str());
 
         if let Some(target) = target {
             meta.other_options(vec![String::from("--filter-platform"), target]);
diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs
index cb79ce08bad..339fdfcc9be 100644
--- a/crates/project_model/src/workspace.rs
+++ b/crates/project_model/src/workspace.rs
@@ -160,14 +160,19 @@ impl ProjectWorkspace {
                     cmd
                 })?;
 
-                let meta = CargoWorkspace::fetch_metadata(&cargo_toml, config, progress)
-                    .with_context(|| {
-                        format!(
-                            "Failed to read Cargo metadata from Cargo.toml file {}, {}",
-                            cargo_toml.display(),
-                            cargo_version
-                        )
-                    })?;
+                let meta = CargoWorkspace::fetch_metadata(
+                    &cargo_toml,
+                    cargo_toml.parent(),
+                    config,
+                    progress,
+                )
+                .with_context(|| {
+                    format!(
+                        "Failed to read Cargo metadata from Cargo.toml file {}, {}",
+                        cargo_toml.display(),
+                        cargo_version
+                    )
+                })?;
                 let cargo = CargoWorkspace::new(meta);
 
                 let sysroot = if config.no_sysroot {
@@ -189,10 +194,15 @@ impl ProjectWorkspace {
 
                 let rustc = match rustc_dir {
                     Some(rustc_dir) => Some({
-                        let meta = CargoWorkspace::fetch_metadata(&rustc_dir, config, progress)
-                            .with_context(|| {
-                                "Failed to read Cargo metadata for Rust sources".to_string()
-                            })?;
+                        let meta = CargoWorkspace::fetch_metadata(
+                            &rustc_dir,
+                            cargo_toml.parent(),
+                            config,
+                            progress,
+                        )
+                        .with_context(|| {
+                            "Failed to read Cargo metadata for Rust sources".to_string()
+                        })?;
                         CargoWorkspace::new(meta)
                     }),
                     None => None,