Skip to content

Commit ab9e7bd

Browse files
authored
Merge pull request #20069 from Veykril/push-mnqkqxomtlxn
fix: Fix cargo project manifest not pointing to the workspace root
2 parents f9e371c + 44f2cf9 commit ab9e7bd

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

crates/project-model/src/workspace.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
1616
use rustc_hash::{FxHashMap, FxHashSet};
1717
use semver::Version;
1818
use span::{Edition, FileId};
19+
use toolchain::Tool;
1920
use tracing::instrument;
2021
use triomphe::Arc;
2122

@@ -29,6 +30,7 @@ use crate::{
2930
project_json::{Crate, CrateArrayIdx},
3031
sysroot::RustLibSrcWorkspace,
3132
toolchain_info::{QueryConfig, rustc_cfg, target_data_layout, target_tuple, version},
33+
utf8_stdout,
3234
};
3335
use tracing::{debug, error, info};
3436

@@ -209,7 +211,7 @@ impl ProjectWorkspace {
209211
progress: &(dyn Fn(String) + Sync),
210212
) -> Result<ProjectWorkspace, anyhow::Error> {
211213
progress("Discovering sysroot".to_owned());
212-
let workspace_dir = cargo_toml.parent();
214+
213215
let CargoConfig {
214216
features,
215217
rustc_source,
@@ -224,6 +226,7 @@ impl ProjectWorkspace {
224226
no_deps,
225227
..
226228
} = config;
229+
let workspace_dir = cargo_toml.parent();
227230
let mut sysroot = match (sysroot, sysroot_src) {
228231
(Some(RustLibSource::Discover), None) => Sysroot::discover(workspace_dir, extra_env),
229232
(Some(RustLibSource::Discover), Some(sysroot_src)) => {
@@ -238,6 +241,31 @@ impl ProjectWorkspace {
238241
(None, _) => Sysroot::empty(),
239242
};
240243

244+
// Resolve the Cargo.toml to the workspace root as we base the `target` dir off of it.
245+
let mut cmd = sysroot.tool(Tool::Cargo, workspace_dir, extra_env);
246+
cmd.args(["locate-project", "--workspace", "--manifest-path", cargo_toml.as_str()]);
247+
let cargo_toml = &match utf8_stdout(&mut cmd) {
248+
Ok(output) => {
249+
#[derive(serde_derive::Deserialize)]
250+
struct Root {
251+
root: Utf8PathBuf,
252+
}
253+
match serde_json::from_str::<Root>(&output) {
254+
Ok(object) => ManifestPath::try_from(AbsPathBuf::assert(object.root))
255+
.expect("manifest path should be absolute"),
256+
Err(e) => {
257+
tracing::error!(%e, %cargo_toml, "failed fetching cargo workspace root");
258+
cargo_toml.clone()
259+
}
260+
}
261+
}
262+
Err(e) => {
263+
tracing::error!(%e, %cargo_toml, "failed fetching cargo workspace root");
264+
cargo_toml.clone()
265+
}
266+
};
267+
let workspace_dir = cargo_toml.parent();
268+
241269
tracing::info!(workspace = %cargo_toml, src_root = ?sysroot.rust_lib_src_root(), root = ?sysroot.root(), "Using sysroot");
242270
progress("Querying project metadata".to_owned());
243271
let toolchain_config = QueryConfig::Cargo(&sysroot, cargo_toml);

0 commit comments

Comments
 (0)