From 4a26afb91dfc593660316a7052e1407e54f77d6c Mon Sep 17 00:00:00 2001 From: nhtyy Date: Wed, 20 Nov 2024 15:44:39 -0800 Subject: [PATCH] fix: use cargo-lock package, imporve err msg --- Cargo.lock | 14 +++++++++- crates/build/Cargo.toml | 4 +-- crates/build/src/build.rs | 57 +++++++++++++-------------------------- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b2cc1a560..6d03ed4704 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1288,6 +1288,18 @@ dependencies = [ "serde", ] +[[package]] +name = "cargo-lock" +version = "10.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6469776d007022d505bbcc2be726f5f096174ae76d710ebc609eb3029a45b551" +dependencies = [ + "semver 1.0.23", + "serde", + "toml", + "url", +] + [[package]] name = "cargo-platform" version = "0.1.8" @@ -5522,13 +5534,13 @@ name = "sp1-build" version = "3.0.0" dependencies = [ "anyhow", + "cargo-lock", "cargo_metadata", "chrono", "clap", "dirs", "semver 1.0.23", "serde", - "toml", ] [[package]] diff --git a/crates/build/Cargo.toml b/crates/build/Cargo.toml index d63a239add..d81f3e36d3 100644 --- a/crates/build/Cargo.toml +++ b/crates/build/Cargo.toml @@ -15,6 +15,6 @@ anyhow = { version = "1.0.83" } clap = { version = "4.5.9", features = ["derive", "env"] } dirs = "5.0.1" chrono = { version = "0.4.38", default-features = false, features = ["clock"] } -serde = { version = "1.0.215", features = ["derive"] } -toml = "0.8.19" +serde = { workspace = true, features = ["derive"] } semver = "1.0.23" +cargo-lock = "10.0.1" diff --git a/crates/build/src/build.rs b/crates/build/src/build.rs index 289a8f1549..1479036438 100644 --- a/crates/build/src/build.rs +++ b/crates/build/src/build.rs @@ -1,9 +1,5 @@ -use std::{ - io::{BufRead, BufReader}, - path::{Path, PathBuf}, -}; - -use anyhow::{Context, Result}; +use std::path::{Path, PathBuf}; +use anyhow::Result; use cargo_metadata::camino::Utf8PathBuf; use crate::{ @@ -199,17 +195,6 @@ fn print_elf_paths_cargo_directives(target_elf_paths: &[(String, Utf8PathBuf)]) /// /// This is also correct if future releases sharing the workspace version, which should be the case. fn verify_locked_version(program_dir: impl AsRef) -> Result<()> { - #[derive(serde::Deserialize)] - struct LockFile { - package: Vec, - } - - #[derive(serde::Deserialize)] - struct Package { - name: String, - version: String, - } - // we need an exception for our test fixtures if env!("CARGO_PKG_NAME") != "test-artifacts" { return Ok(()); @@ -239,26 +224,18 @@ fn verify_locked_version(program_dir: impl AsRef) -> Result<()> { println!("cargo:warning=Found Cargo.lock at {}", lock_path.display()); - // strip any comments for serialization and the rust compiler header - let reader = BufReader::new(std::fs::File::open(&lock_path)?).lines(); - let toml_string = reader - .skip(4) - .map(|line| line.context("Failed to readline from cargo lock file")) - .map(|line| line.map(|line| line + "\n")) - .collect::>()?; + let lock_file = cargo_lock::Lockfile::load(lock_path)?; - let locked = toml::from_str::(&toml_string)?; - - let vm_package = locked - .package + let vm_package = lock_file + .packages .iter() - .find(|p| p.name == "sp1-zkvm") + .find(|p| p.name.as_str() == "sp1-zkvm") .ok_or_else(|| anyhow::anyhow!("sp1-zkvm not found in lock file!"))?; - let sp1_sdk = locked - .package + let sp1_sdk = lock_file + .packages .iter() - .find(|p| p.name == "sp1-sdk") + .find(|p| p.name.as_str() == "sp1-sdk") .ok_or_else(|| anyhow::anyhow!("sp1-sdk not found in lock file!"))?; // print these just to be useful @@ -267,17 +244,21 @@ fn verify_locked_version(program_dir: impl AsRef) -> Result<()> { println!("cargo:warning=Locked version of sp1-sdk is {}", sp1_sdk.version); println!("cargo:warning=Current toolchain version = {}", toolchain_version); - let vm_version = semver::Version::parse(&vm_package.version)?; let toolchain_version = semver::Version::parse(toolchain_version)?; - let sp1_sdk_version = semver::Version::parse(&sp1_sdk.version)?; + let vm_version = &vm_package.version; + let sp1_sdk_version = &sp1_sdk.version; + + if vm_version.major != toolchain_version.major || vm_version.minor != toolchain_version.minor { + return Err(anyhow::anyhow!( + "Locked version of sp1-zkvm is incompatible with the current toolchain version" + )); + } - if vm_version.major != toolchain_version.major - || vm_version.minor != toolchain_version.minor - || sp1_sdk_version.major != toolchain_version.major + if sp1_sdk_version.major != toolchain_version.major || sp1_sdk_version.minor != toolchain_version.minor { return Err(anyhow::anyhow!( - "Locked version of sp1-zkvm or sp1-sdk is incompatible with the current toolchain version" + "Locked version of sp1-sdk is incompatible with the current toolchain version" )); }