Skip to content

Commit 4eefc93

Browse files
committed
fix: correctly set paths in ParsingContext
1 parent 446a5d8 commit 4eefc93

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,5 @@ tokio = { version = "1.35", features = ["rt-multi-thread"] }
7070
snapbox = "0.6.9"
7171

7272
[patch.crates-io]
73-
solar-parse = { git = "https://github.com/paradigmxyz/solar", branch = "main" }
74-
solar-sema = { git = "https://github.com/paradigmxyz/solar", branch = "main" }
75-
73+
solar-parse = { git = "https://github.com/paradigmxyz/solar", branch = "main" }
74+
solar-sema = { git = "https://github.com/paradigmxyz/solar", branch = "main" }

crates/compilers/src/preprocessor/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,32 @@ impl Preprocessor<SolcCompiler> for TestOptimizerPreprocessor {
5555

5656
let sess = Session::builder().with_buffer_emitter(Default::default()).build();
5757
let _ = sess.enter_parallel(|| -> solar_parse::interface::Result {
58+
// Set up the parsing context with the project paths.
5859
let mut parsing_context = ParsingContext::new(&sess);
59-
// Set remappings into HIR parsing context.
60+
parsing_context.file_resolver.set_current_dir(&paths.root);
6061
for remapping in &paths.remappings {
6162
parsing_context
6263
.file_resolver
6364
.add_import_map(PathBuf::from(&remapping.name), PathBuf::from(&remapping.path));
6465
}
65-
// Load and parse test and script contracts only (dependencies are automatically
66-
// resolved).
66+
for include_path in &paths.include_paths {
67+
let _ = parsing_context.file_resolver.add_import_path(include_path.clone());
68+
}
6769

70+
// Add the sources into the context.
6871
let mut preprocessed_paths = vec![];
6972
for (path, source) in sources.iter() {
70-
if is_test_or_script(path, paths) && !source.content.is_empty() {
71-
if let Ok(src_file) = sess
72-
.source_map()
73-
.new_dummy_source_file(path.clone(), source.content.to_string())
73+
if is_test_or_script(path, paths) {
74+
if let Ok(src_file) =
75+
sess.source_map().new_source_file(path.clone(), source.content.as_str())
7476
{
7577
parsing_context.add_file(src_file);
7678
preprocessed_paths.push(path.clone());
7779
}
7880
}
7981
}
8082

83+
// Parse and preprocess.
8184
let hir_arena = ThreadLocal::new();
8285
if let Some(gcx) = parsing_context.parse_and_lower(&hir_arena)? {
8386
let hir = &gcx.get().hir;

crates/compilers/src/project_util/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl TempProject<MultiCompiler, HardhatArtifacts> {
124124
.artifacts(HardhatArtifacts::default())
125125
.paths(paths)
126126
.build(Default::default())?;
127-
Ok(Self::create_new(tmp_dir, inner)?)
127+
Ok(TempProject::create_new(tmp_dir, inner)?)
128128
}
129129
}
130130

@@ -133,7 +133,12 @@ impl<
133133
T: ArtifactOutput<CompilerContract = C::CompilerContract> + Default,
134134
> TempProject<C, T>
135135
{
136-
/// Makes sure all resources are created
136+
/// Wraps an existing project in a temp dir.
137+
pub fn from_project(inner: Project<C, T>) -> std::result::Result<Self, SolcIoError> {
138+
Self::create_new(tempdir("tmp_project")?, inner)
139+
}
140+
141+
/// Makes sure all resources are created.
137142
pub fn create_new(
138143
root: TempDir,
139144
inner: Project<C, T>,
@@ -215,6 +220,14 @@ impl<
215220
&mut self.project_mut().paths
216221
}
217222

223+
/// Deletes the current project and copies it from `source`.
224+
pub fn copy_project_from(&self, source: &Path) -> Result<()> {
225+
let root = self.root();
226+
std::fs::remove_dir_all(root).map_err(|e| SolcIoError::new(e, root))?;
227+
std::fs::create_dir_all(root).map_err(|e| SolcIoError::new(e, root))?;
228+
copy_dir(source, root)
229+
}
230+
218231
/// Copies a single file into the projects source
219232
pub fn copy_source(&self, source: &Path) -> Result<()> {
220233
copy_file(source, &self.paths().sources)

crates/compilers/tests/project.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4164,26 +4164,17 @@ fn can_preprocess_constructors_and_creation_code() {
41644164
canonicalize(Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/preprocessor"))
41654165
.unwrap();
41664166

4167-
let paths = ProjectPathsConfig::builder()
4168-
.sources(root.join("src"))
4169-
.tests(root.join("test"))
4170-
.root(&root)
4171-
.build::<SolcLanguage>()
4172-
.unwrap();
4173-
4174-
let project = ProjectBuilder::<SolcCompiler>::new(Default::default())
4175-
.paths(paths)
4176-
.build(SolcCompiler::default())
4177-
.unwrap();
4178-
4179-
// TODO: figure out how to set root to parsing context.
4180-
let cur_dir = env::current_dir().unwrap();
4181-
env::set_current_dir(root).unwrap();
4182-
let compiled = ProjectCompiler::new(&project)
4167+
let project = TempProject::hardhat().unwrap();
4168+
project.copy_project_from(&root).unwrap();
4169+
let r = ProjectCompiler::new(&project.project())
41834170
.unwrap()
41844171
.with_preprocessor(TestOptimizerPreprocessor)
4185-
.compile()
4186-
.expect("failed to compile");
4172+
.compile();
4173+
4174+
let compiled = match r {
4175+
Ok(compiled) => compiled,
4176+
Err(e) => panic!("failed to compile: {e}"),
4177+
};
41874178
compiled.assert_success();
4188-
env::set_current_dir(cur_dir).unwrap();
4179+
assert!(!compiled.is_unchanged());
41894180
}

0 commit comments

Comments
 (0)