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(build): add option to build specific dir #8149

Merged
merged 3 commits into from
Jun 14, 2024
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
5 changes: 5 additions & 0 deletions crates/cli/src/opts/build/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ pub struct CoreBuildArgs {
#[serde(skip)]
pub skip: Option<Vec<SkipBuildFilter>>,

/// Build source files from specified paths.
#[arg(long, short, num_args(0..))]
#[serde(skip)]
pub paths: Option<Vec<PathBuf>>,

#[command(flatten)]
#[serde(flatten)]
pub compiler: CompilerArgs,
Expand Down
15 changes: 14 additions & 1 deletion crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use clap::Parser;
use eyre::Result;
use foundry_cli::{opts::CoreBuildArgs, utils::LoadConfig};
use foundry_common::compile::ProjectCompiler;
use foundry_compilers::{Project, ProjectCompileOutput};
use foundry_compilers::{
compilers::{multi::MultiCompilerLanguage, Language},
utils::source_files_iter,
Project, ProjectCompileOutput,
};
use foundry_config::{
figment::{
self,
Expand Down Expand Up @@ -80,7 +84,16 @@ impl BuildArgs {

let project = config.project()?;

// Collect sources to compile if build subdirectories specified.
let mut files = vec![];
if let Some(dirs) = self.args.paths {
for dir in dirs {
files.extend(source_files_iter(dir, MultiCompilerLanguage::FILE_EXTENSIONS));
}
}

let compiler = ProjectCompiler::new()
.files(files)
.print_names(self.names)
.print_sizes(self.sizes)
.quiet(self.format_json)
Expand Down
63 changes: 63 additions & 0 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,69 @@ function test_run() external {}
);
});

forgetest_init!(can_build_specific_paths, |prj, cmd| {
prj.wipe();
prj.add_source(
"Counter.sol",
r"
contract Counter {
function count() external {}
}",
)
.unwrap();
prj.add_test(
"Foo.sol",
r"
contract Foo {
function test_foo() external {}
}",
)
.unwrap();
prj.add_test(
"Bar.sol",
r"
contract Bar {
function test_bar() external {}
}",
)
.unwrap();

// Build 2 files within test dir
prj.clear();
cmd.args(["build", "--paths", "test", "--force"]);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_build_path_with_two_files.stdout"),
);

// Build one file within src dir
prj.clear();
cmd.forge_fuse();
cmd.args(["build", "--paths", "src", "--force"]);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_build_path_with_one_file.stdout"),
);

// Build 3 files from test and src dirs
prj.clear();
cmd.forge_fuse();
cmd.args(["build", "--paths", "src", "test", "--force"]);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_build_path_with_three_files.stdout"),
);

// Build single test file
prj.clear();
cmd.forge_fuse();
cmd.args(["build", "--paths", "test/Bar.sol", "--force"]);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_build_path_with_one_file.stdout"),
);
});

// checks that build --sizes includes all contracts even if unchanged
forgetest_init!(can_build_sizes_repeatedly, |prj, cmd| {
prj.clear_cache();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Compiling 1 files with 0.8.23
Solc 0.8.23 finished in 33.25ms
Compiler run successful!
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Compiling 3 files with 0.8.23
Solc 0.8.23 finished in 33.25ms
Compiler run successful!
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Compiling 2 files with 0.8.23
Solc 0.8.23 finished in 33.25ms
Compiler run successful!
Loading