Skip to content

Try to read dependency from the current package. #7770

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

Merged
merged 7 commits into from
Aug 15, 2025
Merged
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
4 changes: 3 additions & 1 deletion rewatch/src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,9 @@ fn get_dependency_paths(
.as_ref()
.map(|package| package.path.clone())
} else {
packages::read_dependency(package_name, project_context).ok()
// packages will only be None when called by build::get_compiler_args
// in that case we can safely pass config as the package config.
packages::read_dependency(package_name, config, project_context).ok()
}
.map(|canonicalized_path| {
vec![
Expand Down
47 changes: 42 additions & 5 deletions rewatch/src/build/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,50 @@ pub fn read_config(package_dir: &Path) -> Result<Config> {
}
}

pub fn read_dependency(package_name: &str, project_context: &ProjectContext) -> Result<PathBuf, String> {
pub fn read_dependency(
package_name: &str,
package_config: &Config,
project_context: &ProjectContext,
) -> Result<PathBuf> {
// package folder + node_modules + package_name
// This can happen in the following scenario:
// The ProjectContext has a MonoRepoContext::MonorepoRoot.
// We are reading a dependency from the root package.
// And that local dependency has a hoisted dependency.
let path_from_current_package = package_config
.path
.parent()
.ok_or_else(|| {
anyhow!(
"Expected {} to have a parent folder",
package_config.path.to_string_lossy()
)
})
.map(|parent_path| helpers::package_path(parent_path, package_name))?;

// current folder + node_modules + package_name
let path_from_current_config = project_context
.current_config
.path
.parent()
.ok_or_else(|| {
anyhow!(
"Expected {} to have a parent folder",
project_context.current_config.path.to_string_lossy()
)
})
.map(|parent_path| helpers::package_path(parent_path, package_name))?;

// root folder + node_modules + package_name
let path_from_root = helpers::package_path(project_context.get_root_path(), package_name);
let path = (if path_from_root.exists() {
let path = (if path_from_current_package.exists() {
Ok(path_from_current_package)
} else if path_from_current_config.exists() {
Ok(path_from_current_config)
} else if path_from_root.exists() {
Ok(path_from_root)
} else {
Err(format!(
Err(anyhow!(
"The package \"{package_name}\" is not found (are node_modules up-to-date?)..."
))
})?;
Expand All @@ -266,7 +303,7 @@ pub fn read_dependency(package_name: &str, project_context: &ProjectContext) ->
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
{
Ok(canonical_path) => Ok(canonical_path),
Err(e) => Err(format!(
Err(e) => Err(anyhow!(
"Failed canonicalizing the package \"{}\" path \"{}\" (are node_modules up-to-date?)...\nMore details: {}",
package_name,
path.to_string_lossy(),
Expand Down Expand Up @@ -312,7 +349,7 @@ fn read_dependencies(
.par_iter()
.map(|package_name| {
let (config, canonical_path) =
match read_dependency(package_name, project_context) {
match read_dependency(package_name, package_config, project_context) {
Err(error) => {
if show_progress {
println!(
Expand Down
6 changes: 5 additions & 1 deletion rewatch/testrepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
"packages/file-casing",
"packages/file-casing-no-namespace",
"packages/pure-dev",
"packages/with-ppx"
"packages/with-ppx",
"packages/nohoist"
],
"nohoist": [
"rescript-bun"
]
},
"dependencies": {
Expand Down
7 changes: 7 additions & 0 deletions rewatch/testrepo/packages/nohoist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@testrepo/nohoist",
"dependencies": {
"rescript": "12.0.0-beta.3",
"rescript-bun": "2.0.1"
}
}
5 changes: 5 additions & 0 deletions rewatch/testrepo/packages/nohoist/rescript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@testrepo/nohoist",
"sources": ["src"],
"dependencies": ["rescript-bun"]
}
6 changes: 6 additions & 0 deletions rewatch/testrepo/packages/nohoist/src/BunSample.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


console.log(import.meta.dir);

/* Not a pure module */
1 change: 1 addition & 0 deletions rewatch/testrepo/packages/nohoist/src/BunSample.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Console.log(RescriptBun.Globals.import.meta.dir)
3 changes: 2 additions & 1 deletion rewatch/testrepo/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"@testrepo/deprecated-config",
"@testrepo/file-casing",
"@testrepo/file-casing-no-namespace",
"@testrepo/with-ppx"
"@testrepo/with-ppx",
"@testrepo/nohoist"
],
"dev-dependencies": [
"@testrepo/pure-dev"
Expand Down
83 changes: 83 additions & 0 deletions rewatch/testrepo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,55 @@ __metadata:
languageName: node
linkType: hard

"@rescript/darwin-arm64@npm:12.0.0-beta.3":
version: 12.0.0-beta.3
resolution: "@rescript/darwin-arm64@npm:12.0.0-beta.3"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard

"@rescript/darwin-x64@npm:12.0.0-beta.1":
version: 12.0.0-beta.1
resolution: "@rescript/darwin-x64@npm:12.0.0-beta.1"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard

"@rescript/darwin-x64@npm:12.0.0-beta.3":
version: 12.0.0-beta.3
resolution: "@rescript/darwin-x64@npm:12.0.0-beta.3"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard

"@rescript/linux-arm64@npm:12.0.0-beta.1":
version: 12.0.0-beta.1
resolution: "@rescript/linux-arm64@npm:12.0.0-beta.1"
conditions: os=linux & cpu=arm64
languageName: node
linkType: hard

"@rescript/linux-arm64@npm:12.0.0-beta.3":
version: 12.0.0-beta.3
resolution: "@rescript/linux-arm64@npm:12.0.0-beta.3"
conditions: os=linux & cpu=arm64
languageName: node
linkType: hard

"@rescript/linux-x64@npm:12.0.0-beta.1":
version: 12.0.0-beta.1
resolution: "@rescript/linux-x64@npm:12.0.0-beta.1"
conditions: os=linux & cpu=x64
languageName: node
linkType: hard

"@rescript/linux-x64@npm:12.0.0-beta.3":
version: 12.0.0-beta.3
resolution: "@rescript/linux-x64@npm:12.0.0-beta.3"
conditions: os=linux & cpu=x64
languageName: node
linkType: hard

"@rescript/webapi@npm:0.1.0-experimental-73e6a0d":
version: 0.1.0-experimental-73e6a0d
resolution: "@rescript/webapi@npm:0.1.0-experimental-73e6a0d"
Expand All @@ -49,6 +77,13 @@ __metadata:
languageName: node
linkType: hard

"@rescript/win32-x64@npm:12.0.0-beta.3":
version: 12.0.0-beta.3
resolution: "@rescript/win32-x64@npm:12.0.0-beta.3"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard

"@testrepo/compiled-by-legacy@npm:*, @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"
Expand Down Expand Up @@ -108,6 +143,15 @@ __metadata:
languageName: unknown
linkType: soft

"@testrepo/nohoist@workspace:packages/nohoist":
version: 0.0.0-use.local
resolution: "@testrepo/nohoist@workspace:packages/nohoist"
dependencies:
rescript: "npm:12.0.0-beta.3"
rescript-bun: "npm:2.0.1"
languageName: unknown
linkType: soft

"@testrepo/nonexisting-dev-files@workspace:packages/nonexisting-dev-files":
version: 0.0.0-use.local
resolution: "@testrepo/nonexisting-dev-files@workspace:packages/nonexisting-dev-files"
Expand Down Expand Up @@ -139,6 +183,15 @@ __metadata:
languageName: unknown
linkType: soft

"rescript-bun@npm:2.0.1":
version: 2.0.1
resolution: "rescript-bun@npm:2.0.1"
peerDependencies:
rescript: 12.0.0-beta.3
checksum: 10c0/97fedd71f2706dca0a13084c76cf0f61f7d910cf2e2dfb8dbe33e19d8ad8c5c67aeb25cdf7ab935264fb26e6dda911f1451a370fd7ae10766023c629810d0e36
languageName: node
linkType: hard

"rescript-nodejs@npm:16.1.0":
version: 16.1.0
resolution: "rescript-nodejs@npm:16.1.0"
Expand Down Expand Up @@ -176,6 +229,36 @@ __metadata:
languageName: node
linkType: hard

"rescript@npm:12.0.0-beta.3":
version: 12.0.0-beta.3
resolution: "rescript@npm:12.0.0-beta.3"
dependencies:
"@rescript/darwin-arm64": "npm:12.0.0-beta.3"
"@rescript/darwin-x64": "npm:12.0.0-beta.3"
"@rescript/linux-arm64": "npm:12.0.0-beta.3"
"@rescript/linux-x64": "npm:12.0.0-beta.3"
"@rescript/win32-x64": "npm:12.0.0-beta.3"
dependenciesMeta:
"@rescript/darwin-arm64":
optional: true
"@rescript/darwin-x64":
optional: true
"@rescript/linux-arm64":
optional: true
"@rescript/linux-x64":
optional: true
"@rescript/win32-x64":
optional: true
bin:
bsc: cli/bsc.js
bstracing: cli/bstracing.js
rescript: cli/rescript.js
rescript-legacy: cli/rescript-legacy.js
rescript-tools: cli/rescript-tools.js
checksum: 10c0/da264d99199f600dcaf78d8907b70200333dc1cc3a45a52f2fa2c72e2f5f393a8dedd01ebbcf46de94cbbe59a997a4685d6e20648739cd2234560cfb94cd7832
languageName: node
linkType: hard

"sury-ppx@npm:^11.0.0-alpha.2":
version: 11.0.0-alpha.2
resolution: "sury-ppx@npm:11.0.0-alpha.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cleaned 0/67
Cleaned 0/115
Parsed 2 source files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this changed so much

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added rescript-bun in that last test package.

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/67
Cleaned 0/115
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/67
Cleaned 1/115
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/67
Cleaned 2/115
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/67
Cleaned 2/115
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/67
Cleaned 2/115
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/67
Cleaned 1/115
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/67
Cleaned 1/115
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 38 ];
if [ "$file_count" -eq 134 ];
then
success "Found files with correct suffix"
else
Expand Down
Loading