Skip to content

Commit eca8651

Browse files
committed
Ensure hoisted package is found.
1 parent d82a5f6 commit eca8651

File tree

7 files changed

+62
-12
lines changed

7 files changed

+62
-12
lines changed

rewatch/src/build/compile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ fn get_dependency_paths(
521521
.as_ref()
522522
.map(|package| package.path.clone())
523523
} else {
524-
packages::read_dependency(package_name, project_context).ok()
524+
// packages will only be None when called by build::get_compiler_args
525+
// in that case we can safely pass config as the package config.
526+
packages::read_dependency(package_name, config, project_context).ok()
525527
}
526528
.map(|canonicalized_path| {
527529
vec![

rewatch/src/build/packages.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,45 @@ pub fn read_config(package_dir: &Path) -> Result<Config> {
250250
}
251251
}
252252

253-
pub fn read_dependency(package_name: &str, project_context: &ProjectContext) -> Result<PathBuf> {
253+
pub fn read_dependency(
254+
package_name: &str,
255+
package_config: &Config,
256+
project_context: &ProjectContext,
257+
) -> Result<PathBuf> {
258+
// package folder + node_modules + package_name
259+
// This can happen in the following scenario:
260+
// The ProjectContext has a MonoRepoContext::MonorepoRoot.
261+
// We are reading a dependency from the root package.
262+
// And that local dependency has a hoisted dependency.
263+
let path_from_current_package = package_config
264+
.path
265+
.parent()
266+
.ok_or_else(|| {
267+
anyhow!(
268+
"Expected {} to have a parent folder",
269+
package_config.path.to_string_lossy()
270+
)
271+
})
272+
.map(|parent_path| helpers::package_path(parent_path, package_name))?;
273+
254274
// current folder + node_modules + package_name
255-
let path_from_current_config = {
256-
match project_context.current_config.path.parent() {
257-
None => Err(anyhow!(
275+
let path_from_current_config = project_context
276+
.current_config
277+
.path
278+
.parent()
279+
.ok_or_else(|| {
280+
anyhow!(
258281
"Expected {} to have a parent folder",
259282
project_context.current_config.path.to_string_lossy()
260-
)),
261-
Some(parent_path) => Ok(helpers::package_path(parent_path, package_name)),
262-
}
263-
}?;
283+
)
284+
})
285+
.map(|parent_path| helpers::package_path(parent_path, package_name))?;
286+
264287
// root folder + node_modules + package_name
265288
let path_from_root = helpers::package_path(project_context.get_root_path(), package_name);
266-
let path = (if path_from_current_config.exists() {
289+
let path = (if path_from_current_package.exists() {
290+
Ok(path_from_current_package)
291+
} else if path_from_current_config.exists() {
267292
Ok(path_from_current_config)
268293
} else if path_from_root.exists() {
269294
Ok(path_from_root)
@@ -324,7 +349,7 @@ fn read_dependencies(
324349
.par_iter()
325350
.map(|package_name| {
326351
let (config, canonical_path) =
327-
match read_dependency(package_name, project_context) {
352+
match read_dependency(package_name, package_config, project_context) {
328353
Err(error) => {
329354
if show_progress {
330355
println!(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@testrepo/nohoist",
3+
"dependencies": {
4+
"rescript": "12.0.0-beta.3",
5+
"rescript-bun": "2.0.1"
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@testrepo/nohoist",
3+
"sources": ["src"],
4+
"dependencies": ["rescript-bun"]
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
console.log(import.meta.dir);
5+
6+
/* Not a pure module */
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
open RescriptBun
2+
open RescriptBun.Globals
3+
4+
Console.log(import.meta.dir)

rewatch/testrepo/rescript.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"@testrepo/deprecated-config",
2727
"@testrepo/file-casing",
2828
"@testrepo/file-casing-no-namespace",
29-
"@testrepo/with-ppx"
29+
"@testrepo/with-ppx",
30+
"@testrepo/nohoist"
3031
],
3132
"dev-dependencies": [
3233
"@testrepo/pure-dev"

0 commit comments

Comments
 (0)