Skip to content

Commit a59aba1

Browse files
committed
Auto merge of #13666 - epage:refactor-package, r=weihanglo
refactor(package): Simplify getting of published Manifest ### What does this PR try to resolve? This is a parallel effort to #13664 in an effort to #13456 This abstracts away the logic for getting the published version of `Cargo.toml` so we can more easily change the APIs that were previously exposed ### How should we test and review this PR? ### Additional information
2 parents 97ed4ff + ca706a4 commit a59aba1

12 files changed

+72
-75
lines changed

src/cargo/core/manifest.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ use crate::util::errors::*;
2222
use crate::util::interning::InternedString;
2323
use crate::util::{short_hash, Filesystem, GlobalContext};
2424

25+
pub const MANIFEST_PREAMBLE: &str = "\
26+
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
27+
#
28+
# When uploading crates to the registry Cargo will automatically
29+
# \"normalize\" Cargo.toml files for maximal compatibility
30+
# with all versions of Cargo and also rewrite `path` dependencies
31+
# to registry (e.g., crates.io) dependencies.
32+
#
33+
# If you are reading this file be aware that the original Cargo.toml
34+
# will likely look very different (and much more reasonable).
35+
# See Cargo.toml.orig for the original contents.
36+
";
37+
2538
pub enum EitherManifest {
2639
Real(Manifest),
2740
Virtual(VirtualManifest),
@@ -470,6 +483,10 @@ impl Manifest {
470483
pub fn contents(&self) -> &str {
471484
self.contents.as_str()
472485
}
486+
pub fn to_resolved_contents(&self) -> CargoResult<String> {
487+
let toml = toml::to_string_pretty(self.resolved_toml())?;
488+
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
489+
}
473490
/// Collection of spans for the original TOML
474491
pub fn document(&self) -> &toml_edit::ImDocument<String> {
475492
&self.document

src/cargo/core/package.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,8 @@ use crate::util::network::http::http_handle_and_timeout;
3232
use crate::util::network::http::HttpTimeout;
3333
use crate::util::network::retry::{Retry, RetryResult};
3434
use crate::util::network::sleep::SleepTracker;
35-
use crate::util::toml::prepare_for_publish;
3635
use crate::util::{self, internal, GlobalContext, Progress, ProgressStyle};
3736

38-
pub const MANIFEST_PREAMBLE: &str = "\
39-
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
40-
#
41-
# When uploading crates to the registry Cargo will automatically
42-
# \"normalize\" Cargo.toml files for maximal compatibility
43-
# with all versions of Cargo and also rewrite `path` dependencies
44-
# to registry (e.g., crates.io) dependencies.
45-
#
46-
# If you are reading this file be aware that the original Cargo.toml
47-
# will likely look very different (and much more reasonable).
48-
# See Cargo.toml.orig for the original contents.
49-
";
50-
5137
/// Information about a package that is available somewhere in the file system.
5238
///
5339
/// A package is a `Cargo.toml` file plus all the files that are part of it.
@@ -197,12 +183,6 @@ impl Package {
197183
}
198184
}
199185

200-
pub fn to_registry_toml(&self, ws: &Workspace<'_>) -> CargoResult<String> {
201-
let manifest = prepare_for_publish(self.manifest().resolved_toml(), ws, self.root())?;
202-
let toml = toml::to_string_pretty(&manifest)?;
203-
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
204-
}
205-
206186
/// Returns if package should include `Cargo.lock`.
207187
pub fn include_lockfile(&self) -> bool {
208188
self.targets().iter().any(|t| t.is_example() || t.is_bin())

src/cargo/ops/cargo_package.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::sources::PathSource;
1616
use crate::util::cache_lock::CacheLockMode;
1717
use crate::util::context::JobsConfig;
1818
use crate::util::errors::CargoResult;
19-
use crate::util::toml::{prepare_for_publish, to_real_manifest};
19+
use crate::util::toml::prepare_for_publish;
2020
use crate::util::{self, human_readable_bytes, restricted_names, FileLock, GlobalContext};
2121
use crate::{drop_println, ops};
2222
use anyhow::Context as _;
@@ -448,32 +448,11 @@ fn error_custom_build_file_not_in_package(
448448
}
449449

450450
/// Construct `Cargo.lock` for the package to be published.
451-
fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
451+
fn build_lock(ws: &Workspace<'_>, publish_pkg: &Package) -> CargoResult<String> {
452452
let gctx = ws.gctx();
453453
let orig_resolve = ops::load_pkg_lockfile(ws)?;
454454

455-
// Convert Package -> TomlManifest -> Manifest -> Package
456-
let contents = orig_pkg.manifest().contents();
457-
let document = orig_pkg.manifest().document();
458-
let toml_manifest =
459-
prepare_for_publish(orig_pkg.manifest().resolved_toml(), ws, orig_pkg.root())?;
460-
let source_id = orig_pkg.package_id().source_id();
461-
let mut warnings = Default::default();
462-
let mut errors = Default::default();
463-
let manifest = to_real_manifest(
464-
contents.to_owned(),
465-
document.clone(),
466-
toml_manifest,
467-
source_id,
468-
orig_pkg.manifest_path(),
469-
gctx,
470-
&mut warnings,
471-
&mut errors,
472-
)?;
473-
let new_pkg = Package::new(manifest, orig_pkg.manifest_path());
474-
475-
// Regenerate Cargo.lock using the old one as a guide.
476-
let tmp_ws = Workspace::ephemeral(new_pkg, ws.gctx(), None, true)?;
455+
let tmp_ws = Workspace::ephemeral(publish_pkg.clone(), ws.gctx(), None, true)?;
477456
let mut tmp_reg = PackageRegistry::new(ws.gctx())?;
478457
let mut new_resolve = ops::resolve_with_previous(
479458
&mut tmp_reg,
@@ -704,6 +683,7 @@ fn tar(
704683

705684
let base_name = format!("{}-{}", pkg.name(), pkg.version());
706685
let base_path = Path::new(&base_name);
686+
let publish_pkg = prepare_for_publish(pkg, ws)?;
707687

708688
let mut uncompressed_size = 0;
709689
for ar_file in ar_files {
@@ -734,8 +714,8 @@ fn tar(
734714
}
735715
FileContents::Generated(generated_kind) => {
736716
let contents = match generated_kind {
737-
GeneratedFile::Manifest => pkg.to_registry_toml(ws)?,
738-
GeneratedFile::Lockfile => build_lock(ws, pkg)?,
717+
GeneratedFile::Manifest => publish_pkg.manifest().to_resolved_contents()?,
718+
GeneratedFile::Lockfile => build_lock(ws, &publish_pkg)?,
739719
GeneratedFile::VcsInfo(ref s) => serde_json::to_string_pretty(s)?,
740720
};
741721
header.set_entry_type(EntryType::file());

src/cargo/ops/vendor.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::core::package::MANIFEST_PREAMBLE;
21
use crate::core::shell::Verbosity;
32
use crate::core::{GitReference, Package, Workspace};
43
use crate::ops;
@@ -360,8 +359,7 @@ fn cp_sources(
360359
let cksum = if dst.file_name() == Some(OsStr::new("Cargo.toml"))
361360
&& pkg.package_id().source_id().is_git()
362361
{
363-
let original_toml = toml::to_string_pretty(pkg.manifest().resolved_toml())?;
364-
let contents = format!("{}\n{}", MANIFEST_PREAMBLE, original_toml);
362+
let contents = pkg.manifest().to_resolved_contents()?;
365363
copy_and_checksum(
366364
&dst,
367365
&mut dst_opts,

src/cargo/util/toml/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::core::dependency::{Artifact, ArtifactTarget, DepKind};
2121
use crate::core::manifest::{ManifestMetadata, TargetSourcePath};
2222
use crate::core::resolver::ResolveBehavior;
2323
use crate::core::{find_workspace_root, resolve_relative_path, CliUnstable, FeatureValue};
24-
use crate::core::{Dependency, Manifest, PackageId, Summary, Target};
24+
use crate::core::{Dependency, Manifest, Package, PackageId, Summary, Target};
2525
use crate::core::{Edition, EitherManifest, Feature, Features, VirtualManifest, Workspace};
2626
use crate::core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, WorkspaceRootConfig};
2727
use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY};
@@ -48,8 +48,8 @@ pub fn read_manifest(
4848
source_id: SourceId,
4949
gctx: &GlobalContext,
5050
) -> CargoResult<EitherManifest> {
51-
let mut warnings = vec![];
52-
let mut errors = vec![];
51+
let mut warnings = Default::default();
52+
let mut errors = Default::default();
5353

5454
let contents =
5555
read_toml_string(path, gctx).map_err(|err| ManifestError::new(err, path.into()))?;
@@ -218,10 +218,32 @@ fn warn_on_unused(unused: &BTreeSet<String>, warnings: &mut Vec<String>) {
218218
}
219219
}
220220

221+
pub fn prepare_for_publish(me: &Package, ws: &Workspace<'_>) -> CargoResult<Package> {
222+
let contents = me.manifest().contents();
223+
let document = me.manifest().document();
224+
let toml_manifest = prepare_toml_for_publish(me.manifest().resolved_toml(), ws, me.root())?;
225+
let source_id = me.package_id().source_id();
226+
let mut warnings = Default::default();
227+
let mut errors = Default::default();
228+
let gctx = ws.gctx();
229+
let manifest = to_real_manifest(
230+
contents.to_owned(),
231+
document.clone(),
232+
toml_manifest,
233+
source_id,
234+
me.manifest_path(),
235+
gctx,
236+
&mut warnings,
237+
&mut errors,
238+
)?;
239+
let new_pkg = Package::new(manifest, me.manifest_path());
240+
Ok(new_pkg)
241+
}
242+
221243
/// Prepares the manifest for publishing.
222244
// - Path and git components of dependency specifications are removed.
223245
// - License path is updated to point within the package.
224-
pub fn prepare_for_publish(
246+
fn prepare_toml_for_publish(
225247
me: &manifest::TomlManifest,
226248
ws: &Workspace<'_>,
227249
package_root: &Path,

tests/testsuite/artifact_dep.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ artifact = [
21842184
"staticlib",
21852185
]
21862186
target = "target""#,
2187-
cargo::core::package::MANIFEST_PREAMBLE
2187+
cargo::core::manifest::MANIFEST_PREAMBLE
21882188
),
21892189
)],
21902190
);

tests/testsuite/features2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ homepage = "https://example.com/"
17081708
license = "MIT"
17091709
resolver = "2"
17101710
"#,
1711-
cargo::core::package::MANIFEST_PREAMBLE
1711+
cargo::core::manifest::MANIFEST_PREAMBLE
17121712
);
17131713

17141714
let f = File::open(&p.root().join("target/package/a-0.1.0.crate")).unwrap();

tests/testsuite/features_namespaced.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ optional = true
10001000
[features]
10011001
feat = ["opt-dep1"]
10021002
"#,
1003-
cargo::core::package::MANIFEST_PREAMBLE
1003+
cargo::core::manifest::MANIFEST_PREAMBLE
10041004
),
10051005
)],
10061006
);
@@ -1116,7 +1116,7 @@ feat1 = []
11161116
feat2 = ["dep:bar"]
11171117
feat3 = ["feat2"]
11181118
"#,
1119-
cargo::core::package::MANIFEST_PREAMBLE
1119+
cargo::core::manifest::MANIFEST_PREAMBLE
11201120
),
11211121
)],
11221122
);

