Skip to content

Commit

Permalink
feat(package): add --exclude-lockfile flag
Browse files Browse the repository at this point in the history
When `--exclude-lockfile` is enabled,
`cargo package` will not verify the lock file if present,
nor will it generate a new one if absent.
Cargo.lock will not be included in the resulting tarball.

Together with `--no-verify`,
this flag decouples packaging from checking the registry index.
While this is useful for some non-normal workflows that requires
to assemble packages having unpublished dependencies.
It is recommended to use `-Zpackage-workspace` to package the entire
workspace, instead of opting out lockfile.
  • Loading branch information
NoisyCoil authored and weihanglo committed Feb 26, 2025
1 parent b7002ff commit 4e8b337
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 75 deletions.
5 changes: 5 additions & 0 deletions src/bin/cargo/commands/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub fn cli() -> Command {
"allow-dirty",
"Allow dirty working directories to be packaged",
))
.arg(flag(
"exclude-lockfile",
"Don't include the lock file when packaging",
))
.arg_silent_suggestion()
.arg_package_spec_no_all(
"Package(s) to assemble",
Expand Down Expand Up @@ -79,6 +83,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
list: args.flag("list"),
check_metadata: !args.flag("no-metadata"),
allow_dirty: args.flag("allow-dirty"),
include_lockfile: !args.flag("exclude-lockfile"),
to_package: specs,
targets: args.targets()?,
jobs: args.jobs()?,
Expand Down
25 changes: 15 additions & 10 deletions src/cargo/ops/cargo_package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct PackageOpts<'gctx> {
pub list: bool,
pub check_metadata: bool,
pub allow_dirty: bool,
pub include_lockfile: bool,
pub verify: bool,
pub jobs: Option<JobsConfig>,
pub keep_going: bool,
Expand Down Expand Up @@ -194,6 +195,7 @@ fn do_package<'a>(
.as_path_unlocked()
.join(LOCKFILE_NAME)
.exists()
&& opts.include_lockfile
{
// Make sure the Cargo.lock is up-to-date and valid.
let dry_run = false;
Expand Down Expand Up @@ -389,7 +391,7 @@ fn prepare_archive(
// Check (git) repository state, getting the current commit hash.
let vcs_info = vcs::check_repo_state(pkg, &src_files, gctx, &opts)?;

build_ar_list(ws, pkg, src_files, vcs_info)
build_ar_list(ws, pkg, src_files, vcs_info, opts.include_lockfile)
}

/// Builds list of files to archive.
Expand All @@ -399,6 +401,7 @@ fn build_ar_list(
pkg: &Package,
src_files: Vec<PathEntry>,
vcs_info: Option<vcs::VcsInfo>,
include_lockfile: bool,
) -> CargoResult<Vec<ArchiveFile>> {
let mut result = HashMap::new();
let root = pkg.root();
Expand Down Expand Up @@ -453,15 +456,17 @@ fn build_ar_list(
))?;
}

let rel_str = "Cargo.lock";
result
.entry(UncasedAscii::new(rel_str))
.or_insert_with(Vec::new)
.push(ArchiveFile {
rel_path: PathBuf::from(rel_str),
rel_str: rel_str.to_string(),
contents: FileContents::Generated(GeneratedFile::Lockfile),
});
if include_lockfile {
let rel_str = "Cargo.lock";
result
.entry(UncasedAscii::new(rel_str))
.or_insert_with(Vec::new)
.push(ArchiveFile {
rel_path: PathBuf::from(rel_str),
rel_str: rel_str.to_string(),
contents: FileContents::Generated(GeneratedFile::Lockfile),
});
}

if let Some(vcs_info) = vcs_info {
let rel_str = VCS_INFO_FILE;
Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/registry/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
list: false,
check_metadata: true,
allow_dirty: opts.allow_dirty,
include_lockfile: true,
// `package_with_dep_graph` ignores this field in favor of
// the already-resolved list of packages
to_package: ops::Packages::Default,
Expand Down
12 changes: 10 additions & 2 deletions src/doc/man/cargo-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ stored in the `target/package` directory. This performs the following steps:
- `[patch]`, `[replace]`, and `[workspace]` sections are removed from the
manifest.
- `Cargo.lock` is always included. When missing, a new lock file will be
generated. {{man "cargo-install" 1}} will use the packaged lock file if
the `--locked` flag is used.
generated unless the `--exclude-lockfile` flag is used. {{man "cargo-install" 1}}
will use the packaged lock file if the `--locked` flag is used.
- A `.cargo_vcs_info.json` file is included that contains information
about the current VCS checkout hash if available, as well as a flag if the
worktree is dirty.
Expand Down Expand Up @@ -98,6 +98,14 @@ or the license).
Allow working directories with uncommitted VCS changes to be packaged.
{{/option}}

{{#option "`--exclude-lockfile`" }}
Don't include the lock file when packaging.

This flag is not for general use.
Some tools may expect a lock file to be present (e.g. `cargo install --locked`).
Consider other options before using this.
{{/option}}

{{> options-index }}

{{#option "`--registry` _registry_"}}
Expand Down
12 changes: 10 additions & 2 deletions src/doc/man/generated_txt/cargo-package.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ DESCRIPTION
manifest.

o Cargo.lock is always included. When missing, a new lock file will
be generated. cargo-install(1) will use the packaged lock file if
the --locked flag is used.
be generated unless the --exclude-lockfile flag is used.
cargo-install(1) will use the packaged lock file if the --locked
flag is used.

o A .cargo_vcs_info.json file is included that contains information
about the current VCS checkout hash if available, as well as a
Expand Down Expand Up @@ -96,6 +97,13 @@ OPTIONS
Allow working directories with uncommitted VCS changes to be
packaged.

--exclude-lockfile
Don’t include the lock file when packaging.

This flag is not for general use. Some tools may expect a lock file
to be present (e.g. cargo install --locked). Consider other options
before using this.

--index index
The URL of the registry index to use.

Expand Down
11 changes: 9 additions & 2 deletions src/doc/src/commands/cargo-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ stored in the `target/package` directory. This performs the following steps:
- `[patch]`, `[replace]`, and `[workspace]` sections are removed from the
manifest.
- `Cargo.lock` is always included. When missing, a new lock file will be
generated. [cargo-install(1)](cargo-install.html) will use the packaged lock file if
the `--locked` flag is used.
generated unless the `--exclude-lockfile` flag is used. [cargo-install(1)](cargo-install.html)
will use the packaged lock file if the `--locked` flag is used.
- A `.cargo_vcs_info.json` file is included that contains information
about the current VCS checkout hash if available, as well as a flag if the
worktree is dirty.
Expand Down Expand Up @@ -94,6 +94,13 @@ or the license).</dd>
<dd class="option-desc">Allow working directories with uncommitted VCS changes to be packaged.</dd>


<dt class="option-term" id="option-cargo-package---exclude-lockfile"><a class="option-anchor" href="#option-cargo-package---exclude-lockfile"></a><code>--exclude-lockfile</code></dt>
<dd class="option-desc">Don’t include the lock file when packaging.</p>
<p>This flag is not for general use.
Some tools may expect a lock file to be present (e.g. <code>cargo install --locked</code>).
Consider other options before using this.</dd>


<dt class="option-term" id="option-cargo-package---index"><a class="option-anchor" href="#option-cargo-package---index"></a><code>--index</code> <em>index</em></dt>
<dd class="option-desc">The URL of the registry index to use.</dd>

Expand Down
13 changes: 11 additions & 2 deletions src/etc/man/cargo-package.1
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ manifest.
.sp
.RS 4
\h'-04'\(bu\h'+03'\fBCargo.lock\fR is always included. When missing, a new lock file will be
generated. \fBcargo\-install\fR(1) will use the packaged lock file if
the \fB\-\-locked\fR flag is used.
generated unless the \fB\-\-exclude\-lockfile\fR flag is used. \fBcargo\-install\fR(1)
will use the packaged lock file if the \fB\-\-locked\fR flag is used.
.RE
.sp
.RS 4
Expand Down Expand Up @@ -127,6 +127,15 @@ or the license).
Allow working directories with uncommitted VCS changes to be packaged.
.RE
.sp
\fB\-\-exclude\-lockfile\fR
.RS 4
Don\[cq]t include the lock file when packaging.
.sp
This flag is not for general use.
Some tools may expect a lock file to be present (e.g. \fBcargo install \-\-locked\fR).
Consider other options before using this.
.RE
.sp
\fB\-\-index\fR \fIindex\fR
.RS 4
The URL of the registry index to use.
Expand Down
70 changes: 36 additions & 34 deletions tests/testsuite/cargo_package/help/stdout.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4e8b337

Please sign in to comment.