@@ -16,6 +16,7 @@ use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
16
16
use rustc_hash:: { FxHashMap , FxHashSet } ;
17
17
use semver:: Version ;
18
18
use span:: { Edition , FileId } ;
19
+ use toolchain:: Tool ;
19
20
use tracing:: instrument;
20
21
use triomphe:: Arc ;
21
22
@@ -29,6 +30,7 @@ use crate::{
29
30
project_json:: { Crate , CrateArrayIdx } ,
30
31
sysroot:: RustLibSrcWorkspace ,
31
32
toolchain_info:: { QueryConfig , rustc_cfg, target_data_layout, target_tuple, version} ,
33
+ utf8_stdout,
32
34
} ;
33
35
use tracing:: { debug, error, info} ;
34
36
@@ -209,7 +211,7 @@ impl ProjectWorkspace {
209
211
progress : & ( dyn Fn ( String ) + Sync ) ,
210
212
) -> Result < ProjectWorkspace , anyhow:: Error > {
211
213
progress ( "Discovering sysroot" . to_owned ( ) ) ;
212
- let workspace_dir = cargo_toml . parent ( ) ;
214
+
213
215
let CargoConfig {
214
216
features,
215
217
rustc_source,
@@ -224,6 +226,7 @@ impl ProjectWorkspace {
224
226
no_deps,
225
227
..
226
228
} = config;
229
+ let workspace_dir = cargo_toml. parent ( ) ;
227
230
let mut sysroot = match ( sysroot, sysroot_src) {
228
231
( Some ( RustLibSource :: Discover ) , None ) => Sysroot :: discover ( workspace_dir, extra_env) ,
229
232
( Some ( RustLibSource :: Discover ) , Some ( sysroot_src) ) => {
@@ -238,6 +241,31 @@ impl ProjectWorkspace {
238
241
( None , _) => Sysroot :: empty ( ) ,
239
242
} ;
240
243
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
+
241
269
tracing:: info!( workspace = %cargo_toml, src_root = ?sysroot. rust_lib_src_root( ) , root = ?sysroot. root( ) , "Using sysroot" ) ;
242
270
progress ( "Querying project metadata" . to_owned ( ) ) ;
243
271
let toolchain_config = QueryConfig :: Cargo ( & sysroot, cargo_toml) ;
0 commit comments