tests/testsuite/inheritable_workspace_fields.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ repository = "https://github.com/example/example"
245245
branch = "master"
246246
repository = "https://gitlab.com/rust-lang/rust"
247247
"#,
248-
cargo::core::package::MANIFEST_PREAMBLE
248+
cargo::core::manifest::MANIFEST_PREAMBLE
249249
),
250250
)],
251251
);
@@ -406,7 +406,7 @@ version = "0.5.2"
406406
[build-dependencies.dep-build]
407407
version = "0.8"
408408
"#,
409-
cargo::core::package::MANIFEST_PREAMBLE
409+
cargo::core::manifest::MANIFEST_PREAMBLE
410410
),
411411
)],
412412
);
@@ -532,7 +532,7 @@ authors = []
532532
version = "0.1.2"
533533
features = ["testing"]
534534
"#,
535-
cargo::core::package::MANIFEST_PREAMBLE
535+
cargo::core::manifest::MANIFEST_PREAMBLE
536536
),
537537
)],
538538
);
@@ -796,7 +796,7 @@ repository = "https://github.com/example/example"
796796
branch = "master"
797797
repository = "https://gitlab.com/rust-lang/rust"
798798
"#,
799-
cargo::core::package::MANIFEST_PREAMBLE
799+
cargo::core::manifest::MANIFEST_PREAMBLE
800800
),
801801
)],
802802
);
@@ -959,7 +959,7 @@ version = "0.5.2"
959959
[build-dependencies.dep-build]
960960
version = "0.8"
961961
"#,
962-
cargo::core::package::MANIFEST_PREAMBLE
962+
cargo::core::manifest::MANIFEST_PREAMBLE
963963
),
964964
)],
965965
);

