Skip to content

Commit 846b904

Browse files
authored
Try to read dependency from the current package. (#7770)
* Try to read dependency from the current package. * Ensure hoisted package is found. * Ensure no warnings in sample * Add nohoist config * Update numbers in snapshots * Update numbers in custom suffix * Lockfile
1 parent e92879e commit 846b904

18 files changed

+163
-17
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: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,50 @@ 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, String> {
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+
274+
// current folder + node_modules + package_name
275+
let path_from_current_config = project_context
276+
.current_config
277+
.path
278+
.parent()
279+
.ok_or_else(|| {
280+
anyhow!(
281+
"Expected {} to have a parent folder",
282+
project_context.current_config.path.to_string_lossy()
283+
)
284+
})
285+
.map(|parent_path| helpers::package_path(parent_path, package_name))?;
286+
254287
// root folder + node_modules + package_name
255288
let path_from_root = helpers::package_path(project_context.get_root_path(), package_name);
256-
let path = (if path_from_root.exists() {
289+
let path = (if path_from_current_package.exists() {
290+
Ok(path_from_current_package)
291+
} else if path_from_current_config.exists() {
292+
Ok(path_from_current_config)
293+
} else if path_from_root.exists() {
257294
Ok(path_from_root)
258295
} else {
259-
Err(format!(
296+
Err(anyhow!(
260297
"The package \"{package_name}\" is not found (are node_modules up-to-date?)..."
261298
))
262299
})?;
@@ -266,7 +303,7 @@ pub fn read_dependency(package_name: &str, project_context: &ProjectContext) ->
266303
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
267304
{
268305
Ok(canonical_path) => Ok(canonical_path),
269-
Err(e) => Err(format!(
306+
Err(e) => Err(anyhow!(
270307
"Failed canonicalizing the package \"{}\" path \"{}\" (are node_modules up-to-date?)...\nMore details: {}",
271308
package_name,
272309
path.to_string_lossy(),
@@ -312,7 +349,7 @@ fn read_dependencies(
312349
.par_iter()
313350
.map(|package_name| {
314351
let (config, canonical_path) =
315-
match read_dependency(package_name, project_context) {
352+
match read_dependency(package_name, package_config, project_context) {
316353
Err(error) => {
317354
if show_progress {
318355
println!(

rewatch/testrepo/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
"packages/file-casing",
1616
"packages/file-casing-no-namespace",
1717
"packages/pure-dev",
18-
"packages/with-ppx"
18+
"packages/with-ppx",
19+
"packages/nohoist"
20+
],
21+
"nohoist": [
22+
"rescript-bun"
1923
]
2024
},
2125
"dependencies": {
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Console.log(RescriptBun.Globals.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"

rewatch/testrepo/yarn.lock

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,55 @@ __metadata:
1212
languageName: node
1313
linkType: hard
1414

15+
"@rescript/darwin-arm64@npm:12.0.0-beta.3":
16+
version: 12.0.0-beta.3
17+
resolution: "@rescript/darwin-arm64@npm:12.0.0-beta.3"
18+
conditions: os=darwin & cpu=arm64
19+
languageName: node
20+
linkType: hard
21+
1522
"@rescript/darwin-x64@npm:12.0.0-beta.1":
1623
version: 12.0.0-beta.1
1724
resolution: "@rescript/darwin-x64@npm:12.0.0-beta.1"
1825
conditions: os=darwin & cpu=x64
1926
languageName: node
2027
linkType: hard
2128

29+
"@rescript/darwin-x64@npm:12.0.0-beta.3":
30+
version: 12.0.0-beta.3
31+
resolution: "@rescript/darwin-x64@npm:12.0.0-beta.3"
32+
conditions: os=darwin & cpu=x64
33+
languageName: node
34+
linkType: hard
35+
2236
"@rescript/linux-arm64@npm:12.0.0-beta.1":
2337
version: 12.0.0-beta.1
2438
resolution: "@rescript/linux-arm64@npm:12.0.0-beta.1"
2539
conditions: os=linux & cpu=arm64
2640
languageName: node
2741
linkType: hard
2842

43+
"@rescript/linux-arm64@npm:12.0.0-beta.3":
44+
version: 12.0.0-beta.3
45+
resolution: "@rescript/linux-arm64@npm:12.0.0-beta.3"
46+
conditions: os=linux & cpu=arm64
47+
languageName: node
48+
linkType: hard
49+
2950
"@rescript/linux-x64@npm:12.0.0-beta.1":
3051
version: 12.0.0-beta.1
3152
resolution: "@rescript/linux-x64@npm:12.0.0-beta.1"
3253
conditions: os=linux & cpu=x64
3354
languageName: node
3455
linkType: hard
3556

57+
"@rescript/linux-x64@npm:12.0.0-beta.3":
58+
version: 12.0.0-beta.3
59+
resolution: "@rescript/linux-x64@npm:12.0.0-beta.3"
60+
conditions: os=linux & cpu=x64
61+
languageName: node
62+
linkType: hard
63+
3664
"@rescript/webapi@npm:0.1.0-experimental-73e6a0d":
3765
version: 0.1.0-experimental-73e6a0d
3866
resolution: "@rescript/webapi@npm:0.1.0-experimental-73e6a0d"
@@ -49,6 +77,13 @@ __metadata:
4977
languageName: node
5078
linkType: hard
5179

80+
"@rescript/win32-x64@npm:12.0.0-beta.3":
81+
version: 12.0.0-beta.3
82+
resolution: "@rescript/win32-x64@npm:12.0.0-beta.3"
83+
conditions: os=win32 & cpu=x64
84+
languageName: node
85+
linkType: hard
86+
5287
"@testrepo/compiled-by-legacy@npm:*, @testrepo/compiled-by-legacy@workspace:packages/compiled-by-legacy":
5388
version: 0.0.0-use.local
5489
resolution: "@testrepo/compiled-by-legacy@workspace:packages/compiled-by-legacy"
@@ -108,6 +143,15 @@ __metadata:
108143
languageName: unknown
109144
linkType: soft
110145

146+
"@testrepo/nohoist@workspace:packages/nohoist":
147+
version: 0.0.0-use.local
148+
resolution: "@testrepo/nohoist@workspace:packages/nohoist"
149+
dependencies:
150+
rescript: "npm:12.0.0-beta.3"
151+
rescript-bun: "npm:2.0.1"
152+
languageName: unknown
153+
linkType: soft
154+
111155
"@testrepo/nonexisting-dev-files@workspace:packages/nonexisting-dev-files":
112156
version: 0.0.0-use.local
113157
resolution: "@testrepo/nonexisting-dev-files@workspace:packages/nonexisting-dev-files"
@@ -139,6 +183,15 @@ __metadata:
139183
languageName: unknown
140184
linkType: soft
141185

186+
"rescript-bun@npm:2.0.1":
187+
version: 2.0.1
188+
resolution: "rescript-bun@npm:2.0.1"
189+
peerDependencies:
190+
rescript: 12.0.0-beta.3
191+
checksum: 10c0/97fedd71f2706dca0a13084c76cf0f61f7d910cf2e2dfb8dbe33e19d8ad8c5c67aeb25cdf7ab935264fb26e6dda911f1451a370fd7ae10766023c629810d0e36
192+
languageName: node
193+
linkType: hard
194+
142195
"rescript-nodejs@npm:16.1.0":
143196
version: 16.1.0
144197
resolution: "rescript-nodejs@npm:16.1.0"
@@ -176,6 +229,36 @@ __metadata:
176229
languageName: node
177230
linkType: hard
178231

232+
"rescript@npm:12.0.0-beta.3":
233+
version: 12.0.0-beta.3
234+
resolution: "rescript@npm:12.0.0-beta.3"
235+
dependencies:
236+
"@rescript/darwin-arm64": "npm:12.0.0-beta.3"
237+
"@rescript/darwin-x64": "npm:12.0.0-beta.3"
238+
"@rescript/linux-arm64": "npm:12.0.0-beta.3"
239+
"@rescript/linux-x64": "npm:12.0.0-beta.3"
240+
"@rescript/win32-x64": "npm:12.0.0-beta.3"
241+
dependenciesMeta:
242+
"@rescript/darwin-arm64":
243+
optional: true
244+
"@rescript/darwin-x64":
245+
optional: true
246+
"@rescript/linux-arm64":
247+
optional: true
248+
"@rescript/linux-x64":
249+
optional: true
250+
"@rescript/win32-x64":
251+
optional: true
252+
bin:
253+
bsc: cli/bsc.js
254+
bstracing: cli/bstracing.js
255+
rescript: cli/rescript.js
256+
rescript-legacy: cli/rescript-legacy.js
257+
rescript-tools: cli/rescript-tools.js
258+
checksum: 10c0/da264d99199f600dcaf78d8907b70200333dc1cc3a45a52f2fa2c72e2f5f393a8dedd01ebbcf46de94cbbe59a997a4685d6e20648739cd2234560cfb94cd7832
259+
languageName: node
260+
linkType: hard
261+
179262
"sury-ppx@npm:^11.0.0-alpha.2":
180263
version: 11.0.0-alpha.2
181264
resolution: "sury-ppx@npm:11.0.0-alpha.2"

rewatch/tests/snapshots/bs-dev-dependency-used-by-non-dev-source.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Cleaned 0/67
1+
Cleaned 0/115
22
Parsed 2 source files
33
Compiled 2 modules
44

0 commit comments

Comments
 (0)