Skip to content

Commit 8a8d39a

Browse files
committed
Do not suggest source config if nothing to vendor
1 parent 451a043 commit 8a8d39a

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/cargo/ops/vendor.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ pub fn vendor(ws: &Workspace<'_>, opts: &VendorOptions<'_>) -> CargoResult<()> {
3232
let vendor_config = sync(config, &workspaces, opts).with_context(|| "failed to sync")?;
3333

3434
if config.shell().verbosity() != Verbosity::Quiet {
35-
crate::drop_eprint!(
36-
config,
37-
"To use vendored sources, add this to your .cargo/config.toml for this project:\n\n"
38-
);
39-
crate::drop_print!(config, "{}", &toml::to_string(&vendor_config).unwrap());
35+
if vendor_config.source.is_empty() {
36+
crate::drop_eprintln!(config, "There is no dependency to vendor in this project.");
37+
} else {
38+
crate::drop_eprint!(
39+
config,
40+
"To use vendored sources, add this to your .cargo/config.toml for this project:\n\n"
41+
);
42+
crate::drop_print!(config, "{}", &toml::to_string(&vendor_config).unwrap());
43+
}
4044
}
4145

4246
Ok(())
@@ -75,6 +79,7 @@ fn sync(
7579
) -> CargoResult<VendorConfig> {
7680
let canonical_destination = opts.destination.canonicalize();
7781
let canonical_destination = canonical_destination.as_deref().unwrap_or(opts.destination);
82+
let dest_dir_already_exists = canonical_destination.exists();
7883

7984
paths::create_dir_all(&canonical_destination)?;
8085
let mut to_remove = HashSet::new();
@@ -239,12 +244,6 @@ fn sync(
239244
let mut config = BTreeMap::new();
240245

241246
let merged_source_name = "vendored-sources";
242-
config.insert(
243-
merged_source_name.to_string(),
244-
VendorSource::Directory {
245-
directory: opts.destination.to_path_buf(),
246-
},
247-
);
248247

249248
// replace original sources with vendor
250249
for source_id in sources {
@@ -290,6 +289,18 @@ fn sync(
290289
config.insert(name, source);
291290
}
292291

292+
if !config.is_empty() {
293+
config.insert(
294+
merged_source_name.to_string(),
295+
VendorSource::Directory {
296+
directory: opts.destination.to_path_buf(),
297+
},
298+
);
299+
} else if !dest_dir_already_exists {
300+
// Nothing to vendor. Remove the destination dir we've just created.
301+
paths::remove_dir(canonical_destination)?;
302+
}
303+
293304
Ok(VendorConfig { source: config })
294305
}
295306

tests/testsuite/vendor.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,3 +753,34 @@ fn vendor_preserves_permissions() {
753753
let metadata = fs::metadata(p.root().join("vendor/bar/example.sh")).unwrap();
754754
assert_eq!(metadata.mode() & 0o777, 0o755);
755755
}
756+
757+
#[cargo_test]
758+
fn no_remote_dependency_no_vendor() {
759+
let p = project()
760+
.file(
761+
"Cargo.toml",
762+
r#"
763+
[package]
764+
name = "foo"
765+
version = "0.1.0"
766+
[dependencies]
767+
bar = { path = "bar" }
768+
"#,
769+
)
770+
.file("src/lib.rs", "")
771+
.file(
772+
"bar/Cargo.toml",
773+
r#"
774+
[package]
775+
name = "bar"
776+
version = "0.1.0"
777+
"#,
778+
)
779+
.file("bar/src/lib.rs", "")
780+
.build();
781+
782+
p.cargo("vendor")
783+
.with_stderr("There is no dependency to vendor in this project.")
784+
.run();
785+
assert!(!p.root().join("vendor").exists());
786+
}

0 commit comments

Comments
 (0)