Skip to content

Commit

Permalink
Auto merge of #10161 - weihanglo:issue-7473, r=alexcrichton
Browse files Browse the repository at this point in the history
Do not suggest source config if nothing to vendor

fixes #7473

Also remove the empty vendor dir if we've just created it but didn't vendor anything.
  • Loading branch information
bors committed Dec 13, 2021
2 parents 599961b + 8a8d39a commit c7db299
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/cargo/ops/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -75,6 +79,7 @@ fn sync(
) -> CargoResult<VendorConfig> {
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();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 })
}

Expand Down
31 changes: 31 additions & 0 deletions tests/testsuite/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit c7db299

Please sign in to comment.