@@ -33,6 +33,8 @@ pub struct CargoWorkspace {
33
33
workspace_root : AbsPathBuf ,
34
34
target_directory : AbsPathBuf ,
35
35
manifest_path : ManifestPath ,
36
+ // Whether this workspace was queried with `--no-deps`.
37
+ no_deps : bool ,
36
38
}
37
39
38
40
impl ops:: Index < Package > for CargoWorkspace {
@@ -259,6 +261,18 @@ impl CargoWorkspace {
259
261
sysroot : & Sysroot ,
260
262
locked : bool ,
261
263
progress : & dyn Fn ( String ) ,
264
+ ) -> anyhow:: Result < cargo_metadata:: Metadata > {
265
+ Self :: fetch_metadata_ ( cargo_toml, current_dir, config, sysroot, locked, false , progress)
266
+ }
267
+
268
+ fn fetch_metadata_ (
269
+ cargo_toml : & ManifestPath ,
270
+ current_dir : & AbsPath ,
271
+ config : & CargoConfig ,
272
+ sysroot : & Sysroot ,
273
+ locked : bool ,
274
+ no_deps : bool ,
275
+ progress : & dyn Fn ( String ) ,
262
276
) -> anyhow:: Result < cargo_metadata:: Metadata > {
263
277
let targets = find_list_of_build_targets ( config, cargo_toml, sysroot) ;
264
278
@@ -314,6 +328,9 @@ impl CargoWorkspace {
314
328
if locked {
315
329
other_options. push ( "--locked" . to_owned ( ) ) ;
316
330
}
331
+ if no_deps {
332
+ other_options. push ( "--no-deps" . to_owned ( ) ) ;
333
+ }
317
334
meta. other_options ( other_options) ;
318
335
319
336
// FIXME: Fetching metadata is a slow process, as it might require
@@ -324,6 +341,22 @@ impl CargoWorkspace {
324
341
( || -> Result < cargo_metadata:: Metadata , cargo_metadata:: Error > {
325
342
let output = meta. cargo_command ( ) . output ( ) ?;
326
343
if !output. status . success ( ) {
344
+ if !no_deps {
345
+ // If we failed to fetch metadata with deps, try again without them.
346
+ // This makes r-a still work partially when offline.
347
+ if let Ok ( metadata) = Self :: fetch_metadata_ (
348
+ cargo_toml,
349
+ current_dir,
350
+ config,
351
+ sysroot,
352
+ locked,
353
+ true ,
354
+ progress,
355
+ ) {
356
+ return Ok ( metadata) ;
357
+ }
358
+ }
359
+
327
360
return Err ( cargo_metadata:: Error :: CargoMetadata {
328
361
stderr : String :: from_utf8 ( output. stderr ) ?,
329
362
} ) ;
@@ -431,8 +464,8 @@ impl CargoWorkspace {
431
464
pkg_data. targets . push ( tgt) ;
432
465
}
433
466
}
434
- let resolve = meta. resolve . expect ( "metadata executed with deps" ) ;
435
- for mut node in resolve. nodes {
467
+ let no_deps = meta. resolve . is_none ( ) ;
468
+ for mut node in meta . resolve . map_or_else ( Vec :: new , |it| it . nodes ) {
436
469
let & source = pkg_by_id. get ( & node. id ) . unwrap ( ) ;
437
470
node. deps . sort_by ( |a, b| a. pkg . cmp ( & b. pkg ) ) ;
438
471
let dependencies = node
@@ -451,7 +484,14 @@ impl CargoWorkspace {
451
484
452
485
let target_directory = AbsPathBuf :: assert ( meta. target_directory ) ;
453
486
454
- CargoWorkspace { packages, targets, workspace_root, target_directory, manifest_path }
487
+ CargoWorkspace {
488
+ packages,
489
+ targets,
490
+ workspace_root,
491
+ target_directory,
492
+ manifest_path,
493
+ no_deps,
494
+ }
455
495
}
456
496
457
497
pub fn packages ( & self ) -> impl ExactSizeIterator < Item = Package > + ' _ {
@@ -533,6 +573,10 @@ impl CargoWorkspace {
533
573
fn is_unique ( & self , name : & str ) -> bool {
534
574
self . packages . iter ( ) . filter ( |( _, v) | v. name == name) . count ( ) == 1
535
575
}
576
+
577
+ pub fn no_deps ( & self ) -> bool {
578
+ self . no_deps
579
+ }
536
580
}
537
581
538
582
fn find_list_of_build_targets (
0 commit comments