Skip to content

Commit c55b479

Browse files
committed
Use oxc_resolver for ppx path resolution
1 parent b70e6ad commit c55b479

23 files changed

+555
-82
lines changed

rewatch/Cargo.lock

Lines changed: 428 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rewatch/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ serde = { version = "1.0.152", features = ["derive"] }
2626
serde_json = { version = "1.0.93" }
2727
sysinfo = "0.29.10"
2828
tempfile = "3.10.1"
29+
oxc_resolver = "11.6.0"
2930

3031

3132
[profile.release]

rewatch/src/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ pub fn get_compiler_args(path: &Path) -> Result<String> {
7878
&rescript_config,
7979
&root_rescript_config,
8080
relative_filename,
81-
&workspace_root,
8281
workspace_root.as_ref().unwrap_or(&package_root),
8382
&contents,
8483
);

rewatch/src/build/parse.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ pub fn generate_asts(
5353
root_package.to_owned(),
5454
&source_file.implementation.path.to_owned(),
5555
&build_state.bsc_path,
56-
&build_state.workspace_root,
5756
);
5857

5958
let iast_result = match source_file.interface.as_ref().map(|i| i.path.to_owned()) {
@@ -62,7 +61,6 @@ pub fn generate_asts(
6261
root_package.to_owned(),
6362
&interface_file_path.to_owned(),
6463
&build_state.bsc_path,
65-
&build_state.workspace_root,
6664
)
6765
.map(Some),
6866
_ => Ok(None),
@@ -243,21 +241,12 @@ pub fn parser_args(
243241
config: &config::Config,
244242
root_config: &config::Config,
245243
filename: &Path,
246-
workspace_root: &Option<PathBuf>,
247244
root_path: &Path,
248245
contents: &str,
249246
) -> (PathBuf, Vec<String>) {
250247
let file = &filename;
251248
let ast_path = helpers::get_ast_path(file);
252-
let ppx_flags = config::flatten_ppx_flags(
253-
&if let Some(workspace_root) = workspace_root {
254-
workspace_root.join("node_modules")
255-
} else {
256-
root_path.join("node_modules")
257-
},
258-
&filter_ppx_flags(&config.ppx_flags, contents),
259-
&config.name,
260-
);
249+
let ppx_flags = config::flatten_ppx_flags(root_path, &filter_ppx_flags(&config.ppx_flags, contents));
261250
let jsx_args = root_config.get_jsx_args();
262251
let jsx_module_args = root_config.get_jsx_module_args();
263252
let jsx_mode_args = root_config.get_jsx_mode_args();
@@ -292,7 +281,6 @@ fn generate_ast(
292281
root_package: packages::Package,
293282
filename: &Path,
294283
bsc_path: &PathBuf,
295-
workspace_root: &Option<PathBuf>,
296284
) -> Result<(PathBuf, Option<helpers::StdErr>), String> {
297285
let file_path = PathBuf::from(&package.path).join(filename);
298286
let contents = helpers::read_file(&file_path).expect("Error reading file");
@@ -302,7 +290,6 @@ fn generate_ast(
302290
&package.config,
303291
&root_package.config,
304292
filename,
305-
workspace_root,
306293
&root_package.path,
307294
&contents,
308295
);

rewatch/src/config.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::build::packages;
22
use crate::helpers::deserialize::*;
33
use anyhow::{Result, bail};
44
use convert_case::{Case, Casing};
5+
use oxc_resolver::{ResolveOptions, Resolver};
56
use serde::Deserialize;
67
use std::fs;
78
use std::path::{Path, PathBuf};
@@ -288,46 +289,30 @@ pub fn flatten_flags(flags: &Option<Vec<OneOrMore<String>>>) -> Vec<String> {
288289

289290
/// Since ppx-flags could be one or more, and could potentially be nested, this function takes the
290291
/// flags and flattens them.
291-
pub fn flatten_ppx_flags(
292-
node_modules_dir: &Path,
293-
flags: &Option<Vec<OneOrMore<String>>>,
294-
package_name: &String,
295-
) -> Vec<String> {
292+
pub fn flatten_ppx_flags(root_path: &Path, flags: &Option<Vec<OneOrMore<String>>>) -> Vec<String> {
293+
let oxc_resolver = Resolver::new(ResolveOptions::default());
296294
match flags {
297295
None => vec![],
298296
Some(flags) => flags
299297
.iter()
300298
.flat_map(|x| match x {
301-
OneOrMore::Single(y) => {
302-
let first_character = y.chars().next();
303-
match first_character {
304-
Some('.') => {
305-
vec![
306-
"-ppx".to_string(),
307-
node_modules_dir
308-
.join(package_name)
309-
.join(y)
310-
.to_string_lossy()
311-
.to_string(),
312-
]
313-
}
314-
_ => vec![
315-
"-ppx".to_string(),
316-
node_modules_dir.join(y).to_string_lossy().to_string(),
317-
],
318-
}
319-
}
299+
OneOrMore::Single(y) => vec![
300+
"-ppx".to_string(),
301+
oxc_resolver
302+
.resolve(root_path, y)
303+
.unwrap()
304+
.full_path()
305+
.to_string_lossy()
306+
.to_string(),
307+
],
320308
OneOrMore::Multiple(ys) if ys.is_empty() => vec![],
321309
OneOrMore::Multiple(ys) => {
322-
let first_character = ys[0].chars().next();
323-
let ppx = match first_character {
324-
Some('.') => node_modules_dir
325-
.join(package_name)
326-
.join(&ys[0])
327-
.to_string_lossy()
328-
.to_string(),
329-
_ => node_modules_dir.join(&ys[0]).to_string_lossy().to_string(),
330-
};
310+
let ppx = oxc_resolver
311+
.resolve(root_path, &ys[0])
312+
.unwrap()
313+
.full_path()
314+
.to_string_lossy()
315+
.to_string();
331316
vec![
332317
"-ppx".to_string(),
333318
vec![ppx]

rewatch/testrepo/package.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
{
22
"name": "testrepo",
33
"private": true,
4-
"workspaces": {
5-
"packages": [
6-
"packages/main",
7-
"packages/dep01",
8-
"packages/dep02",
9-
"packages/new-namespace",
10-
"packages/namespace-casing",
11-
"packages/with-dev-deps",
12-
"packages/compiled-by-legacy",
13-
"packages/nonexisting-dev-files",
14-
"packages/deprecated-config",
15-
"packages/file-casing",
16-
"packages/file-casing-no-namespace"
17-
]
18-
},
4+
"workspaces": [
5+
"packages/*"
6+
],
197
"dependencies": {
208
"rescript": "12.0.0-beta.1"
219
},

rewatch/testrepo/packages/dep01/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"author": "",
1313
"license": "MIT",
1414
"dependencies": {
15-
"@testrepo/dep02": "*"
15+
"@testrepo/dep02": "workspace:*"
1616
}
1717
}

rewatch/testrepo/packages/main/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"author": "",
1313
"license": "MIT",
1414
"dependencies": {
15-
"@testrepo/compiled-by-legacy": "*",
16-
"@testrepo/dep01": "*"
15+
"@testrepo/compiled-by-legacy": "workspace:*",
16+
"@testrepo/dep01": "workspace:*"
1717
}
1818
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@testrepo/with-ppx",
3+
"version": "0.0.1",
4+
"keywords": [
5+
"rescript"
6+
],
7+
"author": "",
8+
"license": "MIT",
9+
"dependencies": {
10+
"rescript-nodejs": "16.1.0",
11+
"rescript-schema": "9.3.0-rescript12.0",
12+
"rescript-schema-ppx": "9.0.1"
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@testrepo/with-ppx",
3+
"sources": [
4+
{
5+
"dir": "src"
6+
}
7+
],
8+
"dependencies": ["rescript-nodejs", "rescript-schema"],
9+
"package-specs": {
10+
"module": "es6",
11+
"in-source": true
12+
},
13+
"suffix": ".res.js",
14+
"compiler-flags": ["-open RescriptSchema"],
15+
"ppx-flags": ["rescript-schema-ppx/bin"]
16+
}

0 commit comments

Comments
 (0)