Skip to content

Commit ead9577

Browse files
authored
fix(lint): lint only files that we build (#11247)
1 parent 256e115 commit ead9577

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

crates/forge/src/cmd/build.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,16 @@ impl BuildArgs {
112112
sh_println!("{}", serde_json::to_string_pretty(&output.output())?)?;
113113
}
114114

115-
if !config.lint.lint_on_build {
116-
return Ok(output);
117-
}
118-
119-
// Only run the `SolidityLinter` if there are no compilation errors
120-
if !output.output().errors.iter().any(|e| e.is_error()) {
121-
self.lint(&project, &config).map_err(|err| eyre!("Lint failed: {err}"))?;
115+
// Only run the `SolidityLinter` if lint on build and no compilation errors.
116+
if config.lint.lint_on_build && !output.output().errors.iter().any(|e| e.is_error()) {
117+
self.lint(&project, &config, self.paths.as_deref())
118+
.map_err(|err| eyre!("Lint failed: {err}"))?;
122119
}
123120

124121
Ok(output)
125122
}
126123

127-
fn lint(&self, project: &Project, config: &Config) -> Result<()> {
124+
fn lint(&self, project: &Project, config: &Config, files: Option<&[PathBuf]>) -> Result<()> {
128125
let format_json = shell::is_json();
129126
if project.compiler.solc.is_some() && !shell::is_quiet() {
130127
let linter = SolidityLinter::new(config.project_paths())
@@ -160,6 +157,10 @@ impl BuildArgs {
160157
.project_paths::<SolcLanguage>()
161158
.input_files_iter()
162159
.filter(|p| {
160+
// Lint only specified build files, if any.
161+
if let Some(files) = files {
162+
return files.iter().any(|file| &curr_dir.join(file) == p);
163+
}
163164
skip.is_match(p)
164165
&& !(ignored.contains(p) || ignored.contains(&curr_dir.join(p)))
165166
})

crates/forge/tests/cli/lint.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ const ONLY_IMPORTS: &str = r#"
4848
import "./ContractWithLints.sol";
4949
"#;
5050

51+
const COUNTER_A: &str = r#"
52+
// SPDX-License-Identifier: MIT
53+
pragma solidity ^0.8.0;
54+
55+
contract CounterA {
56+
uint256 public CounterA_Fail_Lint;
57+
}
58+
"#;
59+
60+
const COUNTER_B: &str = r#"
61+
// SPDX-License-Identifier: MIT
62+
pragma solidity ^0.8.0;
63+
64+
contract CounterB {
65+
uint256 public CounterB_Fail_Lint;
66+
}
67+
"#;
68+
5169
forgetest!(can_use_config, |prj, cmd| {
5270
prj.wipe_contracts();
5371
prj.add_source("ContractWithLints", CONTRACT).unwrap();
@@ -388,6 +406,48 @@ note[unused-import]: unused imports should be removed
388406
"#]]);
389407
});
390408

409+
// <https://github.com/foundry-rs/foundry/issues/11234>
410+
forgetest!(can_lint_only_built_files, |prj, cmd| {
411+
prj.wipe_contracts();
412+
prj.add_source("CounterAWithLints", COUNTER_A).unwrap();
413+
prj.add_source("CounterBWithLints", COUNTER_B).unwrap();
414+
415+
// Both contracts should be linted on build.
416+
cmd.forge_fuse().args(["build"]).assert_success().stderr_eq(str![[r#"
417+
note[mixed-case-variable]: mutable variables should use mixedCase
418+
[FILE]:6:24
419+
|
420+
6 | uint256 public CounterA_Fail_Lint;
421+
| ------------------
422+
|
423+
= help: https://book.getfoundry.sh/reference/forge/forge-lint#mixed-case-variable
424+
425+
note[mixed-case-variable]: mutable variables should use mixedCase
426+
[FILE]:6:24
427+
|
428+
6 | uint256 public CounterB_Fail_Lint;
429+
| ------------------
430+
|
431+
= help: https://book.getfoundry.sh/reference/forge/forge-lint#mixed-case-variable
432+
433+
434+
"#]]);
435+
// Only contract CounterBWithLints that we build should be linted.
436+
cmd.forge_fuse().args(["build", "src/CounterBWithLints.sol"]).assert_success().stderr_eq(str![
437+
[r#"
438+
note[mixed-case-variable]: mutable variables should use mixedCase
439+
[FILE]:6:24
440+
|
441+
6 | uint256 public CounterB_Fail_Lint;
442+
| ------------------
443+
|
444+
= help: https://book.getfoundry.sh/reference/forge/forge-lint#mixed-case-variable
445+
446+
447+
"#]
448+
]);
449+
});
450+
391451
// ------------------------------------------------------------------------------------------------
392452

393453
#[tokio::test]

0 commit comments

Comments
 (0)