@@ -16,6 +16,7 @@ use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
1616use rustc_hash:: { FxHashMap , FxHashSet } ;
1717use semver:: Version ;
1818use span:: { Edition , FileId } ;
19+ use toolchain:: Tool ;
1920use tracing:: instrument;
2021use 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} ;
3335use 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