Skip to content

Commit 6baf1f5

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 b3e5712 commit 6baf1f5

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/cargo/ops/cargo_package/vcs.rs

Lines changed: 34 additions & 1 deletion
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

@@ -133,12 +134,14 @@ fn git(
133134
// Find the intersection of dirty in git, and the src_files that would
134135
// be packaged. This is a lazy n^2 check, but seems fine with
135136
// thousands of files.
137+
let mut dirty_files_outside_pkg_root = dirty_symlinks(pkg, repo, src_files)?;
138+
dirty_files_outside_pkg_root.extend(dirty_metadata_paths(pkg, repo)?);
136139
let cwd = gctx.cwd();
137140
let mut dirty_src_files: Vec<_> = src_files
138141
.iter()
139142
.filter(|src_file| dirty_files.iter().any(|path| src_file.starts_with(path)))
140143
.map(|p| p.as_ref())
141-
.chain(dirty_metadata_paths(pkg, repo)?.iter())
144+
.chain(dirty_files_outside_pkg_root.iter())
142145
.map(|path| {
143146
pathdiff::diff_paths(path, cwd)
144147
.as_ref()
@@ -206,6 +209,36 @@ fn dirty_metadata_paths(pkg: &Package, repo: &git2::Repository) -> CargoResult<V
206209
Ok(dirty_files)
207210
}
208211

212+
/// Checks whether source files are symlinks and have been modified.
213+
///
214+
/// This is required because those paths may link to a file outside the
215+
/// current package root, but still under the git workdir, affecting the
216+
/// final packaged `.crate` file.
217+
fn dirty_symlinks(
218+
pkg: &Package,
219+
repo: &git2::Repository,
220+
src_files: &[PathEntry],
221+
) -> CargoResult<HashSet<PathBuf>> {
222+
let workdir = repo.workdir().unwrap();
223+
let mut dirty_symlinks = HashSet::new();
224+
for rel_path in src_files
225+
.iter()
226+
.filter(|p| p.is_symlink_or_under_symlink())
227+
.map(|p| p.as_ref().as_path())
228+
// If inside package root. Don't bother checking git status.
229+
.filter(|p| paths::strip_prefix_canonical(*p, pkg.root()).is_err())
230+
// Handle files outside package root but under git workdir,
231+
.filter_map(|p| paths::strip_prefix_canonical(p, workdir).ok())
232+
{
233+
// TODO: Should we warn users if there are like thousands of symlinks,
234+
// which may hurt the performance?
235+
if repo.status_file(&rel_path)? != git2::Status::CURRENT {
236+
dirty_symlinks.insert(workdir.join(rel_path));
237+
}
238+
}
239+
Ok(dirty_symlinks)
240+
}
241+
209242
/// Helper to collect dirty statuses for a single repo.
210243
fn collect_statuses(repo: &git2::Repository, dirty_files: &mut Vec<PathBuf>) -> CargoResult<()> {
211244
let mut status_opts = git2::StatusOptions::new();

tests/testsuite/package.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,12 @@ 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] 4 files in the working directory contain changes that were not yet committed into git:
13821382
13831383
LICENSE
13841384
README.md
1385+
lib.rs
1386+
original-dir/file
13851387
13861388
to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag
13871389

0 commit comments

Comments
 (0)