Skip to content

Commit 863e1f4

Browse files
committed
fix(package): check dirtiness of path fields in manifest
This adds a special case for `package.{readme,license-file}` to Git VCS status check. If they were specified with paths outside the current package root, but still under git workdir, Cargo checks git status of those files to determine if they were dirty. We don't need to take care of other fields with path values because * `PathSource` only list files under the package root. Things like `target.path` works for `cargo build`, but won't be included in `.crate` file from `cargo publish`. * The only exceptions are `package.readme`/`package.license-file`. Cargo would copy files over if they are outside package root.
1 parent a991a7d commit 863e1f4

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ fn check_repo_state(
796796
.and_then(|p| p.to_str())
797797
.unwrap_or("")
798798
.replace("\\", "/");
799-
let Some(git) = git(gctx, src_files, &repo, &opts)? else {
799+
let Some(git) = git(p, gctx, src_files, &repo, &opts)? else {
800800
// If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
801801
// then don't generate the corresponding file in order to maintain consistency with past behavior.
802802
return Ok(None);
@@ -805,6 +805,7 @@ fn check_repo_state(
805805
return Ok(Some(VcsInfo { git, path_in_vcs }));
806806

807807
fn git(
808+
pkg: &Package,
808809
gctx: &GlobalContext,
809810
src_files: &[PathBuf],
810811
repo: &git2::Repository,
@@ -828,6 +829,7 @@ fn check_repo_state(
828829
let mut dirty_src_files: Vec<_> = src_files
829830
.iter()
830831
.filter(|src_file| dirty_files.iter().any(|path| src_file.starts_with(path)))
832+
.chain(dirty_metadata_paths(pkg, repo)?.iter())
831833
.map(|path| {
832834
pathdiff::diff_paths(path, cwd)
833835
.as_ref()
@@ -860,6 +862,41 @@ fn check_repo_state(
860862
}
861863
}
862864

865+
/// Checks whether files at paths specified in `package.readme` and
866+
/// `package.license-file` have been modified.
867+
///
868+
/// This is required because those paths may link to a file outside the
869+
/// current package root, but still under the git workdir, affecting the
870+
/// final packaged `.crate` file.
871+
fn dirty_metadata_paths(pkg: &Package, repo: &git2::Repository) -> CargoResult<Vec<PathBuf>> {
872+
let mut dirty_files = Vec::new();
873+
let workdir = repo.workdir().unwrap();
874+
let root = pkg.root();
875+
let meta = pkg.manifest().metadata();
876+
for path in [&meta.license_file, &meta.readme] {
877+
let Some(path) = path.as_deref().map(Path::new) else {
878+
continue;
879+
};
880+
let abs_path = paths::normalize_path(&root.join(path));
881+
if paths::strip_prefix_canonical(abs_path.as_path(), root).is_ok() {
882+
// Inside package root. Don't bother checking git status.
883+
continue;
884+
}
885+
if let Ok(rel_path) = paths::strip_prefix_canonical(abs_path.as_path(), workdir) {
886+
// Outside package root but under git workdir,
887+
if repo.status_file(&rel_path)? != git2::Status::CURRENT {
888+
dirty_files.push(if abs_path.is_symlink() {
889+
// For symlinks, shows paths to symlink sources
890+
workdir.join(rel_path)
891+
} else {
892+
abs_path
893+
});
894+
}
895+
}
896+
}
897+
Ok(dirty_files)
898+
}
899+
863900
// Helper to collect dirty statuses for a single repo.
864901
fn collect_statuses(
865902
repo: &git2::Repository,

tests/testsuite/package.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,9 +1373,14 @@ fn dirty_file_outside_pkg_root_considered_dirty() {
13731373

13741374
// Ensure dirty files be reported.
13751375
p.cargo("package --workspace --no-verify")
1376+
.with_status(101)
13761377
.with_stderr_data(str![[r#"
1377-
[PACKAGING] isengard v0.0.0 ([ROOT]/foo/isengard)
1378-
[PACKAGED] 8 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
1378+
[ERROR] 2 files in the working directory contain changes that were not yet committed into git:
1379+
1380+
LICENSE
1381+
README.md
1382+
1383+
to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag
13791384
13801385
"#]])
13811386
.run();

0 commit comments

Comments
 (0)