Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow detect_missing_libraries only while building #803

Closed
wants to merge 12 commits into from
10 changes: 0 additions & 10 deletions crates/cli/src/opts/build/zksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ pub struct ZkSyncArgs {
)]
pub fallback_oz: Option<bool>,

/// Detect missing libraries, instead of erroring
///
/// Currently unused
#[clap(long = "zk-detect-missing-libraries")]
pub detect_missing_libraries: bool,

/// Set the LLVM optimization parameter `-O[0 | 1 | 2 | 3 | s | z]`.
/// Use `3` for best performance and `z` for minimal size.
#[clap(
Expand Down Expand Up @@ -164,10 +158,6 @@ impl ZkSyncArgs {
set_if_some!(self.llvm_options.clone(), zksync.llvm_options);
set_if_some!(self.force_evmla, zksync.force_evmla);
set_if_some!(self.fallback_oz, zksync.fallback_oz);
set_if_some!(
self.detect_missing_libraries.then_some(true),
zksync.detect_missing_libraries
);

set_if_some!(self.optimizer.then_some(true), zksync.optimizer);
set_if_some!(
Expand Down
9 changes: 4 additions & 5 deletions crates/config/src/zksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ pub struct ZkSyncConfig {
pub force_evmla: bool,

pub llvm_options: Vec<String>,

/// Detect missing libraries, instead of erroring
///
/// Currently unused
pub detect_missing_libraries: bool,
pub zk_detect_missing_libraries: bool,

/// Enable optimizer for zkSync
pub optimizer: bool,
Expand Down Expand Up @@ -91,7 +90,7 @@ impl Default for ZkSyncConfig {
fallback_oz: Default::default(),
enable_eravm_extensions: Default::default(),
force_evmla: Default::default(),
detect_missing_libraries: Default::default(),
zk_detect_missing_libraries: Default::default(),
llvm_options: Default::default(),
optimizer: true,
optimizer_mode: '3',
Expand Down Expand Up @@ -137,7 +136,7 @@ impl ZkSyncConfig {
via_ir: Some(via_ir),
// Set in project paths.
remappings: Vec::new(),
detect_missing_libraries: self.detect_missing_libraries,
detect_missing_libraries: self.zk_detect_missing_libraries,
enable_eravm_extensions: self.enable_eravm_extensions,
force_evmla: self.force_evmla,
llvm_options: self.llvm_options.clone(),
Expand Down
14 changes: 12 additions & 2 deletions crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use foundry_config::{
value::{Dict, Map, Value},
Metadata, Profile, Provider,
},
zksync::config_create_project,
Config,
};
use serde::Serialize;
Expand Down Expand Up @@ -66,6 +67,10 @@ pub struct BuildArgs {
#[serde(skip)]
pub ignore_eip_3860: bool,

/// Detect missing libraries, instead of erroring
#[clap(long = "zk-detect-missing-libraries", group = "build")]
pub zk_detect_missing_libraries: bool,

#[command(flatten)]
#[serde(flatten)]
pub args: CoreBuildArgs,
Expand Down Expand Up @@ -122,8 +127,13 @@ impl BuildArgs {
// no way to return a default from either branch. Ok(output)
Ok(())
} else {
let zk_project =
foundry_config::zksync::config_create_project(&config, config.cache, false)?;
let mut zk_project = config_create_project(&config, config.cache, false)?;

// Note: Here we override the detect_missing_libraries flag to the compiler settings
// with the value from the CLI argument.
// Doing it here guarantees that this is only used while build is specified.
zk_project.settings.settings.detect_missing_libraries =
self.zk_detect_missing_libraries;

// Collect sources to compile if build subdirectories specified.
let mut files = vec![];
Expand Down
51 changes: 50 additions & 1 deletion crates/forge/tests/cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils::generate_large_contract;
use foundry_config::Config;
use foundry_config::{Config};
use foundry_test_utils::{forgetest, snapbox::IntoData, str};
use globset::Glob;

Expand Down Expand Up @@ -208,3 +208,52 @@ contract ValidContract {}

cmd.args(["build"]).assert_success();
});

forgetest!(test_zk_build_with_missing_libraries_as_arg, |prj, cmd| {
prj.add_source(
"Foo",
r#"
library Foo {
function get_something() external returns (uint256 c) {}
}
"#,
)
.unwrap();

prj.add_source(
"UsesFoo",
r#"
import "./Foo.sol";
contract UsesFoo {
uint256 something;

constructor() {
something = Foo.get_something();
}
}
"#,
)
.unwrap();

cmd.args(["build", "--zksync", "--zk-detect-missing-libraries", "--json", "--force"])
.assert_success()
.stdout_eq(str![[r#"
...
"missingLibraries": [
"src/Foo.sol:Foo"
]
...
"#]]);
});

forgetest_init!(test_zk_missing_libraries_param_only_on_build, |prj, cmd| {
cmd.args(["script", "--zksync", "--zk-detect-missing-libraries"]).assert_failure().stderr_eq(
str![
r#"
...
error: unexpected argument '--zk-detect-missing-libraries' found
...
"#
],
);
});
Loading