{"id":"85f7c9fdb7f3d099","repo":"rust-lang/cargo","slug":"files-in-the-working-directory-contain-changes","errorCode":null,"errorMessage":"{} files in the working directory contain changes that were not yet committed into git:\n\n{}\n\nto proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag","messagePattern":"(.+?) files in the working directory contain changes that were not yet committed into git:\n\n(.+?)\n\nto proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_package/vcs.rs","lineNumber":264,"sourceCode":"        .chain(dirty_files_outside_pkg_root(ws, pkg, repo, src_files)?.iter())\n        .map(|path| {\n            pathdiff::diff_paths(path, cwd)\n                .as_ref()\n                .unwrap_or(path)\n                .display()\n                .to_string()\n        })\n        .collect();\n    let dirty = !dirty_src_files.is_empty();\n    if !dirty || opts.allow_dirty {\n        let maybe_head_id = repo.head()?.try_peel_to_id()?;\n        Ok(maybe_head_id.map(|id| GitVcsInfo {\n            sha1: id.to_string(),\n            dirty,\n        }))\n    } else {\n        dirty_src_files.sort_unstable();\n        anyhow::bail!(\n            \"{} files in the working directory contain changes that were \\\n             not yet committed into git:\\n\\n{}\\n\\n\\\n             to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag\",\n            dirty_src_files.len(),\n            dirty_src_files.join(\"\\n\")\n        )\n    }\n}\n\n/// Helper to collect dirty statuses for a single repo.\n/// `relative_package_root` is `Some` if the root is a sub-directory of the workdir.\n/// Writes dirty files outside `relative_package_root` into `dirty_files_outside_package_root`,\n/// and all *everything else* into `dirty_files`.\n#[must_use]\nfn collect_statuses(\n    repo: &gix::Repository,\n    workdir: &Path,\n    relative_package_root: Option<&Path>,","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_package/vcs.rs#L246-L282","documentation":"Before packaging, Cargo runs a git status check over the package source. If any tracked files (or files under the package root) are modified/uncommitted it bails, because the published crate would not match what is recorded in version control. The guard lives in cargo_package/vcs.rs and can be bypassed with --allow-dirty.","triggerScenarios":"Running `cargo package` or `cargo publish` while `git status` reports modified, staged-but-uncommitted, or untracked-in-source files for the package being packaged.","commonSituations":"Edited source after the last commit; generated files (e.g. from a codegen step) not added to git; switching branches with leftover changes; CI that builds from a dirty worktree.","solutions":["Commit or stash the uncommitted changes listed in the error, then re-run `cargo package`.","If the changes are intentional and should ship in the crate, pass `--allow-dirty` (e.g. `cargo publish --allow-dirty`).","Add generated/transient files to .gitignore so they no longer count as dirty."],"exampleFix":"# before\n$ cargo publish\nerror: 3 files in the working directory contain changes ...\n\n# after (commit first)\n$ git add -A && git commit -m \"prep release\"\n$ cargo publish\n\n# or, ship the dirty tree knowingly:\n$ cargo publish --allow-dirty","handlingStrategy":"validation","validationCode":"# Fail fast if the worktree is dirty before packaging:\nif [ -n \"$(git status --porcelain -- $PKG_ROOT)\" ]; then\n  echo \"worktree dirty; commit or pass --allow-dirty\" >&2\n  exit 1\nfi\ncargo package","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always commit (or stash) before `cargo publish` so the crate matches HEAD.","Use `cargo package --allow-dirty` only when you understand the published crate will diverge from git.","CI: run `git diff --exit-code` (or stash) before publish."],"tags":["packaging","vcs","git","publish","dirty"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}