tests/testsuite/package.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ registry-index = "{}"
12371237
[dependencies.ghi]
12381238
version = "1.0"
12391239
"#,
1240-
cargo::core::package::MANIFEST_PREAMBLE,
1240+
cargo::core::manifest::MANIFEST_PREAMBLE,
12411241
registry.index_url()
12421242
);
12431243

@@ -1294,7 +1294,7 @@ name = "bar"
12941294
version = "0.1.0"
12951295
authors = []
12961296
"#,
1297-
cargo::core::package::MANIFEST_PREAMBLE
1297+
cargo::core::manifest::MANIFEST_PREAMBLE
12981298
);
12991299
validate_crate_contents(
13001300
f,
@@ -1367,7 +1367,7 @@ version = "1.0.0"
13671367
[target.{host}.dependencies.baz]
13681368
version = "1.0.0"
13691369
"#,
1370-
cargo::core::package::MANIFEST_PREAMBLE,
1370+
cargo::core::manifest::MANIFEST_PREAMBLE,
13711371
host = rustc_host()
13721372
);
13731373
verify(&p, "package", rewritten_toml);
@@ -1387,7 +1387,7 @@ public = true
13871387
version = "1.0.0"
13881388
public = true
13891389
"#,
1390-
cargo::core::package::MANIFEST_PREAMBLE,
1390+
cargo::core::manifest::MANIFEST_PREAMBLE,
13911391
host = rustc_host()
13921392
);
13931393
verify(&p, "package -Zpublic-dependency", rewritten_toml);
@@ -2808,7 +2808,7 @@ name = "bar"
28082808
version = "0.1.0"
28092809
resolver = "1"
28102810
"#,
2811-
cargo::core::package::MANIFEST_PREAMBLE
2811+
cargo::core::manifest::MANIFEST_PREAMBLE
28122812
);
28132813
validate_crate_contents(
28142814
f,
@@ -2826,7 +2826,7 @@ edition = "2015"
28262826
name = "baz"
28272827
version = "0.1.0"
28282828
"#,
2829-
cargo::core::package::MANIFEST_PREAMBLE
2829+
cargo::core::manifest::MANIFEST_PREAMBLE
28302830
);
28312831
validate_crate_contents(
28322832
f,
@@ -2891,7 +2891,7 @@ description = "foo"
28912891
homepage = "https://example.com/"
28922892
license = "MIT"
28932893
"#,
2894-
cargo::core::package::MANIFEST_PREAMBLE
2894+
cargo::core::manifest::MANIFEST_PREAMBLE
28952895
);
28962896
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
28972897
# It is not intended for manual editing.
@@ -2985,7 +2985,7 @@ description = "foo"
29852985
documentation = "https://example.com/"
29862986
license = "MIT"
29872987
"#,
2988-
cargo::core::package::MANIFEST_PREAMBLE
2988+
cargo::core::manifest::MANIFEST_PREAMBLE
29892989
);
29902990
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
29912991
# It is not intended for manual editing.
@@ -3092,7 +3092,7 @@ description = "foo"
30923092
homepage = "https://example.com/"
30933093
license = "MIT"
30943094
"#,
3095-
cargo::core::package::MANIFEST_PREAMBLE
3095+
cargo::core::manifest::MANIFEST_PREAMBLE
30963096
);
30973097
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
30983098
# It is not intended for manual editing.

