Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not suggest source config if nothing to vendor #10161

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}