From c0287bec8d42f89cc17cf305bcfdfb69719d64a1 Mon Sep 17 00:00:00 2001 From: Joe Neeman Date: Thu, 13 Jun 2024 12:57:18 -0500 Subject: [PATCH] Change verification order during packaging. Once we support packaging workspaces with dependencies, dependency packages need to be built before anything is verified. In addition to a little refactoring, this commit reorders the console messages so that package metadata (archive size, etc.) is reported before verification results. Co-Authored-By: Tor Hovland <55164+torhovland@users.noreply.github.com> --- src/cargo/ops/cargo_package.rs | 131 ++++++++++-------- src/cargo/ops/registry/publish.rs | 3 +- tests/testsuite/alt_registry.rs | 6 +- tests/testsuite/cargo_features.rs | 2 +- tests/testsuite/cross_publish.rs | 4 +- tests/testsuite/features_namespaced.rs | 2 +- .../testsuite/inheritable_workspace_fields.rs | 10 +- tests/testsuite/package.rs | 84 +++++------ tests/testsuite/publish.rs | 26 ++-- tests/testsuite/publish_lockfile.rs | 12 +- tests/testsuite/registry.rs | 2 +- tests/testsuite/source_replacement.rs | 2 +- tests/testsuite/weak_dep_features.rs | 2 +- 13 files changed, 152 insertions(+), 134 deletions(-) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index c9fc22c7930..30731de20aa 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -28,6 +28,7 @@ use tar::{Archive, Builder, EntryType, Header, HeaderMode}; use tracing::debug; use unicase::Ascii as UncasedAscii; +#[derive(Clone)] pub struct PackageOpts<'gctx> { pub gctx: &'gctx GlobalContext, pub list: bool, @@ -82,48 +83,39 @@ struct GitVcsInfo { sha1: String, } +/// Packages a single package in a workspace, returning the resulting tar file. +/// +/// # Panics +/// Panics if `opts.list` is true. In that case you probably don't want to +/// actually build the package tarball; you should just make and print the list +/// of files. (We don't currently provide a public API for that, but see how +/// [`package`] does it.) pub fn package_one( ws: &Workspace<'_>, pkg: &Package, opts: &PackageOpts<'_>, -) -> CargoResult> { - let gctx = ws.gctx(); - let mut src = PathSource::new(pkg.root(), pkg.package_id().source_id(), gctx); - src.update()?; +) -> CargoResult { + assert!(!opts.list); - if opts.check_metadata { - check_metadata(pkg, gctx)?; - } + let ar_files = prepare_archive(ws, pkg, opts)?; + let tarball = create_package(ws, pkg, ar_files)?; - if !pkg.manifest().exclude().is_empty() && !pkg.manifest().include().is_empty() { - gctx.shell().warn( - "both package.include and package.exclude are specified; \ - the exclude list will be ignored", - )?; + if opts.verify { + run_verify(ws, pkg, &tarball, opts)?; } - let src_files = src.list_files(pkg)?; - // Check (git) repository state, getting the current commit hash if not - // dirty. - let vcs_info = if !opts.allow_dirty { - // This will error if a dirty repo is found. - check_repo_state(pkg, &src_files, gctx)? - } else { - None - }; - - let ar_files = build_ar_list(ws, pkg, src_files, vcs_info)?; + Ok(tarball) +} +// Builds a tarball and places it in the output directory. +fn create_package( + ws: &Workspace<'_>, + pkg: &Package, + ar_files: Vec, +) -> CargoResult { + let gctx = ws.gctx(); let filecount = ar_files.len(); - if opts.list { - for ar_file in ar_files { - drop_println!(gctx, "{}", ar_file.rel_str); - } - - return Ok(None); - } - // Check that the package dependencies are safe to deploy. for dep in pkg.dependencies() { super::check_dep_has_version(dep, false)?; @@ -145,10 +137,6 @@ pub fn package_one( dst.file().set_len(0)?; let uncompressed_size = tar(ws, pkg, ar_files, dst.file(), &filename) .with_context(|| "failed to prepare local package for uploading")?; - if opts.verify { - dst.seek(SeekFrom::Start(0))?; - run_verify(ws, pkg, &dst, opts).with_context(|| "failed to verify package tarball")? - } dst.seek(SeekFrom::Start(0))?; let src_path = dst.path(); @@ -172,7 +160,7 @@ pub fn package_one( // It doesn't really matter if this fails. drop(gctx.shell().status("Packaged", message)); - return Ok(Some(dst)); + return Ok(dst); } pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult>> { @@ -185,7 +173,6 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult