Skip to content

Commit

Permalink
Fix packaging plugins when --no-verify is passed (#1643)
Browse files Browse the repository at this point in the history
Resolves the issue brought up in
#1605 (comment)
  • Loading branch information
DelevoXDG authored Oct 16, 2024
1 parent 06df46f commit 0ee3bc5
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dialoguer = "0.11.0"
directories = "5"
dunce = "1"
expect-test = "1.5"
flate2 = { version = "1.0.30", default-features = false, features = ["zlib"] }
fs4 = { version = "0.7", features = ["tokio"] }
fs_extra = "1"
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
Expand Down
1 change: 1 addition & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ which.workspace = true
windows-sys.workspace = true
zstd.workspace = true
cargo_metadata.workspace = true
flate2.workspace = true

[target.'cfg(not(target_os = "linux"))'.dependencies]
reqwest = { workspace = true, default-features = true }
Expand Down
32 changes: 32 additions & 0 deletions scarb/src/compiler/plugin/proc_macro/compilation.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::compiler::ProcMacroCompilationUnit;
use crate::core::{Config, Package, Workspace};
use crate::flock::Filesystem;
use crate::internal::fsx;
use crate::ops::PackageOpts;
use crate::process::exec_piping;
use crate::CARGO_MANIFEST_FILE_NAME;
use anyhow::{anyhow, Context, Result};
use camino::Utf8PathBuf;
use cargo_metadata::MetadataCommand;
use flate2::read::GzDecoder;
use indoc::formatdoc;
use libloading::library_filename;
use ra_ap_toolchain::Tool;
Expand All @@ -15,7 +17,10 @@ use serde::{Serialize, Serializer};
use serde_json::value::RawValue;
use std::fmt::Display;
use std::fs;
use std::io::{Seek, SeekFrom};
use std::ops::Deref;
use std::process::Command;
use tar::Archive;
use tracing::trace_span;

pub const PROC_MACRO_BUILD_PROFILE: &str = "release";
Expand Down Expand Up @@ -150,6 +155,32 @@ pub fn get_crate_archive_basename(package: &Package) -> Result<String> {
Ok(format!("{}-{}", package_name, package_version))
}

pub fn unpack_crate(package: &Package, config: &Config) -> Result<()> {
let archive_basename = get_crate_archive_basename(package)?;
let archive_name = format!("{}.crate", archive_basename);

let tar = package
.target_path(config)
.into_child("package")
.open_ro_exclusive(&archive_name, &archive_name, config)?;

// The following implementation has been copied from the `Cargo` codebase with slight modifications only.
// The original implementation can be found here:
// https://github.com/rust-lang/cargo/blob/a4600184b8d6619ed0b5a0a19946dbbe97e1d739/src/cargo/ops/cargo_package.rs#L1110

tar.deref().seek(SeekFrom::Start(0))?;
let f = GzDecoder::new(tar.deref());
let dst = tar.parent().unwrap().join(&archive_basename);
if dst.exists() {
fsx::remove_dir_all(&dst)?;
}
let mut archive = Archive::new(f);
archive.set_preserve_mtime(false); // Don't set modified time to avoid filesystem errors
archive.unpack(dst.parent().unwrap())?;

Ok(())
}

pub fn fetch_crate(package: &Package, ws: &Workspace<'_>) -> Result<()> {
run_cargo(CargoAction::Fetch, package, ws)
}
Expand Down Expand Up @@ -233,6 +264,7 @@ impl<'c> From<CargoCommand<'c>> for Command {
CargoAction::Package(ref opts) => {
cmd.arg("--target-dir");
cmd.arg(args.target_dir);
cmd.arg("--no-verify");
if !opts.check_metadata {
cmd.arg("--no-metadata");
}
Expand Down
29 changes: 29 additions & 0 deletions scarb/src/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl FileLockGuard {
self.path.as_path()
}

pub fn parent(&self) -> Option<&Utf8Path> {
self.path.parent()
}

pub fn lock_kind(&self) -> FileLockKind {
self.lock_kind
}
Expand Down Expand Up @@ -293,6 +297,31 @@ impl Filesystem {
)
}

/// Opens exclusive access to a [`File`], returning the locked version of it.
///
/// This function will fail if `path` doesn't already exist, but if it does then it will
/// acquire an exclusive lock on `path`.
/// If the process must block waiting for the lock, the `description` annotated with _blocking_
/// status message is printed to [`Config::ui`].
///
/// The returned file can be accessed to look at the path and also has read
/// access to the underlying file.
/// Any writes to the file will return an error.
pub fn open_ro_exclusive(
&self,
path: impl AsRef<Utf8Path>,
description: &str,
config: &Config,
) -> Result<FileLockGuard> {
self.open(
path.as_ref(),
OpenOptions::new().read(true),
FileLockKind::Exclusive,
description,
config,
)
}

fn open(
&self,
path: &Utf8Path,
Expand Down
5 changes: 4 additions & 1 deletion scarb/src/ops/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use scarb_ui::{HumanBytes, HumanCount};
use serde::Serialize;

use crate::compiler::plugin::proc_macro::compilation::{
get_crate_archive_basename, package_crate, SharedLibraryProvider,
get_crate_archive_basename, package_crate, unpack_crate, SharedLibraryProvider,
};
use crate::core::publishing::manifest_normalization::prepare_manifest_for_publish;
use crate::core::publishing::source::list_source_files;
Expand Down Expand Up @@ -279,6 +279,9 @@ fn prepare_archive_recipe(
));
}

// Unpack .crate to make normalized Cargo.toml available.
unpack_crate(pkg, ws.config())?;

// Add normalized Cargo.toml file.
recipe.push(ArchiveFile {
path: CARGO_MANIFEST_FILE_NAME.into(),
Expand Down
23 changes: 23 additions & 0 deletions scarb/tests/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,29 @@ fn package_without_verification() {
"#});
}

#[test]
fn package_cairo_plugin_without_verification() {
let t = TempDir::new().unwrap();
CairoPluginProjectBuilder::default().build(&t);

Scarb::quick_snapbox()
.arg("package")
.arg("--no-verify")
.arg("--no-metadata")
.env("CARGO_TERM_QUIET", "true")
.current_dir(&t)
.assert()
.success()
.stdout_matches(indoc! {r#"
[..] Packaging some v1.0.0 [..]
[..]warn: package name or version differs between Cargo manifest and Scarb manifest
[..]Scarb manifest: `some-1.0.0`, Cargo manifest: `some-0.1.0`
[..]this might become an error in future Scarb releases
[..] Packaged [..]
"#});
}

#[test]
fn package_without_publish_metadata() {
let t = TempDir::new().unwrap();
Expand Down

0 comments on commit 0ee3bc5

Please sign in to comment.