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

Normalize path for cargo vendor output #10668

Merged
merged 4 commits into from
Jul 16, 2022
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
7 changes: 5 additions & 2 deletions src/cargo/ops/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct VendorConfig {
#[serde(rename_all = "lowercase", untagged)]
enum VendorSource {
Directory {
directory: PathBuf,
directory: String,
},
Registry {
registry: Option<String>,
Expand Down Expand Up @@ -298,7 +298,10 @@ fn sync(
config.insert(
merged_source_name.to_string(),
VendorSource::Directory {
directory: opts.destination.to_path_buf(),
// Windows-flavour paths are valid here on Windows but Unix.
// This backslash normalization is for making output paths more
// cross-platform compatible.
directory: opts.destination.to_string_lossy().replace("\\", "/"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here explaining why it is replacing the slashes?

},
);
} else if !dest_dir_already_exists {
Expand Down
49 changes: 48 additions & 1 deletion tests/testsuite/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,53 @@ directory = "vendor"
.run();
}

#[cargo_test]
fn vendor_path_specified() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"

[dependencies]
log = "0.3.5"
"#,
)
.file("src/lib.rs", "")
.build();

Package::new("log", "0.3.5").publish();

let path = if cfg!(windows) {
r#"deps\.vendor"#
} else {
"deps/.vendor"
};

let output = p
.cargo("vendor --respect-source-config")
.arg(path)
.exec_with_output()
.unwrap();
// Assert against original output to ensure that
// path is normalized by `ops::vendor` on Windows.
assert_eq!(
&String::from_utf8(output.stdout).unwrap(),
r#"
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "deps/.vendor"
"#
);

let lock = p.read_file("deps/.vendor/log/Cargo.toml");
assert!(lock.contains("version = \"0.3.5\""));
}

fn add_vendor_config(p: &Project) {
p.change_file(
".cargo/config",
Expand Down Expand Up @@ -117,7 +164,7 @@ fn package_exclude() {
.publish();

p.cargo("vendor --respect-source-config").run();
let csum = dbg!(p.read_file("vendor/bar/.cargo-checksum.json"));
let csum = p.read_file("vendor/bar/.cargo-checksum.json");
assert!(csum.contains(".include"));
assert!(!csum.contains(".exclude"));
assert!(!csum.contains(".dotdir/exclude"));
Expand Down