Skip to content

Commit 7d45b2f

Browse files
committed
fix(ast_tools): enable oxc_ast_tools to be run from any directory (#13898)
Get path to root of repo from `CARGO_MANIFEST_DIR` env var rather than `env::current_dir`, so it doesn't depend on `cwd`. This means that you can run `cargo run -p oxc_ast_tools` from anywhere in the repo, which is really useful when working on parser + linter NAPI packages which contain generated code. The root path will also be used in another PR to follow which generates an ESTree walker.
1 parent 91c88e2 commit 7d45b2f

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

tasks/ast_tools/src/codegen.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
use std::{
2+
env,
3+
path::{Path, PathBuf},
4+
};
5+
16
use rustc_hash::FxHashMap;
27

38
use crate::{
@@ -13,6 +18,8 @@ pub type GeneratorId = usize;
1318
/// [`Schema`] is the source of truth on types, and which generators and derives act upon.
1419
/// [`Codegen`] is the engine which runs the generators and derives.
1520
pub struct Codegen {
21+
/// Path to root of repo.
22+
root_path: PathBuf,
1623
/// Mapping from derive name to `DeriveId`
1724
derive_name_to_id: FxHashMap<&'static str, DeriveId>,
1825
/// Mapping from attribute name to ID of derive/generator which uses the attr,
@@ -23,6 +30,13 @@ pub struct Codegen {
2330
impl Codegen {
2431
/// Create new [`Codegen`].
2532
pub fn new() -> Self {
33+
// Get path to root of repo.
34+
// Use `CARGO_MANIFEST_DIR` instead of `env::current_dir` because want to be able to run this from any directory.
35+
// `CARGO_MANIFEST_DIR` is the path to `tasks/ast_tools`, so pop 2 path segments to get root of repo.
36+
let mut root_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
37+
root_path.pop();
38+
root_path.pop();
39+
2640
let mut derive_name_to_id = FxHashMap::default();
2741

2842
let mut attr_processors = FxHashMap::default();
@@ -58,7 +72,12 @@ impl Codegen {
5872
}
5973
}
6074

61-
Self { derive_name_to_id, attr_processors }
75+
Self { root_path, derive_name_to_id, attr_processors }
76+
}
77+
78+
/// Get path to root of repo.
79+
pub fn root_path(&self) -> &Path {
80+
&self.root_path
6281
}
6382

6483
/// Get a [`Derive`] by its name.

tasks/ast_tools/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn main() {
357357
// Write outputs to disk
358358
if !options.dry_run {
359359
for output in outputs {
360-
output.write_to_file().unwrap();
360+
output.write_to_file(codegen.root_path()).unwrap();
361361
}
362362
}
363363
}
@@ -428,7 +428,7 @@ fn generate_updated_proc_macro(codegen: &Codegen) -> RawOutput {
428428
// Load `oxc_ast_macros` crate's `lib.rs` file.
429429
// Substitute list of used attrs into `#[proc_macro_derive(Ast, attributes(...))]`.
430430
let path = format!("{AST_MACROS_CRATE_PATH}/src/lib.rs");
431-
let code = fs::read_to_string(&path).unwrap();
431+
let code = fs::read_to_string(codegen.root_path().join(&path)).unwrap();
432432
let (start, end) = code.split_once("#[proc_macro_derive(").unwrap();
433433
let (_, end) = end.split_once(")]").unwrap();
434434
assert!(end.starts_with("\npub fn ast_derive("));

tasks/ast_tools/src/output/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,26 @@ pub struct RawOutput {
7979

8080
impl RawOutput {
8181
/// Write [`RawOutput`] to file
82-
pub fn write_to_file(&self) -> io::Result<()> {
82+
pub fn write_to_file(&self, root_path: &Path) -> io::Result<()> {
8383
log!("Write {}... ", &self.path);
84-
let result = write_to_file_impl(&self.content, &self.path);
84+
let result = write_to_file_impl(&self.content, &self.path, root_path);
8585
log_result!(result);
8686
result
8787
}
8888
}
8989

90-
fn write_to_file_impl(data: &[u8], path: &str) -> io::Result<()> {
90+
fn write_to_file_impl(data: &[u8], path: &str, root_path: &Path) -> io::Result<()> {
91+
let path = root_path.join(path);
92+
9193
// If contents hasn't changed, don't touch the file
92-
if let Ok(existing_data) = fs::read(path)
94+
if let Ok(existing_data) = fs::read(&path)
9395
&& existing_data == data
9496
{
9597
return Ok(());
9698
}
9799

98-
let path = Path::new(path);
99-
if let Some(parent) = path.parent() {
100-
fs::create_dir_all(parent)?;
100+
if let Some(parent_path) = path.parent() {
101+
fs::create_dir_all(parent_path)?;
101102
}
102103
fs::write(path, data)
103104
}

tasks/ast_tools/src/parse/load.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fs;
1+
use std::{fs, path::Path};
22

33
use indexmap::map::Entry;
44
use syn::{
@@ -31,8 +31,9 @@ pub fn load_file(
3131
file_path: &str,
3232
skeletons: &mut FxIndexMap<String, Skeleton>,
3333
meta_skeletons: &mut FxIndexMap<String, Skeleton>,
34+
root_path: &Path,
3435
) {
35-
let content = fs::read_to_string(file_path).unwrap();
36+
let content = fs::read_to_string(root_path.join(file_path)).unwrap();
3637

3738
let file = parse_file(content.as_str()).unwrap();
3839

tasks/ast_tools/src/parse/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
//! [`Derive`]: crate::Derive
4848
//! [`Generator`]: crate::Generator
4949
50+
use std::path::Path;
51+
5052
use oxc_index::IndexVec;
5153

5254
use crate::{
@@ -80,7 +82,13 @@ pub fn parse_files(file_paths: &[&str], codegen: &Codegen) -> Schema {
8082
.enumerate()
8183
.map(|(file_id, &file_path)| {
8284
let file_id = FileId::from_usize(file_id);
83-
analyse_file(file_id, file_path, &mut skeletons, &mut meta_skeletons)
85+
analyse_file(
86+
file_id,
87+
file_path,
88+
&mut skeletons,
89+
&mut meta_skeletons,
90+
codegen.root_path(),
91+
)
8492
})
8593
.collect::<IndexVec<_, _>>();
8694

@@ -96,9 +104,10 @@ fn analyse_file(
96104
file_path: &str,
97105
skeletons: &mut FxIndexMap<String, Skeleton>,
98106
meta_skeletons: &mut FxIndexMap<String, Skeleton>,
107+
root_path: &Path,
99108
) -> File {
100109
log!("Load {file_path}... ");
101-
load_file(file_id, file_path, skeletons, meta_skeletons);
110+
load_file(file_id, file_path, skeletons, meta_skeletons, root_path);
102111
log_success!();
103112

104113
File::new(file_path)

0 commit comments

Comments
 (0)