Skip to content

Commit 1a104b5

Browse files
committed
Add --no-gen-lockfile flag to package command
When `no-gen-lockfile` is enabled, `package` will not verify the lock file if present, nor will it generate a new one if absent. Together with `no-verify`, this flag decouples packaging from the registry.
1 parent 2928e32 commit 1a104b5

File tree

7 files changed

+81
-23
lines changed

7 files changed

+81
-23
lines changed

src/bin/cargo/commands/package.rs

Lines changed: 5 additions & 0 deletions
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+
"no-gen-lockfile",
31+
"Do not regenerate or create the lock file",
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+
gen_lockfile: !args.flag("no-gen-lockfile"),
8287
to_package: specs,
8388
targets: args.targets()?,
8489
jobs: args.jobs()?,

src/cargo/ops/cargo_package/mod.rs

Lines changed: 50 additions & 15 deletions
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 gen_lockfile: bool,
4950
pub verify: bool,
5051
pub jobs: Option<JobsConfig>,
5152
pub keep_going: bool,
@@ -91,6 +92,7 @@ fn create_package(
9192
pkg: &Package,
9293
ar_files: Vec<ArchiveFile>,
9394
local_reg: Option<&TmpRegistry<'_>>,
95+
gen_lockfile: bool,
9496
) -> CargoResult<FileLock> {
9597
let gctx = ws.gctx();
9698
let filecount = ar_files.len();
@@ -114,8 +116,16 @@ fn create_package(
114116
gctx.shell()
115117
.status("Packaging", pkg.package_id().to_string())?;
116118
dst.file().set_len(0)?;
117-
let uncompressed_size = tar(ws, pkg, local_reg, ar_files, dst.file(), &filename)
118-
.context("failed to prepare local package for uploading")?;
119+
let uncompressed_size = tar(
120+
ws,
121+
pkg,
122+
local_reg,
123+
ar_files,
124+
dst.file(),
125+
&filename,
126+
gen_lockfile,
127+
)
128+
.context("failed to prepare local package for uploading")?;
119129

120130
dst.seek(SeekFrom::Start(0))?;
121131
let src_path = dst.path();
@@ -194,6 +204,7 @@ fn do_package<'a>(
194204
.as_path_unlocked()
195205
.join(LOCKFILE_NAME)
196206
.exists()
207+
&& opts.gen_lockfile
197208
{
198209
// Make sure the Cargo.lock is up-to-date and valid.
199210
let dry_run = false;
@@ -241,7 +252,8 @@ fn do_package<'a>(
241252
drop_println!(ws.gctx(), "{}", ar_file.rel_str);
242253
}
243254
} else {
244-
let tarball = create_package(ws, &pkg, ar_files, local_reg.as_ref())?;
255+
let tarball =
256+
create_package(ws, &pkg, ar_files, local_reg.as_ref(), opts.gen_lockfile)?;
245257
if let Some(local_reg) = local_reg.as_mut() {
246258
if pkg.publish() != &Some(Vec::new()) {
247259
local_reg.add_package(ws, &pkg, &tarball)?;
@@ -389,7 +401,7 @@ fn prepare_archive(
389401
// Check (git) repository state, getting the current commit hash.
390402
let vcs_info = vcs::check_repo_state(pkg, &src_files, gctx, &opts)?;
391403

392-
build_ar_list(ws, pkg, src_files, vcs_info)
404+
build_ar_list(ws, pkg, src_files, vcs_info, opts.gen_lockfile)
393405
}
394406

395407
/// Builds list of files to archive.
@@ -399,6 +411,7 @@ fn build_ar_list(
399411
pkg: &Package,
400412
src_files: Vec<PathEntry>,
401413
vcs_info: Option<vcs::VcsInfo>,
414+
gen_lockfile: bool,
402415
) -> CargoResult<Vec<ArchiveFile>> {
403416
let mut result = HashMap::new();
404417
let root = pkg.root();
@@ -409,7 +422,20 @@ fn build_ar_list(
409422
anyhow::format_err!("non-utf8 path in source directory: {}", rel_path.display())
410423
})?;
411424
match rel_str {
412-
"Cargo.lock" => continue,
425+
"Cargo.lock" => {
426+
if gen_lockfile {
427+
continue;
428+
} else {
429+
result
430+
.entry(UncasedAscii::new(rel_str))
431+
.or_insert_with(Vec::new)
432+
.push(ArchiveFile {
433+
rel_path: rel_path.to_owned(),
434+
rel_str: rel_str.to_owned(),
435+
contents: FileContents::OnDisk(src_file.to_path_buf()),
436+
});
437+
}
438+
}
413439
VCS_INFO_FILE | ORIGINAL_MANIFEST_FILE => anyhow::bail!(
414440
"invalid inclusion of reserved file name {} in package source",
415441
rel_str
@@ -453,15 +479,17 @@ fn build_ar_list(
453479
))?;
454480
}
455481

456-
let rel_str = "Cargo.lock";
457-
result
458-
.entry(UncasedAscii::new(rel_str))
459-
.or_insert_with(Vec::new)
460-
.push(ArchiveFile {
461-
rel_path: PathBuf::from(rel_str),
462-
rel_str: rel_str.to_string(),
463-
contents: FileContents::Generated(GeneratedFile::Lockfile),
464-
});
482+
if gen_lockfile {
483+
let rel_str = "Cargo.lock";
484+
result
485+
.entry(UncasedAscii::new(rel_str))
486+
.or_insert_with(Vec::new)
487+
.push(ArchiveFile {
488+
rel_path: PathBuf::from(rel_str),
489+
rel_str: rel_str.to_string(),
490+
contents: FileContents::Generated(GeneratedFile::Lockfile),
491+
});
492+
}
465493

466494
if let Some(vcs_info) = vcs_info {
467495
let rel_str = VCS_INFO_FILE;
@@ -729,6 +757,7 @@ fn tar(
729757
ar_files: Vec<ArchiveFile>,
730758
dst: &File,
731759
filename: &str,
760+
gen_lockfile: bool,
732761
) -> CargoResult<u64> {
733762
// Prepare the encoder and its header.
734763
let filename = Path::new(filename);
@@ -779,7 +808,13 @@ fn tar(
779808
FileContents::Generated(generated_kind) => {
780809
let contents = match generated_kind {
781810
GeneratedFile::Manifest => publish_pkg.manifest().to_normalized_contents()?,
782-
GeneratedFile::Lockfile => build_lock(ws, &publish_pkg, local_reg)?,
811+
GeneratedFile::Lockfile => {
812+
if gen_lockfile {
813+
build_lock(ws, &publish_pkg, local_reg)?
814+
} else {
815+
continue;
816+
}
817+
}
783818
GeneratedFile::VcsInfo(ref s) => serde_json::to_string_pretty(s)?,
784819
};
785820
header.set_entry_type(EntryType::file());

src/cargo/ops/registry/publish.rs

Lines changed: 1 addition & 0 deletions
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+
gen_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

Lines changed: 6 additions & 2 deletions
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 `--no-gen-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,10 @@ or the license).
9898
Allow working directories with uncommitted VCS changes to be packaged.
9999
{{/option}}
100100

101+
{{#option "`--no-gen-lockfile`" }}
102+
Do not regenerate or create the lock file.
103+
{{/option}}
104+
101105
{{> options-index }}
102106

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

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

Lines changed: 6 additions & 2 deletions
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 --no-gen-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,9 @@ OPTIONS
9697
Allow working directories with uncommitted VCS changes to be
9798
packaged.
9899

100+
--no-gen-lockfile
101+
Do not regenerate or create the lock file.
102+
99103
--index index
100104
The URL of the registry index to use.
101105

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

Lines changed: 6 additions & 2 deletions
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 `--no-gen-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,10 @@ 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---no-gen-lockfile"><a class="option-anchor" href="#option-cargo-package---no-gen-lockfile"></a><code>--no-gen-lockfile</code></dt>
98+
<dd class="option-desc">Do not regenerate or create the lock file.</dd>
99+
100+
97101
<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>
98102
<dd class="option-desc">The URL of the registry index to use.</dd>
99103

src/etc/man/cargo-package.1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ manifest.
3636
.sp
3737
.RS 4
3838
\h'-04'\(bu\h'+02'\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\-\-no\-gen\-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,11 @@ or the license).
127127
Allow working directories with uncommitted VCS changes to be packaged.
128128
.RE
129129
.sp
130+
\fB\-\-no\-gen\-lockfile\fR
131+
.RS 4
132+
Do not regenerate or create the lock file.
133+
.RE
134+
.sp
130135
\fB\-\-index\fR \fIindex\fR
131136
.RS 4
132137
The URL of the registry index to use.

0 commit comments

Comments
 (0)