Skip to content

Commit 6cf8267

Browse files
authored
feat(package): add --exclude-lockfile flag (#15234)
### What does this PR try to resolve? Fixes #15059 Fixes #15159 This provides an escape hatch `--exclude-lockfile`for uncommon workflows that don't verify (`--no-verify` is passed) the build with their unpublished packages In effect, this takes the heuristic removed in #14815 and replaces it with a flag 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. ### How should we test and review this PR? The first commit was stolen from <NoisyCoil@1a104b5> (credit to @NoisyCoil!) The second added two failing cases we observed in #15059. ### Additional information
2 parents ca694a8 + 4e8b337 commit 6cf8267

File tree

9 files changed

+240
-52
lines changed

9 files changed

+240
-52
lines changed

src/bin/cargo/commands/package.rs

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ pub fn cli() -> Command {
2626
"allow-dirty",
2727
"Allow dirty working directories to be packaged",
2828
))
29+
.arg(flag(
30+
"exclude-lockfile",
31+
"Don't include the lock file when packaging",
32+
))
2933
.arg_silent_suggestion()
3034
.arg_package_spec_no_all(
3135
"Package(s) to assemble",
@@ -79,6 +83,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
7983
list: args.flag("list"),
8084
check_metadata: !args.flag("no-metadata"),
8185
allow_dirty: args.flag("allow-dirty"),
86+
include_lockfile: !args.flag("exclude-lockfile"),
8287
to_package: specs,
8388
targets: args.targets()?,
8489
jobs: args.jobs()?,

src/cargo/ops/cargo_package/mod.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub struct PackageOpts<'gctx> {
4646
pub list: bool,
4747
pub check_metadata: bool,
4848
pub allow_dirty: bool,
49+
pub include_lockfile: bool,
4950
pub verify: bool,
5051
pub jobs: Option<JobsConfig>,
5152
pub keep_going: bool,
@@ -191,6 +192,7 @@ fn do_package<'a>(
191192
.as_path_unlocked()
192193
.join(LOCKFILE_NAME)
193194
.exists()
195+
&& opts.include_lockfile
194196
{
195197
// Make sure the Cargo.lock is up-to-date and valid.
196198
let dry_run = false;
@@ -386,7 +388,7 @@ fn prepare_archive(
386388
// Check (git) repository state, getting the current commit hash.
387389
let vcs_info = vcs::check_repo_state(pkg, &src_files, ws, &opts)?;
388390

389-
build_ar_list(ws, pkg, src_files, vcs_info)
391+
build_ar_list(ws, pkg, src_files, vcs_info, opts.include_lockfile)
390392
}
391393

392394
/// Builds list of files to archive.
@@ -396,6 +398,7 @@ fn build_ar_list(
396398
pkg: &Package,
397399
src_files: Vec<PathEntry>,
398400
vcs_info: Option<vcs::VcsInfo>,
401+
include_lockfile: bool,
399402
) -> CargoResult<Vec<ArchiveFile>> {
400403
let mut result = HashMap::new();
401404
let root = pkg.root();
@@ -450,15 +453,17 @@ fn build_ar_list(
450453
))?;
451454
}
452455

453-
let rel_str = "Cargo.lock";
454-
result
455-
.entry(UncasedAscii::new(rel_str))
456-
.or_insert_with(Vec::new)
457-
.push(ArchiveFile {
458-
rel_path: PathBuf::from(rel_str),
459-
rel_str: rel_str.to_string(),
460-
contents: FileContents::Generated(GeneratedFile::Lockfile),
461-
});
456+
if include_lockfile {
457+
let rel_str = "Cargo.lock";
458+
result
459+
.entry(UncasedAscii::new(rel_str))
460+
.or_insert_with(Vec::new)
461+
.push(ArchiveFile {
462+
rel_path: PathBuf::from(rel_str),
463+
rel_str: rel_str.to_string(),
464+
contents: FileContents::Generated(GeneratedFile::Lockfile),
465+
});
466+
}
462467

463468
if let Some(vcs_info) = vcs_info {
464469
let rel_str = VCS_INFO_FILE;

src/cargo/ops/registry/publish.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
146146
list: false,
147147
check_metadata: true,
148148
allow_dirty: opts.allow_dirty,
149+
include_lockfile: true,
149150
// `package_with_dep_graph` ignores this field in favor of
150151
// the already-resolved list of packages
151152
to_package: ops::Packages::Default,

src/doc/man/cargo-package.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ stored in the `target/package` directory. This performs the following steps:
2727
- `[patch]`, `[replace]`, and `[workspace]` sections are removed from the
2828
manifest.
2929
- `Cargo.lock` is always included. When missing, a new lock file will be
30-
generated. {{man "cargo-install" 1}} will use the packaged lock file if
31-
the `--locked` flag is used.
30+
generated unless the `--exclude-lockfile` flag is used. {{man "cargo-install" 1}}
31+
will use the packaged lock file if the `--locked` flag is used.
3232
- A `.cargo_vcs_info.json` file is included that contains information
3333
about the current VCS checkout hash if available, as well as a flag if the
3434
worktree is dirty.
@@ -98,6 +98,14 @@ or the license).
9898
Allow working directories with uncommitted VCS changes to be packaged.
9999
{{/option}}
100100

101+
{{#option "`--exclude-lockfile`" }}
102+
Don't include the lock file when packaging.
103+
104+
This flag is not for general use.
105+
Some tools may expect a lock file to be present (e.g. `cargo install --locked`).
106+
Consider other options before using this.
107+
{{/option}}
108+
101109
{{> options-index }}
102110

103111
{{#option "`--registry` _registry_"}}

src/doc/man/generated_txt/cargo-package.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ DESCRIPTION
2727
manifest.
2828

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

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

100+
--exclude-lockfile
101+
Don’t include the lock file when packaging.
102+
103+
This flag is not for general use. Some tools may expect a lock file
104+
to be present (e.g. cargo install --locked). Consider other options
105+
before using this.
106+
99107
--index index
100108
The URL of the registry index to use.
101109

src/doc/src/commands/cargo-package.md

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

9696

97+
<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>
98+
<dd class="option-desc">Don’t include the lock file when packaging.</p>
99+
<p>This flag is not for general use.
100+
Some tools may expect a lock file to be present (e.g. <code>cargo install --locked</code>).
101+
Consider other options before using this.</dd>
102+
103+
97104
<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>
98105
<dd class="option-desc">The URL of the registry index to use.</dd>
99106

src/etc/man/cargo-package.1

+11-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ manifest.
3636
.sp
3737
.RS 4
3838
\h'-04'\(bu\h'+03'\fBCargo.lock\fR is always included. When missing, a new lock file will be
39-
generated. \fBcargo\-install\fR(1) will use the packaged lock file if
40-
the \fB\-\-locked\fR flag is used.
39+
generated unless the \fB\-\-exclude\-lockfile\fR flag is used. \fBcargo\-install\fR(1)
40+
will use the packaged lock file if the \fB\-\-locked\fR flag is used.
4141
.RE
4242
.sp
4343
.RS 4
@@ -127,6 +127,15 @@ or the license).
127127
Allow working directories with uncommitted VCS changes to be packaged.
128128
.RE
129129
.sp
130+
\fB\-\-exclude\-lockfile\fR
131+
.RS 4
132+
Don\[cq]t include the lock file when packaging.
133+
.sp
134+
This flag is not for general use.
135+
Some tools may expect a lock file to be present (e.g. \fBcargo install \-\-locked\fR).
136+
Consider other options before using this.
137+
.RE
138+
.sp
130139
\fB\-\-index\fR \fIindex\fR
131140
.RS 4
132141
The URL of the registry index to use.

tests/testsuite/cargo_package/help/stdout.term.svg

+36-34
Loading

0 commit comments

Comments
 (0)