tests/testsuite/publish.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ You may press ctrl-c [..]
15661566
[dependencies.dep1]\n\
15671567
version = \"1.0\"\n\
15681568
",
1569-
cargo::core::package::MANIFEST_PREAMBLE
1569+
cargo::core::manifest::MANIFEST_PREAMBLE
15701570
),
15711571
),
15721572
(
@@ -1680,7 +1680,7 @@ repository = "foo"
16801680
16811681
[dev-dependencies]
16821682
"#,
1683-
cargo::core::package::MANIFEST_PREAMBLE
1683+
cargo::core::manifest::MANIFEST_PREAMBLE
16841684
),
16851685
)],
16861686
);
@@ -1979,7 +1979,7 @@ features = ["cat"]
19791979
version = "1.0"
19801980
features = ["cat"]
19811981
"#,
1982-
cargo::core::package::MANIFEST_PREAMBLE
1982+
cargo::core::manifest::MANIFEST_PREAMBLE
19831983
),
19841984
)],
19851985
);

tests/testsuite/weak_dep_features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ optional = true
642642
feat1 = []
643643
feat2 = ["bar?/feat"]
644644
"#,
645-
cargo::core::package::MANIFEST_PREAMBLE
645+
cargo::core::manifest::MANIFEST_PREAMBLE
646646
),
647647
)],
648648
);

0 commit comments

Comments
 (0)