Skip to content

Commit

Permalink
fix: recompile artefacts from a different noir version (#3248)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom French <[email protected]>
Co-authored-by: kek kek kek <[email protected]>
Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
4 people authored Oct 24, 2023
1 parent 2c8565f commit 7347b27
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions compiler/noirc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
build-data = "0.1.3"

[dependencies]
clap.workspace = true
noirc_errors.workspace = true
Expand Down
14 changes: 14 additions & 0 deletions compiler/noirc_driver/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const GIT_COMMIT: &&str = &"GIT_COMMIT";

fn main() {
// Rebuild if the tests have changed
println!("cargo:rerun-if-changed=tests");

// Only use build_data if the environment variable isn't set
// The environment variable is always set when working via Nix
if std::env::var(GIT_COMMIT).is_err() {
build_data::set_GIT_COMMIT();
build_data::set_GIT_DIRTY();
build_data::no_debug_rebuilds();
}
}
2 changes: 2 additions & 0 deletions compiler/noirc_driver/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum ContractFunctionType {

#[derive(Serialize, Deserialize)]
pub struct CompiledContract {
pub noir_version: String,

/// The name of the contract.
pub name: String,
/// Each of the contract's functions are compiled into a separate `CompiledProgram`
Expand Down
19 changes: 18 additions & 1 deletion compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ pub use program::CompiledProgram;

const STD_CRATE_NAME: &str = "std";

pub const GIT_COMMIT: &str = env!("GIT_COMMIT");
pub const GIT_DIRTY: &str = env!("GIT_DIRTY");
pub const NOIRC_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Version string that gets placed in artifacts that Noir builds. This is semver compatible.
/// Note: You can't directly use the value of a constant produced with env! inside a concat! macro.
pub const NOIR_ARTIFACT_VERSION_STRING: &str =
concat!(env!("CARGO_PKG_VERSION"), "+", env!("GIT_COMMIT"));

#[derive(Args, Clone, Debug, Default, Serialize, Deserialize)]
pub struct CompileOptions {
/// Emit debug information for the intermediate SSA IR
Expand Down Expand Up @@ -305,6 +314,7 @@ fn compile_contract_inner(
.collect(),
functions,
file_map,
noir_version: NOIR_ARTIFACT_VERSION_STRING.to_string(),
})
} else {
Err(errors)
Expand Down Expand Up @@ -342,5 +352,12 @@ pub fn compile_no_check(

let file_map = filter_relevant_files(&[debug.clone()], &context.file_manager);

Ok(CompiledProgram { hash, circuit, debug, abi, file_map })
Ok(CompiledProgram {
hash,
circuit,
debug,
abi,
file_map,
noir_version: NOIR_ARTIFACT_VERSION_STRING.to_string(),
})
}
1 change: 1 addition & 0 deletions compiler/noirc_driver/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use super::debug::DebugFile;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CompiledProgram {
pub noir_version: String,
/// Hash of the [`Program`][noirc_frontend::monomorphization::ast::Program] from which this [`CompiledProgram`]
/// was compiled.
///
Expand Down
10 changes: 0 additions & 10 deletions compiler/wasm/build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
const GIT_COMMIT: &&str = &"GIT_COMMIT";

fn main() {
// Only use build_data if the environment variable isn't set
// The environment variable is always set when working via Nix
if std::env::var(GIT_COMMIT).is_err() {
build_data::set_GIT_COMMIT();
build_data::set_GIT_DIRTY();
build_data::no_debug_rebuilds();
}

build_data::set_SOURCE_TIMESTAMP();
build_data::no_debug_rebuilds();
}
4 changes: 3 additions & 1 deletion compiler/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nargo::artifacts::{
};
use noirc_driver::{
add_dep, compile_contract, compile_main, prepare_crate, prepare_dependency, CompileOptions,
CompiledContract, CompiledProgram,
CompiledContract, CompiledProgram, NOIR_ARTIFACT_VERSION_STRING,
};
use noirc_frontend::{graph::CrateGraph, hir::Context};
use std::path::Path;
Expand Down Expand Up @@ -118,6 +118,7 @@ fn preprocess_program(program: CompiledProgram) -> PreprocessedProgram {
hash: program.hash,
backend: String::from(BACKEND_IDENTIFIER),
abi: program.abi,
noir_version: NOIR_ARTIFACT_VERSION_STRING.to_string(),
bytecode: program.circuit,
}
}
Expand All @@ -136,6 +137,7 @@ fn preprocess_contract(contract: CompiledContract) -> PreprocessedContract {
.collect();

PreprocessedContract {
noir_version: String::from(NOIR_ARTIFACT_VERSION_STRING),
name: contract.name,
backend: String::from(BACKEND_IDENTIFIER),
functions: preprocessed_functions,
Expand Down
8 changes: 3 additions & 5 deletions compiler/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use getrandom as _;

use gloo_utils::format::JsValueSerdeExt;
use log::Level;
use noirc_driver::{GIT_COMMIT, GIT_DIRTY, NOIRC_VERSION};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -37,11 +38,8 @@ pub fn init_log_level(level: String) {
});
}

const BUILD_INFO: BuildInfo = BuildInfo {
git_hash: env!("GIT_COMMIT"),
version: env!("CARGO_PKG_VERSION"),
dirty: env!("GIT_DIRTY"),
};
const BUILD_INFO: BuildInfo =
BuildInfo { git_hash: GIT_COMMIT, version: NOIRC_VERSION, dirty: GIT_DIRTY };

#[wasm_bindgen]
pub fn build_info() -> JsValue {
Expand Down
5 changes: 4 additions & 1 deletion release-tests/test/version.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ test("promise resolved", async () => {

test("prints version", async () => {
const processOutput = (await $`${NARGO_BIN} --version`).toString();
assert.match(processOutput, /nargo\s\d{1,2}.\d{1,2}/);

// Regex to match the "nargo version" part of the output
assert.match(processOutput, /nargo version = \d{1,2}\.\d{1,2}\.\d{1,2}/);
});


test("reports a clean commit", async () => {
const processOutput = (await $`${NARGO_BIN} --version`).toString();
assert.not.match(processOutput, /is dirty: true/)
Expand Down
2 changes: 2 additions & 0 deletions tooling/nargo/src/artifacts/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use serde::{Deserialize, Serialize};
/// - Proving and verification keys have been pregenerated based on this ACIR.
#[derive(Serialize, Deserialize)]
pub struct PreprocessedContract {
/// Version of noir used to compile this contract
pub noir_version: String,
/// The name of the contract.
pub name: String,
/// The identifier of the proving backend which this contract has been compiled for.
Expand Down
2 changes: 2 additions & 0 deletions tooling/nargo/src/artifacts/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use serde::{Deserialize, Serialize};
/// - Proving and verification keys have been pregenerated based on this ACIR.
#[derive(Serialize, Deserialize, Debug)]
pub struct PreprocessedProgram {
pub noir_version: String,

/// Hash of the [`Program`][noirc_frontend::monomorphization::ast::Program] from which this [`PreprocessedProgram`]
/// was compiled.
///
Expand Down
8 changes: 7 additions & 1 deletion tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use nargo::package::Package;
use nargo::prepare_package;
use nargo::workspace::Workspace;
use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection};
use noirc_driver::NOIR_ARTIFACT_VERSION_STRING;
use noirc_driver::{CompilationResult, CompileOptions, CompiledContract, CompiledProgram};
use noirc_frontend::graph::CrateName;

Expand Down Expand Up @@ -183,19 +184,22 @@ fn compile_program(
hash: preprocessed_program.hash,
circuit: preprocessed_program.bytecode,
abi: preprocessed_program.abi,
noir_version: preprocessed_program.noir_version,
debug: debug_artifact.debug_symbols.remove(0),
file_map: debug_artifact.file_map,
})
} else {
None
};

let force_recompile =
cached_program.as_ref().map_or(false, |p| p.noir_version != NOIR_ARTIFACT_VERSION_STRING);
let (program, warnings) = match noirc_driver::compile_main(
&mut context,
crate_id,
compile_options,
cached_program,
false,
force_recompile,
) {
Ok(program_and_warnings) => program_and_warnings,
Err(errors) => {
Expand Down Expand Up @@ -241,6 +245,7 @@ fn save_program(program: CompiledProgram, package: &Package, circuit_dir: &Path)
hash: program.hash,
backend: String::from(BACKEND_IDENTIFIER),
abi: program.abi,
noir_version: program.noir_version,
bytecode: program.circuit,
};

Expand Down Expand Up @@ -271,6 +276,7 @@ fn save_contract(contract: CompiledContract, package: &Package, circuit_dir: &Pa
});

let preprocessed_contract = PreprocessedContract {
noir_version: contract.noir_version,
name: contract.name,
backend: String::from(BACKEND_IDENTIFIER),
functions: preprocessed_functions,
Expand Down
5 changes: 3 additions & 2 deletions tooling/nargo_cli/src/cli/init_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use crate::backends::Backend;
use crate::errors::CliError;

use super::fs::{create_named_dir, write_to_file};
use super::{NargoConfig, CARGO_PKG_VERSION};
use super::NargoConfig;
use clap::Args;
use nargo::constants::{PKG_FILE, SRC_DIR};
use nargo::package::PackageType;
use noirc_driver::NOIRC_VERSION;
use noirc_frontend::graph::CrateName;
use std::path::PathBuf;

Expand Down Expand Up @@ -72,7 +73,7 @@ pub(crate) fn initialize_project(
name = "{package_name}"
type = "{package_type}"
authors = [""]
compiler_version = "{CARGO_PKG_VERSION}"
compiler_version = "{NOIRC_VERSION}"
[dependencies]"#
);
Expand Down
14 changes: 10 additions & 4 deletions tooling/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::{Args, Parser, Subcommand};
use const_format::formatcp;
use nargo_toml::find_package_root;
use noirc_driver::NOIR_ARTIFACT_VERSION_STRING;
use std::path::PathBuf;

use color_eyre::eyre;
Expand All @@ -26,10 +27,15 @@ mod verify_cmd;

const GIT_HASH: &str = env!("GIT_COMMIT");
const IS_DIRTY: &str = env!("GIT_DIRTY");
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

static VERSION_STRING: &str =
formatcp!("{} (git version hash: {}, is dirty: {})", CARGO_PKG_VERSION, GIT_HASH, IS_DIRTY);
const NARGO_VERSION: &str = env!("CARGO_PKG_VERSION");

static VERSION_STRING: &str = formatcp!(
"version = {}\nnoirc version = {}\n(git version hash: {}, is dirty: {})",
NARGO_VERSION,
NOIR_ARTIFACT_VERSION_STRING,
GIT_HASH,
IS_DIRTY
);

#[derive(Parser, Debug)]
#[command(name="nargo", author, version=VERSION_STRING, about, long_about = None)]
Expand Down

0 comments on commit 7347b27

Please sign in to comment.