Skip to content

Use oxc_resolver for ppx path resolution #7740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
428 changes: 428 additions & 0 deletions rewatch/Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rewatch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93" }
sysinfo = "0.29.10"
tempfile = "3.10.1"
oxc_resolver = "11.6.0"


[profile.release]
Expand Down
1 change: 0 additions & 1 deletion rewatch/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pub fn get_compiler_args(path: &Path) -> Result<String> {
&rescript_config,
&root_rescript_config,
relative_filename,
&workspace_root,
workspace_root.as_ref().unwrap_or(&package_root),
&contents,
);
Expand Down
15 changes: 1 addition & 14 deletions rewatch/src/build/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ pub fn generate_asts(
root_package.to_owned(),
&source_file.implementation.path.to_owned(),
&build_state.bsc_path,
&build_state.workspace_root,
);

let iast_result = match source_file.interface.as_ref().map(|i| i.path.to_owned()) {
Expand All @@ -62,7 +61,6 @@ pub fn generate_asts(
root_package.to_owned(),
&interface_file_path.to_owned(),
&build_state.bsc_path,
&build_state.workspace_root,
)
.map(Some),
_ => Ok(None),
Expand Down Expand Up @@ -243,21 +241,12 @@ pub fn parser_args(
config: &config::Config,
root_config: &config::Config,
filename: &Path,
workspace_root: &Option<PathBuf>,
root_path: &Path,
contents: &str,
) -> (PathBuf, Vec<String>) {
let file = &filename;
let ast_path = helpers::get_ast_path(file);
let ppx_flags = config::flatten_ppx_flags(
&if let Some(workspace_root) = workspace_root {
workspace_root.join("node_modules")
} else {
root_path.join("node_modules")
},
&filter_ppx_flags(&config.ppx_flags, contents),
&config.name,
);
let ppx_flags = config::flatten_ppx_flags(root_path, &filter_ppx_flags(&config.ppx_flags, contents));
let jsx_args = root_config.get_jsx_args();
let jsx_module_args = root_config.get_jsx_module_args();
let jsx_mode_args = root_config.get_jsx_mode_args();
Expand Down Expand Up @@ -292,7 +281,6 @@ fn generate_ast(
root_package: packages::Package,
filename: &Path,
bsc_path: &PathBuf,
workspace_root: &Option<PathBuf>,
) -> Result<(PathBuf, Option<helpers::StdErr>), String> {
let file_path = PathBuf::from(&package.path).join(filename);
let contents = helpers::read_file(&file_path).expect("Error reading file");
Expand All @@ -302,7 +290,6 @@ fn generate_ast(
&package.config,
&root_package.config,
filename,
workspace_root,
&root_package.path,
&contents,
);
Expand Down
51 changes: 18 additions & 33 deletions rewatch/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::build::packages;
use crate::helpers::deserialize::*;
use anyhow::{Result, bail};
use convert_case::{Case, Casing};
use oxc_resolver::{ResolveOptions, Resolver};
use serde::Deserialize;
use std::fs;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -288,46 +289,30 @@ pub fn flatten_flags(flags: &Option<Vec<OneOrMore<String>>>) -> Vec<String> {

/// Since ppx-flags could be one or more, and could potentially be nested, this function takes the
/// flags and flattens them.
pub fn flatten_ppx_flags(
node_modules_dir: &Path,
flags: &Option<Vec<OneOrMore<String>>>,
package_name: &String,
) -> Vec<String> {
pub fn flatten_ppx_flags(root_path: &Path, flags: &Option<Vec<OneOrMore<String>>>) -> Vec<String> {
let oxc_resolver = Resolver::new(ResolveOptions::default());
match flags {
None => vec![],
Some(flags) => flags
.iter()
.flat_map(|x| match x {
OneOrMore::Single(y) => {
let first_character = y.chars().next();
match first_character {
Some('.') => {
vec![
"-ppx".to_string(),
node_modules_dir
.join(package_name)
.join(y)
.to_string_lossy()
.to_string(),
]
}
_ => vec![
"-ppx".to_string(),
node_modules_dir.join(y).to_string_lossy().to_string(),
],
}
}
OneOrMore::Single(y) => vec![
"-ppx".to_string(),
oxc_resolver
.resolve(root_path, y)
.unwrap()
.full_path()
.to_string_lossy()
.to_string(),
],
OneOrMore::Multiple(ys) if ys.is_empty() => vec![],
OneOrMore::Multiple(ys) => {
let first_character = ys[0].chars().next();
let ppx = match first_character {
Some('.') => node_modules_dir
.join(package_name)
.join(&ys[0])
.to_string_lossy()
.to_string(),
_ => node_modules_dir.join(&ys[0]).to_string_lossy().to_string(),
};
let ppx = oxc_resolver
.resolve(root_path, &ys[0])
.unwrap()
.full_path()
.to_string_lossy()
.to_string();
vec![
"-ppx".to_string(),
vec![ppx]
Expand Down
18 changes: 3 additions & 15 deletions rewatch/testrepo/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
{
"name": "testrepo",
"private": true,
"workspaces": {
"packages": [
"packages/main",
"packages/dep01",
"packages/dep02",
"packages/new-namespace",
"packages/namespace-casing",
"packages/with-dev-deps",
"packages/compiled-by-legacy",
"packages/nonexisting-dev-files",
"packages/deprecated-config",
"packages/file-casing",
"packages/file-casing-no-namespace"
]
},
"workspaces": [
"packages/*"
],
"dependencies": {
"rescript": "12.0.0-beta.1"
},
Expand Down
2 changes: 1 addition & 1 deletion rewatch/testrepo/packages/dep01/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"author": "",
"license": "MIT",
"dependencies": {
"@testrepo/dep02": "*"
"@testrepo/dep02": "workspace:*"
}
}
4 changes: 2 additions & 2 deletions rewatch/testrepo/packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"@testrepo/compiled-by-legacy": "*",
"@testrepo/dep01": "*"
"@testrepo/compiled-by-legacy": "workspace:*",
"@testrepo/dep01": "workspace:*"
}
}
14 changes: 14 additions & 0 deletions rewatch/testrepo/packages/with-ppx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@testrepo/with-ppx",
"version": "0.0.1",
"keywords": [
"rescript"
],
"author": "",
"license": "MIT",
"dependencies": {
"rescript-nodejs": "16.1.0",
"rescript-schema": "9.3.0-rescript12.0",
"rescript-schema-ppx": "9.0.1"
}
}
16 changes: 16 additions & 0 deletions rewatch/testrepo/packages/with-ppx/rescript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@testrepo/with-ppx",
"sources": [
{
"dir": "src"
}
],
"dependencies": ["rescript-nodejs", "rescript-schema"],
"package-specs": {
"module": "es6",
"in-source": true
},
"suffix": ".res.js",
"compiler-flags": ["-open RescriptSchema"],
"ppx-flags": ["rescript-schema-ppx/bin"]
}
17 changes: 17 additions & 0 deletions rewatch/testrepo/packages/with-ppx/src/FileWithPpx.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as S$RescriptSchema from "rescript-schema/src/S.mjs";

let schema = S$RescriptSchema.schema(s => ({
foo: s.m(S$RescriptSchema.string)
}));

let foo = S$RescriptSchema.parseOrThrow("{ \"foo\": \"bar\" }", schema);

console.log(foo);

export {
schema,
foo,
}
/* schema Not a pure module */
6 changes: 6 additions & 0 deletions rewatch/testrepo/packages/with-ppx/src/FileWithPpx.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@schema
type t = {foo: string}

let foo = S.parseOrThrow(`{ "foo": "bar" }`, schema)

Console.log(foo)
3 changes: 2 additions & 1 deletion rewatch/testrepo/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@testrepo/nonexisting-dev-files",
"@testrepo/deprecated-config",
"@testrepo/file-casing",
"@testrepo/file-casing-no-namespace"
"@testrepo/file-casing-no-namespace",
"@testrepo/with-ppx"
]
}
43 changes: 37 additions & 6 deletions rewatch/testrepo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ __metadata:
languageName: node
linkType: hard

"@testrepo/compiled-by-legacy@npm:*, @testrepo/compiled-by-legacy@workspace:packages/compiled-by-legacy":
"@testrepo/compiled-by-legacy@workspace:*, @testrepo/compiled-by-legacy@workspace:packages/compiled-by-legacy":
version: 0.0.0-use.local
resolution: "@testrepo/compiled-by-legacy@workspace:packages/compiled-by-legacy"
languageName: unknown
linkType: soft

"@testrepo/dep01@npm:*, @testrepo/dep01@workspace:packages/dep01":
"@testrepo/dep01@workspace:*, @testrepo/dep01@workspace:packages/dep01":
version: 0.0.0-use.local
resolution: "@testrepo/dep01@workspace:packages/dep01"
dependencies:
"@testrepo/dep02": "npm:*"
"@testrepo/dep02": "workspace:*"
languageName: unknown
linkType: soft

"@testrepo/dep02@npm:*, @testrepo/dep02@workspace:packages/dep02":
"@testrepo/dep02@workspace:*, @testrepo/dep02@workspace:packages/dep02":
version: 0.0.0-use.local
resolution: "@testrepo/dep02@workspace:packages/dep02"
languageName: unknown
Expand Down Expand Up @@ -91,8 +91,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@testrepo/main@workspace:packages/main"
dependencies:
"@testrepo/compiled-by-legacy": "npm:*"
"@testrepo/dep01": "npm:*"
"@testrepo/compiled-by-legacy": "workspace:*"
"@testrepo/dep01": "workspace:*"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -123,13 +123,44 @@ __metadata:
languageName: unknown
linkType: soft

"@testrepo/with-ppx@workspace:packages/with-ppx":
version: 0.0.0-use.local
resolution: "@testrepo/with-ppx@workspace:packages/with-ppx"
dependencies:
rescript-nodejs: "npm:16.1.0"
rescript-schema: "npm:9.3.0-rescript12.0"
rescript-schema-ppx: "npm:9.0.1"
languageName: unknown
linkType: soft

"rescript-nodejs@npm:16.1.0":
version: 16.1.0
resolution: "rescript-nodejs@npm:16.1.0"
checksum: 10c0/2ea271dbddebdceec79bf5ee6089c15474f2c014cb22c1cc39d43ef27fd363fcb1cd8e1244d0cb998cd6b426d7474e3055e41277951fb01ee1eeecf68bbe01ab
languageName: node
linkType: hard

"rescript-schema-ppx@npm:9.0.1":
version: 9.0.1
resolution: "rescript-schema-ppx@npm:9.0.1"
peerDependencies:
rescript-schema: 9.x
checksum: 10c0/c6f95183d3c138f1b1c1110314480c3601366010bbd406f6ab8876e1eef9ab64f87109bda1467801f9905ff30b81c3bf1c6c11ea8bb1ca5ceb801012b0bab39e
languageName: node
linkType: hard

"rescript-schema@npm:9.3.0-rescript12.0":
version: 9.3.0-rescript12.0
resolution: "rescript-schema@npm:9.3.0-rescript12.0"
peerDependencies:
rescript: ^12.0.0-alpha.8
peerDependenciesMeta:
rescript:
optional: true
checksum: 10c0/61dc401034ea310233c531d91ab2ce688644bd255f6dc98c42d632a082639fd5501ac4a97ab53982c56845b0296da2c7d4ff07542878a5eef8a90db2451b4c2e
languageName: node
linkType: hard

"rescript@npm:12.0.0-beta.1, rescript@npm:^12.0.0-alpha.13":
version: 12.0.0-beta.1
resolution: "rescript@npm:12.0.0-beta.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cleaned 0/60
Cleaned 0/64
Parsed 2 source files
Compiled 2 modules

Expand Down
2 changes: 1 addition & 1 deletion rewatch/tests/snapshots/dependency-cycle.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cleaned 0/60
Cleaned 0/64
Parsed 1 source files
Compiled 0 modules

Expand Down
2 changes: 1 addition & 1 deletion rewatch/tests/snapshots/remove-file.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cleaned 1/60
Cleaned 1/64
Parsed 0 source files
Compiled 1 modules

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cleaned 2/60
Cleaned 2/64
Parsed 2 source files
Compiled 3 modules

Expand Down
2 changes: 1 addition & 1 deletion rewatch/tests/snapshots/rename-file-internal-dep.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cleaned 2/60
Cleaned 2/64
Parsed 2 source files
Compiled 2 modules

Expand Down
2 changes: 1 addition & 1 deletion rewatch/tests/snapshots/rename-file-with-interface.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
 No implementation file found for interface file (skipping): src/ModuleWithInterface.resi
Cleaned 2/60
Cleaned 2/64
Parsed 1 source files
Compiled 2 modules

Expand Down
2 changes: 1 addition & 1 deletion rewatch/tests/snapshots/rename-file.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cleaned 1/60
Cleaned 1/64
Parsed 1 source files
Compiled 1 modules

Expand Down
2 changes: 1 addition & 1 deletion rewatch/tests/snapshots/rename-interface-file.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
 No implementation file found for interface file (skipping): src/ModuleWithInterface2.resi
Cleaned 1/60
Cleaned 1/64
Parsed 1 source files
Compiled 2 modules

Expand Down
2 changes: 1 addition & 1 deletion rewatch/tests/suffix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fi
# Count files with new extension
file_count=$(find ./packages -name *.res.js | wc -l)

if [ "$file_count" -eq 36 ];
if [ "$file_count" -eq 38 ];
then
success "Found files with correct suffix"
else
Expand Down
Loading
Loading