Skip to content

Commit 75e3a01

Browse files
committed
fix: disallow flattening invalid files
1 parent 0ebf088 commit 75e3a01

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed

crates/compilers/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl ProjectPathsConfig<SolcLanguage> {
135135
SolcError::msg(format!("cannot resolve file at {}", path.display()))
136136
})?;
137137
let node = graph.node(node_id);
138+
node.data.parse_result()?;
138139
let content = node.content();
139140

140141
// Firstly we strip all licesnses, verson pragmas

crates/compilers/src/resolver/parse.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ pub struct SolData {
2020
pub libraries: Vec<SolLibrary>,
2121
pub contract_names: Vec<String>,
2222
pub is_yul: bool,
23+
pub parse_result: Result<(), String>,
2324
}
2425

2526
impl SolData {
27+
/// Returns the result of parsing the file.
28+
pub fn parse_result(&self) -> crate::Result<()> {
29+
self.parse_result.clone().map_err(crate::SolcError::ParseError)
30+
}
31+
2632
#[allow(dead_code)]
2733
pub fn fmt_version<W: std::fmt::Write>(
2834
&self,
@@ -45,6 +51,7 @@ impl SolData {
4551
let mut imports = Vec::<Spanned<SolImport>>::new();
4652
let mut libraries = Vec::new();
4753
let mut contract_names = Vec::new();
54+
let mut parse_error = Ok(());
4855

4956
let sess = solar_parse::interface::Session::builder()
5057
.with_buffer_emitter(Default::default())
@@ -110,7 +117,9 @@ impl SolData {
110117
}
111118
});
112119
if let Err(e) = sess.emitted_diagnostics().unwrap() {
120+
let e = e.to_string();
113121
trace!("failed parsing {file:?}: {e}");
122+
parse_error = Err(e);
114123
}
115124
let license = content.lines().next().and_then(|line| {
116125
utils::capture_outer_and_inner(
@@ -132,6 +141,7 @@ impl SolData {
132141
libraries,
133142
contract_names,
134143
is_yul,
144+
parse_result: parse_error,
135145
}
136146
}
137147

crates/compilers/tests/project.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ contract A { }
711711
}
712712

713713
#[test]
714-
fn can_flatten_on_failure() {
714+
fn cannot_flatten_on_failure() {
715715
let project = TempProject::<MultiCompiler>::dapptools().unwrap();
716716

717717
project
@@ -742,26 +742,8 @@ contract Contract {
742742
.unwrap();
743743

744744
let result = project.paths().clone().with_language::<SolcLanguage>().flatten(target.as_path());
745-
assert!(result.is_ok());
746-
747-
let result = result.unwrap();
748-
assert_eq!(
749-
result,
750-
r"// SPDX-License-Identifier: UNLICENSED
751-
pragma solidity ^0.8.10;
752-
753-
// src/Lib.sol
754-
755-
library Lib {}
756-
757-
// src/Contract.sol
758-
759-
// Intentionally erroneous code
760-
contract Contract {
761-
failure();
762-
}
763-
"
764-
);
745+
assert!(result.is_err());
746+
println!("{}", result.unwrap_err());
765747
}
766748

767749
#[test]

crates/core/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub enum SolcError {
2727
/// Errors related to the Solc executable itself.
2828
#[error("solc exited with {0}\n{1}")]
2929
SolcError(std::process::ExitStatus, String),
30+
#[error("failed to parse a file: {0}")]
31+
ParseError(String),
3032
#[error("invalid UTF-8 in Solc output")]
3133
InvalidUtf8,
3234
#[error("missing pragma from Solidity file")]

0 commit comments

Comments
 (0)