From 9e2b373acb59d3ad3ca7d53c5443fd7f8c079f8a Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 19 Dec 2024 20:18:05 -0500 Subject: [PATCH 1/2] test(package): relative path to cwd for dirtiness report --- tests/testsuite/package.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 75d21ffb2ce..31c834e401c 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1099,6 +1099,20 @@ Cargo.toml to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag +"#]]) + .run(); + + // cd to `src` and cargo report relative paths. + p.cargo("package") + .cwd(p.root().join("src")) + .with_status(101) + .with_stderr_data(str![[r#" +[ERROR] 1 files in the working directory contain changes that were not yet committed into git: + +Cargo.toml + +to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag + "#]]) .run(); } From d325acea1d51d7ababf580500ef15228d46b2df1 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 19 Dec 2024 20:22:18 -0500 Subject: [PATCH 2/2] fix(package): use relpath to cwd for vcs dirtiness report Address https://github.com/rust-lang/cargo/pull/14968#issuecomment-2555901072 --- src/cargo/ops/cargo_package.rs | 8 +++++--- tests/testsuite/package.rs | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index b12a72db579..b54a16d2c9b 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -796,7 +796,7 @@ fn check_repo_state( .and_then(|p| p.to_str()) .unwrap_or("") .replace("\\", "/"); - let Some(git) = git(src_files, &repo, &opts)? else { + let Some(git) = git(gctx, src_files, &repo, &opts)? else { // If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning, // then don't generate the corresponding file in order to maintain consistency with past behavior. return Ok(None); @@ -805,6 +805,7 @@ fn check_repo_state( return Ok(Some(VcsInfo { git, path_in_vcs })); fn git( + gctx: &GlobalContext, src_files: &[PathBuf], repo: &git2::Repository, opts: &PackageOpts<'_>, @@ -823,12 +824,13 @@ fn check_repo_state( // Find the intersection of dirty in git, and the src_files that would // be packaged. This is a lazy n^2 check, but seems fine with // thousands of files. - let workdir = repo.workdir().unwrap(); + let cwd = gctx.cwd(); let mut dirty_src_files: Vec<_> = src_files .iter() .filter(|src_file| dirty_files.iter().any(|path| src_file.starts_with(path))) .map(|path| { - path.strip_prefix(workdir) + pathdiff::diff_paths(path, cwd) + .as_ref() .unwrap_or(path) .display() .to_string() diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 31c834e401c..1740de4ac77 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1109,7 +1109,7 @@ to proceed despite this and include the uncommitted changes, pass the `--allow-d .with_stderr_data(str![[r#" [ERROR] 1 files in the working directory contain changes that were not yet committed into git: -Cargo.toml +../Cargo.toml to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag