Skip to content

Commit de39f59

Browse files
committed
fix(package): check dirtiness of symlink source files
This adds a special case for checking source files are symlinks and have been modified when under a VCS control This is required because those paths may link to a file outside the current package root, but still under the git workdir, affecting the final packaged `.crate` file. This may have potential performance issue. If a package contains thousands of symlinks, Cargo will fire `git status` for each of them.
1 parent 014e516 commit de39f59

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/cargo/ops/cargo_package/vcs.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Helpers to gather the VCS information for `cargo package`.
22
3+
use std::collections::HashSet;
34
use std::path::Path;
45
use std::path::PathBuf;
56

@@ -187,6 +188,7 @@ fn git(
187188
.filter(|src_file| dirty_files.iter().any(|path| src_file.starts_with(path)))
188189
.map(|p| p.as_ref())
189190
.chain(dirty_metadata_paths(pkg, repo)?.iter())
191+
.chain(dirty_symlinks(pkg, repo, src_files)?.iter())
190192
.map(|path| {
191193
pathdiff::diff_paths(path, cwd)
192194
.as_ref()
@@ -249,6 +251,34 @@ fn dirty_metadata_paths(pkg: &Package, repo: &git2::Repository) -> CargoResult<V
249251
Ok(dirty_files)
250252
}
251253

254+
/// Checks whether source files are symlinks and have been modified.
255+
///
256+
/// This is required because those paths may link to a file outside the
257+
/// current package root, but still under the git workdir, affecting the
258+
/// final packaged `.crate` file.
259+
fn dirty_symlinks(
260+
pkg: &Package,
261+
repo: &git2::Repository,
262+
src_files: &[PathEntry],
263+
) -> CargoResult<HashSet<PathBuf>> {
264+
let workdir = repo.workdir().unwrap();
265+
let mut dirty_symlinks = HashSet::new();
266+
for rel_path in src_files
267+
.iter()
268+
.filter(|p| p.is_symlink_or_under_symlink())
269+
.map(|p| p.as_ref().as_path())
270+
// If inside package root. Don't bother checking git status.
271+
.filter(|p| paths::strip_prefix_canonical(p, pkg.root()).is_err())
272+
// Handle files outside package root but under git workdir,
273+
.filter_map(|p| paths::strip_prefix_canonical(p, workdir).ok())
274+
{
275+
if repo.status_file(&rel_path)? != git2::Status::CURRENT {
276+
dirty_symlinks.insert(workdir.join(rel_path));
277+
}
278+
}
279+
Ok(dirty_symlinks)
280+
}
281+
252282
/// Helper to collect dirty statuses for a single repo.
253283
fn collect_statuses(repo: &git2::Repository, dirty_files: &mut Vec<PathBuf>) -> CargoResult<()> {
254284
let mut status_opts = git2::StatusOptions::new();

tests/testsuite/package.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,13 @@ fn dirty_file_outside_pkg_root_considered_dirty() {
13781378
p.cargo("package --workspace --no-verify")
13791379
.with_status(101)
13801380
.with_stderr_data(str![[r#"
1381-
[ERROR] 2 files in the working directory contain changes that were not yet committed into git:
1381+
[ERROR] 5 files in the working directory contain changes that were not yet committed into git:
13821382
13831383
LICENSE
13841384
README.md
1385+
README.md
1386+
lib.rs
1387+
original-dir/file
13851388
13861389
to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag
13871390

0 commit comments

Comments
 (0)