diff --git a/src/cargo/ops/vendor.rs b/src/cargo/ops/vendor.rs index adae834c243..8ecb802d7b8 100644 --- a/src/cargo/ops/vendor.rs +++ b/src/cargo/ops/vendor.rs @@ -32,11 +32,15 @@ pub fn vendor(ws: &Workspace<'_>, opts: &VendorOptions<'_>) -> CargoResult<()> { let vendor_config = sync(config, &workspaces, opts).with_context(|| "failed to sync")?; if config.shell().verbosity() != Verbosity::Quiet { - crate::drop_eprint!( - config, - "To use vendored sources, add this to your .cargo/config.toml for this project:\n\n" - ); - crate::drop_print!(config, "{}", &toml::to_string(&vendor_config).unwrap()); + if vendor_config.source.is_empty() { + crate::drop_eprintln!(config, "There is no dependency to vendor in this project."); + } else { + crate::drop_eprint!( + config, + "To use vendored sources, add this to your .cargo/config.toml for this project:\n\n" + ); + crate::drop_print!(config, "{}", &toml::to_string(&vendor_config).unwrap()); + } } Ok(()) @@ -75,6 +79,7 @@ fn sync( ) -> CargoResult { let canonical_destination = opts.destination.canonicalize(); let canonical_destination = canonical_destination.as_deref().unwrap_or(opts.destination); + let dest_dir_already_exists = canonical_destination.exists(); paths::create_dir_all(&canonical_destination)?; let mut to_remove = HashSet::new(); @@ -239,12 +244,6 @@ fn sync( let mut config = BTreeMap::new(); let merged_source_name = "vendored-sources"; - config.insert( - merged_source_name.to_string(), - VendorSource::Directory { - directory: opts.destination.to_path_buf(), - }, - ); // replace original sources with vendor for source_id in sources { @@ -290,6 +289,18 @@ fn sync( config.insert(name, source); } + if !config.is_empty() { + config.insert( + merged_source_name.to_string(), + VendorSource::Directory { + directory: opts.destination.to_path_buf(), + }, + ); + } else if !dest_dir_already_exists { + // Nothing to vendor. Remove the destination dir we've just created. + paths::remove_dir(canonical_destination)?; + } + Ok(VendorConfig { source: config }) } diff --git a/tests/testsuite/vendor.rs b/tests/testsuite/vendor.rs index 09968aa949e..75d78d7d9e6 100644 --- a/tests/testsuite/vendor.rs +++ b/tests/testsuite/vendor.rs @@ -753,3 +753,34 @@ fn vendor_preserves_permissions() { let metadata = fs::metadata(p.root().join("vendor/bar/example.sh")).unwrap(); assert_eq!(metadata.mode() & 0o777, 0o755); } + +#[cargo_test] +fn no_remote_dependency_no_vendor() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + [dependencies] + bar = { path = "bar" } + "#, + ) + .file("src/lib.rs", "") + .file( + "bar/Cargo.toml", + r#" + [package] + name = "bar" + version = "0.1.0" + "#, + ) + .file("bar/src/lib.rs", "") + .build(); + + p.cargo("vendor") + .with_stderr("There is no dependency to vendor in this project.") + .run(); + assert!(!p.root().join("vendor").exists()); +}