Skip to content

Commit f42ed7a

Browse files
borsehuss
authored andcommitted
Auto merge of #8175 - ehuss:allow-package-list, r=alexcrichton
Allow `cargo package --list` even for things that don't package. `cargo package --list` was changed in #7905 to generate `Cargo.lock` earlier. If there is a problem, then it would fail where previously it would succeed. This changes it so that file generation is deferred until after `--list`. This also changes it so that the "dependencies must have a version" check is deferred until after `--list` as well. Closes #8151
1 parent d97ae45 commit f42ed7a

File tree

4 files changed

+86
-20
lines changed

4 files changed

+86
-20
lines changed

src/cargo/ops/cargo_package.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,17 @@ struct ArchiveFile {
5050
enum FileContents {
5151
/// Absolute path to the file on disk to add to the archive.
5252
OnDisk(PathBuf),
53-
/// Contents of a file generated in memory.
54-
Generated(String),
53+
/// Generates a file.
54+
Generated(GeneratedFile),
55+
}
56+
57+
enum GeneratedFile {
58+
/// Generates `Cargo.toml` by rewriting the original.
59+
Manifest,
60+
/// Generates `Cargo.lock` in some cases (like if there is a binary).
61+
Lockfile,
62+
/// Adds a `.cargo-vcs_info.json` file if in a (clean) git repo.
63+
VcsInfo(String),
5564
}
5665

5766
pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option<FileLock>> {
@@ -71,8 +80,6 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
7180
check_metadata(pkg, config)?;
7281
}
7382

74-
verify_dependencies(pkg)?;
75-
7683
if !pkg.manifest().exclude().is_empty() && !pkg.manifest().include().is_empty() {
7784
config.shell().warn(
7885
"both package.include and package.exclude are specified; \
@@ -100,6 +107,8 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
100107
return Ok(None);
101108
}
102109

110+
verify_dependencies(pkg)?;
111+
103112
let filename = format!("{}-{}.crate", pkg.name(), pkg.version());
104113
let dir = ws.target_dir().join("package");
105114
let mut dst = {
@@ -156,11 +165,10 @@ fn build_ar_list(
156165
rel_str: "Cargo.toml.orig".to_string(),
157166
contents: FileContents::OnDisk(src_file),
158167
});
159-
let generated = pkg.to_registry_toml(ws.config())?;
160168
result.push(ArchiveFile {
161169
rel_path,
162170
rel_str,
163-
contents: FileContents::Generated(generated),
171+
contents: FileContents::Generated(GeneratedFile::Manifest),
164172
});
165173
}
166174
"Cargo.lock" => continue,
@@ -179,18 +187,17 @@ fn build_ar_list(
179187
}
180188
}
181189
if pkg.include_lockfile() {
182-
let new_lock = build_lock(ws)?;
183190
result.push(ArchiveFile {
184191
rel_path: PathBuf::from("Cargo.lock"),
185192
rel_str: "Cargo.lock".to_string(),
186-
contents: FileContents::Generated(new_lock),
193+
contents: FileContents::Generated(GeneratedFile::Lockfile),
187194
});
188195
}
189196
if let Some(vcs_info) = vcs_info {
190197
result.push(ArchiveFile {
191198
rel_path: PathBuf::from(VCS_INFO_FILE),
192199
rel_str: VCS_INFO_FILE.to_string(),
193-
contents: FileContents::Generated(vcs_info),
200+
contents: FileContents::Generated(GeneratedFile::VcsInfo(vcs_info)),
194201
});
195202
}
196203
if let Some(license_file) = &pkg.manifest().metadata().license_file {
@@ -530,7 +537,12 @@ fn tar(
530537
format!("could not archive source file `{}`", disk_path.display())
531538
})?;
532539
}
533-
FileContents::Generated(contents) => {
540+
FileContents::Generated(generated_kind) => {
541+
let contents = match generated_kind {
542+
GeneratedFile::Manifest => pkg.to_registry_toml(ws.config())?,
543+
GeneratedFile::Lockfile => build_lock(ws)?,
544+
GeneratedFile::VcsInfo(s) => s,
545+
};
534546
header.set_entry_type(EntryType::file());
535547
header.set_mode(0o644);
536548
header.set_mtime(

tests/testsuite/package.rs

+52-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::io::prelude::*;
55
use std::path::Path;
66

77
use cargo_test_support::paths::CargoPathExt;
8-
use cargo_test_support::registry::Package;
8+
use cargo_test_support::publish::validate_crate_contents;
9+
use cargo_test_support::registry::{self, Package};
910
use cargo_test_support::{
10-
basic_manifest, cargo_process, git, path2url, paths, project, publish::validate_crate_contents,
11-
registry, symlink_supported, t,
11+
basic_manifest, cargo_process, git, path2url, paths, project, symlink_supported, t,
1212
};
1313

1414
#[cargo_test]
@@ -1697,3 +1697,52 @@ fn package_restricted_windows() {
16971697
)
16981698
.run();
16991699
}
1700+
1701+
#[cargo_test]
1702+
fn list_with_path_and_lock() {
1703+
// Allow --list even for something that isn't packageable.
1704+
1705+
// Init an empty registry because a versionless path dep will search for
1706+
// the package on crates.io.
1707+
registry::init();
1708+
let p = project()
1709+
.file(
1710+
"Cargo.toml",
1711+
r#"
1712+
[package]
1713+
name = "foo"
1714+
version = "0.1.0"
1715+
license = "MIT"
1716+
description = "foo"
1717+
homepage = "foo"
1718+
1719+
[dependencies]
1720+
bar = {path="bar"}
1721+
"#,
1722+
)
1723+
.file("src/main.rs", "fn main() {}")
1724+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
1725+
.file("bar/src/lib.rs", "")
1726+
.build();
1727+
1728+
p.cargo("package --list")
1729+
.with_stdout(
1730+
"\
1731+
Cargo.lock
1732+
Cargo.toml
1733+
Cargo.toml.orig
1734+
src/main.rs
1735+
",
1736+
)
1737+
.run();
1738+
1739+
p.cargo("package")
1740+
.with_status(101)
1741+
.with_stderr(
1742+
"\
1743+
error: all path dependencies must have a version specified when packaging.
1744+
dependency `bar` does not specify a version.
1745+
",
1746+
)
1747+
.run();
1748+
}

tests/testsuite/publish_lockfile.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ fn no_warn_workspace_extras() {
297297
.cwd("a")
298298
.with_stderr(
299299
"\
300-
[UPDATING] `[..]` index
301300
[PACKAGING] a v0.1.0 ([..])
301+
[UPDATING] `[..]` index
302302
",
303303
)
304304
.run();
@@ -328,10 +328,10 @@ fn warn_package_with_yanked() {
328328
p.cargo("package --no-verify")
329329
.with_stderr(
330330
"\
331+
[PACKAGING] foo v0.0.1 ([..])
331332
[UPDATING] `[..]` index
332333
[WARNING] package `bar v0.1.0` in Cargo.lock is yanked in registry \
333334
`crates.io`, consider updating to a version that is not yanked
334-
[PACKAGING] foo v0.0.1 ([..])
335335
",
336336
)
337337
.run();

tests/testsuite/registry.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,18 @@ fn package_with_path_deps() {
361361
.file("notyet/src/lib.rs", "")
362362
.build();
363363

364-
p.cargo("package -v")
364+
p.cargo("package")
365365
.with_status(101)
366366
.with_stderr_contains(
367367
"\
368-
[ERROR] no matching package named `notyet` found
369-
location searched: registry [..]
370-
required by package `foo v0.0.1 ([..])`
368+
[PACKAGING] foo [..]
369+
[UPDATING] [..]
370+
[ERROR] failed to prepare local package for uploading
371+
372+
Caused by:
373+
no matching package named `notyet` found
374+
location searched: registry `https://github.com/rust-lang/crates.io-index`
375+
required by package `foo v0.0.1 [..]`
371376
",
372377
)
373378
.run();
@@ -377,8 +382,8 @@ required by package `foo v0.0.1 ([..])`
377382
p.cargo("package")
378383
.with_stderr(
379384
"\
380-
[UPDATING] `[..]` index
381385
[PACKAGING] foo v0.0.1 ([CWD])
386+
[UPDATING] `[..]` index
382387
[VERIFYING] foo v0.0.1 ([CWD])
383388
[DOWNLOADING] crates ...
384389
[DOWNLOADED] notyet v0.0.1 (registry `[ROOT][..]`)

0 commit comments

Comments
 (0)