Skip to content
Merged
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
11 changes: 10 additions & 1 deletion src/cargo/ops/cargo_package/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use crate::core::{Package, Workspace};
use crate::ops::PackageOpts;
use crate::sources::PathEntry;
use crate::{CargoResult, GlobalContext};

use annotate_snippets::Level;
use anyhow::Context;
use cargo_util::paths;
use gix::bstr::BString;
use gix::bstr::ByteSlice;
use gix::bstr::ByteVec;
use gix::dir::walk::EmissionMode;
use gix::dirwalk::Options;
use gix::index::entry::Mode;
Expand Down Expand Up @@ -286,7 +289,13 @@ fn collect_statuses(
.index_worktree_submodules(None)
.into_iter(
relative_package_root.map(|rela_pkg_root| {
gix::path::into_bstr(rela_pkg_root).into_owned()
// Use `:(top)` magic signature to make the pathspec relative to
// the repo root, not the current working directory.
let mut pathspec = BString::from(":(top)");
let prefix =
gix::path::to_unix_separators_on_windows(gix::path::into_bstr(rela_pkg_root));
pathspec.push_str(prefix.as_ref());
pathspec
}), /* pathspec patterns */
)
.with_context(|| {
Expand Down
53 changes: 53 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,59 @@ to proceed despite this and include the uncommitted changes, pass the `--allow-d
);
}

/// Regression test for https://github.com/rust-lang/cargo/issues/16478
#[cargo_test]
fn dirty_untracked_file_when_packaged_from_workspace_member() {
let (p, repo) = git::new_repo("foo", |p| {
p.file(
"Cargo.toml",
r#"
[workspace]
members = ["inner"]
resolver = "2"
"#,
)
.file(
"inner/Cargo.toml",
r#"
[package]
name = "inner"
edition = "2021"
"#,
)
.file("inner/src/lib.rs", "")
});
git::commit(&repo);

p.change_file("inner/untracked", "untracked");

p.cargo("package --list --no-metadata")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:

inner/untracked

to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag

"#]])
.run();

// Running from workspace member directory should also detect the untracked file.
p.cargo("package --list --no-metadata")
.cwd("inner")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:

untracked

to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag

"#]])
.run();
}

#[cargo_test]
fn dirty_file_outside_pkg_root_considered_dirty() {
if !symlink_supported() {
Expand Down