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

feat(forge): support solc --include-path #2747

Merged
merged 8 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

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

61 changes: 57 additions & 4 deletions cli/tests/it/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
//! Contains various tests for checking forge's commands
use ethers::solc::{
artifacts::{BytecodeHash, Metadata},
ConfigurableContractArtifact,
use ethers::{
prelude::remappings::Remapping,
solc::{
artifacts::{BytecodeHash, Metadata},
ConfigurableContractArtifact,
},
};
use foundry_cli_test_utils::{
ethers_solc::PathStyle,
forgetest, forgetest_init,
util::{read_string, OutputExt, TestCommand, TestProject},
};
use foundry_config::{parse_with_profile, BasicConfig, Chain, Config, SolidityErrorCode};
use std::{env, fs, path::PathBuf};
use std::{env, fs, path::PathBuf, str::FromStr};

// tests `--help` is printed to std out
forgetest!(print_help, |_: TestProject, mut cmd: TestCommand| {
Expand Down Expand Up @@ -1252,3 +1255,53 @@ contract ContractThreeTest is DSTest {
let third_out = cmd.arg("test").arg("--gas-report").stdout();
assert!(third_out.contains("foo") && third_out.contains("bar") && third_out.contains("baz"));
});

forgetest_init!(can_use_absolute_imports, |prj: TestProject, mut cmd: TestCommand| {
let remapping = prj.paths().libraries[0].join("myDepdendency");
let config = Config {
remappings: vec![Remapping::from_str(&format!("myDepdendency/={}", remapping.display()))
.unwrap()
.into()],
..Default::default()
};
prj.write_config(config);

prj.inner()
.add_lib(
"myDepdendency/src/interfaces/IConfig.sol",
r#"
pragma solidity ^0.8.10;

interface IConfig {}
"#,
)
.unwrap();

prj.inner()
.add_lib(
"myDepdendency/src/Config.sol",
r#"
pragma solidity ^0.8.10;
import "src/interfaces/IConfig.sol";

contract Config {}
"#,
)
.unwrap();

prj.inner()
.add_source(
"Greeter",
r#"
pragma solidity ^0.8.10;
import "myDepdendency/src/Config.sol";

contract Greeter {}
"#,
)
.unwrap();

cmd.arg("build");
let stdout = cmd.stdout_lossy();
assert!(stdout.contains("Compiler run successful"));
});
4 changes: 2 additions & 2 deletions cli/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ forgetest!(can_extract_config_values, |prj: TestProject, mut cmd: TestCommand| {
revert_strings: Some(RevertStrings::Strip),
sparse_mode: true,
allow_paths: vec![],
include_paths: vec![],
rpc_endpoints: Default::default(),
build_info: false,
build_info_path: None,
Expand Down Expand Up @@ -477,8 +478,7 @@ forgetest_init!(
vec![
"dep1/=lib/dep1/src/".parse().unwrap(),
"ds-test/=lib/forge-std/lib/ds-test/src/".parse().unwrap(),
"forge-std/=lib/forge-std/src/".parse().unwrap(),
"src/=src/".parse().unwrap()
"forge-std/=lib/forge-std/src/".parse().unwrap()
]
);
}
Expand Down
2 changes: 2 additions & 0 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ libs = ['lib']
remappings = []
# additional solc allow paths
allow_paths = []
# additional solc include paths
include_paths = []
# list of libraries to link in the form of `<path to lib>:<lib name>:<address>`: `"src/MyLib.sol:MyLib:0x8De6DDbCd5053d32292AAA0D2105A32d108484a6"`
# the <path to lib> supports remappings
libraries = []
Expand Down
17 changes: 8 additions & 9 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ pub struct Config {
pub cache_path: PathBuf,
/// where the broadcast logs are stored
pub broadcast: PathBuf,
/// additional solc allow paths
/// additional solc allow paths for `--allow-paths`
pub allow_paths: Vec<PathBuf>,
/// additional solc include paths for `--include-path`
pub include_paths: Vec<PathBuf>,
/// whether to force a `project.clean()`
pub force: bool,
/// evm version to use
Expand Down Expand Up @@ -502,6 +504,8 @@ impl Config {

self.allow_paths = self.allow_paths.into_iter().map(|allow| p(&root, &allow)).collect();

self.include_paths = self.include_paths.into_iter().map(|allow| p(&root, &allow)).collect();

if let Some(ref mut model_checker) = self.model_checker {
model_checker.contracts = std::mem::take(&mut model_checker.contracts)
.into_iter()
Expand Down Expand Up @@ -588,6 +592,7 @@ impl Config {
.allowed_path(&self.__root.0)
.allowed_paths(&self.libs)
.allowed_paths(&self.allow_paths)
.include_paths(&self.include_paths)
.solc_config(SolcConfig::builder().settings(self.solc_settings()?).build())
.ignore_error_codes(self.ignored_error_codes.iter().copied().map(Into::into))
.set_auto_detect(self.is_auto_detect())
Expand Down Expand Up @@ -714,13 +719,7 @@ impl Config {
/// contracts/math/math.sol
/// ```
pub fn get_all_remappings(&self) -> Vec<Remapping> {
self.remappings
.iter()
.map(|m| m.clone().into())
.chain(self.get_source_dir_remapping())
.chain(self.get_test_dir_remapping())
.chain(self.get_script_dir_remapping())
.collect()
self.remappings.iter().map(|m| m.clone().into()).collect()
}

/// Returns the remapping for the project's _src_ directory
Expand Down Expand Up @@ -1469,6 +1468,7 @@ impl Default for Config {
cache_path: "cache".into(),
broadcast: "broadcast".into(),
allow_paths: vec![],
include_paths: vec![],
force: false,
evm_version: Default::default(),
gas_reports: vec!["*".to_string()],
Expand Down Expand Up @@ -2634,7 +2634,6 @@ mod tests {
Remapping::from_str("ds-test/=lib/ds-test/src/").unwrap(),
Remapping::from_str("env-lib/=lib/env-lib/").unwrap(),
Remapping::from_str("other/=lib/other/").unwrap(),
Remapping::from_str("some-source/=some-source/").unwrap(),
],
);

Expand Down