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

Case insensitive file name matching when packaging files #13885

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
26 changes: 17 additions & 9 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,23 +366,31 @@ fn check_for_file_and_add(
result: &mut Vec<ArchiveFile>,
ws: &Workspace<'_>,
) -> CargoResult<()> {
fn compare_file_names_case_insensitive(result: &Vec<ArchiveFile>, rel_str: &str) -> bool {
result
.iter()
.any(|ar| ar.rel_path.to_string_lossy().to_lowercase() == rel_str.to_lowercase())
}

match abs_file_path.strip_prefix(&pkg.root()) {
Ok(rel_file_path) => {
if !result.iter().any(|ar| ar.rel_path == rel_file_path) {
let rel_str = rel_file_path
.to_str()
.expect("everything was utf8")
.to_string();

if !compare_file_names_case_insensitive(result, &rel_str) {
result.push(ArchiveFile {
rel_path: rel_file_path.to_path_buf(),
rel_str: rel_file_path
.to_str()
.expect("everything was utf8")
.to_string(),
rel_str,
contents: FileContents::OnDisk(abs_file_path),
})
}
}
Err(_) => {
// The file exists somewhere outside of the package.
let file_name = file_path.file_name().unwrap();
if result.iter().any(|ar| ar.rel_path == file_name) {
let file_name = file_path.file_name().unwrap().to_str().unwrap();
if compare_file_names_case_insensitive(result, file_name) {
ws.gctx().shell().warn(&format!(
"{} `{}` appears to be a path outside of the package, \
but there is already a file named `{}` in the root of the package. \
Expand All @@ -391,13 +399,13 @@ fn check_for_file_and_add(
to the root of the package to remove this warning.",
label,
file_path.display(),
file_name.to_str().unwrap(),
file_name,
label,
))?;
} else {
result.push(ArchiveFile {
rel_path: PathBuf::from(file_name),
rel_str: file_name.to_str().unwrap().to_string(),
rel_str: file_name.to_string(),
contents: FileContents::OnDisk(abs_file_path),
})
}
Expand Down
34 changes: 34 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4928,3 +4928,37 @@ path = "src/lib.rs"
)],
);
}

#[cargo_test]
fn issue_13722_each_file_should_only_be_added_once() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
readme = "READme.MD"
license-file = "LICense"
"#,
)
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.file("rEaDmE.Md", "")
.file("lIcEnse", "")
.build();

p.cargo("package").run();
assert!(p.root().join("target/package/foo-0.0.1.crate").is_file());
p.cargo("package -l")
.with_stdout(
"\
Cargo.lock
Cargo.toml
Cargo.toml.orig
lIcEnse
rEaDmE.Md
src/main.rs
",
)
.run();
}