1
1
//! See [`CargoWorkspace`].
2
2
3
+ use std:: convert:: TryInto ;
3
4
use std:: iter;
4
5
use std:: path:: PathBuf ;
5
- use std:: { convert :: TryInto , ops, process:: Command } ;
6
+ use std:: { ops, process:: Command } ;
6
7
7
8
use anyhow:: { Context , Result } ;
8
9
use base_db:: Edition ;
@@ -13,8 +14,8 @@ use rustc_hash::FxHashMap;
13
14
use serde:: Deserialize ;
14
15
use serde_json:: from_value;
15
16
16
- use crate :: utf8_stdout;
17
17
use crate :: CfgOverrides ;
18
+ use crate :: { utf8_stdout, ManifestPath } ;
18
19
19
20
/// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
20
21
/// workspace. It pretty closely mirrors `cargo metadata` output.
@@ -108,7 +109,7 @@ pub struct PackageData {
108
109
/// Name as given in the `Cargo.toml`
109
110
pub name : String ,
110
111
/// Path containing the `Cargo.toml`
111
- pub manifest : AbsPathBuf ,
112
+ pub manifest : ManifestPath ,
112
113
/// Targets provided by the crate (lib, bin, example, test, ...)
113
114
pub targets : Vec < Target > ,
114
115
/// Is this package a member of the current workspace
@@ -215,12 +216,6 @@ impl TargetKind {
215
216
}
216
217
}
217
218
218
- impl PackageData {
219
- pub fn root ( & self ) -> & AbsPath {
220
- self . manifest . parent ( ) . unwrap ( )
221
- }
222
- }
223
-
224
219
#[ derive( Deserialize , Default ) ]
225
220
// Deserialise helper for the cargo metadata
226
221
struct PackageMetadata {
@@ -230,10 +225,16 @@ struct PackageMetadata {
230
225
231
226
impl CargoWorkspace {
232
227
pub fn fetch_metadata (
233
- cargo_toml : & AbsPath ,
228
+ cargo_toml : & ManifestPath ,
234
229
config : & CargoConfig ,
235
230
progress : & dyn Fn ( String ) ,
236
231
) -> Result < cargo_metadata:: Metadata > {
232
+ let target = config
233
+ . target
234
+ . clone ( )
235
+ . or_else ( || cargo_config_build_target ( cargo_toml) )
236
+ . or_else ( || rustc_discover_host_triple ( cargo_toml) ) ;
237
+
237
238
let mut meta = MetadataCommand :: new ( ) ;
238
239
meta. cargo_path ( toolchain:: cargo ( ) ) ;
239
240
meta. manifest_path ( cargo_toml. to_path_buf ( ) ) ;
@@ -249,16 +250,8 @@ impl CargoWorkspace {
249
250
meta. features ( CargoOpt :: SomeFeatures ( config. features . clone ( ) ) ) ;
250
251
}
251
252
}
252
- if let Some ( parent) = cargo_toml. parent ( ) {
253
- meta. current_dir ( parent. to_path_buf ( ) ) ;
254
- }
255
- let target = if let Some ( target) = & config. target {
256
- Some ( target. clone ( ) )
257
- } else if let stdout @ Some ( _) = cargo_config_build_target ( cargo_toml) {
258
- stdout
259
- } else {
260
- rustc_discover_host_triple ( cargo_toml)
261
- } ;
253
+ meta. current_dir ( cargo_toml. parent ( ) . as_os_str ( ) ) ;
254
+
262
255
if let Some ( target) = target {
263
256
meta. other_options ( vec ! [ String :: from( "--filter-platform" ) , target] ) ;
264
257
}
@@ -269,21 +262,7 @@ impl CargoWorkspace {
269
262
progress ( "metadata" . to_string ( ) ) ;
270
263
271
264
let meta = meta. exec ( ) . with_context ( || {
272
- let cwd: Option < AbsPathBuf > =
273
- std:: env:: current_dir ( ) . ok ( ) . and_then ( |p| p. try_into ( ) . ok ( ) ) ;
274
-
275
- let workdir = cargo_toml
276
- . parent ( )
277
- . map ( |p| p. to_path_buf ( ) )
278
- . or ( cwd)
279
- . map ( |dir| format ! ( " in `{}`" , dir. display( ) ) )
280
- . unwrap_or_default ( ) ;
281
-
282
- format ! (
283
- "Failed to run `cargo metadata --manifest-path {}`{}" ,
284
- cargo_toml. display( ) ,
285
- workdir
286
- )
265
+ format ! ( "Failed to run `cargo metadata --manifest-path {}`" , cargo_toml. display( ) , )
287
266
} ) ?;
288
267
289
268
Ok ( meta)
@@ -312,7 +291,7 @@ impl CargoWorkspace {
312
291
id : id. repr . clone ( ) ,
313
292
name : name. clone ( ) ,
314
293
version : version. clone ( ) ,
315
- manifest : AbsPathBuf :: assert ( PathBuf :: from ( & manifest_path) ) ,
294
+ manifest : AbsPathBuf :: assert ( PathBuf :: from ( & manifest_path) ) . try_into ( ) . unwrap ( ) ,
316
295
targets : Vec :: new ( ) ,
317
296
is_member,
318
297
edition,
@@ -376,7 +355,7 @@ impl CargoWorkspace {
376
355
}
377
356
378
357
pub fn from_cargo_metadata3 (
379
- cargo_toml : & AbsPath ,
358
+ cargo_toml : & ManifestPath ,
380
359
config : & CargoConfig ,
381
360
progress : & dyn Fn ( String ) ,
382
361
) -> Result < CargoWorkspace > {
@@ -412,9 +391,9 @@ impl CargoWorkspace {
412
391
}
413
392
}
414
393
415
- fn rustc_discover_host_triple ( cargo_toml : & AbsPath ) -> Option < String > {
394
+ fn rustc_discover_host_triple ( cargo_toml : & ManifestPath ) -> Option < String > {
416
395
let mut rustc = Command :: new ( toolchain:: rustc ( ) ) ;
417
- rustc. current_dir ( cargo_toml. parent ( ) . unwrap ( ) ) . arg ( "-vV" ) ;
396
+ rustc. current_dir ( cargo_toml. parent ( ) ) . arg ( "-vV" ) ;
418
397
log:: debug!( "Discovering host platform by {:?}" , rustc) ;
419
398
match utf8_stdout ( rustc) {
420
399
Ok ( stdout) => {
@@ -435,10 +414,10 @@ fn rustc_discover_host_triple(cargo_toml: &AbsPath) -> Option<String> {
435
414
}
436
415
}
437
416
438
- fn cargo_config_build_target ( cargo_toml : & AbsPath ) -> Option < String > {
417
+ fn cargo_config_build_target ( cargo_toml : & ManifestPath ) -> Option < String > {
439
418
let mut cargo_config = Command :: new ( toolchain:: cargo ( ) ) ;
440
419
cargo_config
441
- . current_dir ( cargo_toml. parent ( ) . unwrap ( ) )
420
+ . current_dir ( cargo_toml. parent ( ) )
442
421
. args ( & [ "-Z" , "unstable-options" , "config" , "get" , "build.target" ] )
443
422
. env ( "RUSTC_BOOTSTRAP" , "1" ) ;
444
423
// if successful we receive `build.target = "target-triple"`
0 commit comments