Skip to content

Commit 3057278

Browse files
authored
Resolve workspace dependencies in editor analysis (rescript-lang#8392)
* Resolve workspace dependencies in editor analysis * CHANGELOG
1 parent 858bdaa commit 3057278

14 files changed

Lines changed: 152 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#### :nail_care: Polish
3131

3232
- Improve default argument type mismatch errors. https://github.com/rescript-lang/rescript/pull/8389
33+
- Resolve workspace dependencies in editor analysis. https://github.com/rescript-lang/rescript/pull/8392
3334

3435
#### :house: Internal
3536

analysis/src/FindFiles.ml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,32 @@ let collectFiles directory =
135135
| None -> None
136136
| Some res -> Some (modName, SharedTypes.Impl {cmt; res}))
137137

138+
(* Dependency resolution uses the package graph recorded by the build system in
139+
.sourcedirs.json when available. If a package is not listed there, analysis
140+
falls back to walking up node_modules from the project root. *)
141+
let readSourcedirsPackageRoots base =
142+
let sourceDirsFile = base /+ "lib" /+ "bs" /+ ".sourcedirs.json" in
143+
let readPackageEntry = function
144+
| Json.Array [Json.String name; Json.String path] ->
145+
let path = if Filename.is_relative path then base /+ path else path in
146+
Some (name, path)
147+
| _ -> None
148+
in
149+
match Files.readFile sourceDirsFile with
150+
| None -> []
151+
| Some text -> (
152+
match Json.parse text with
153+
| None -> []
154+
| Some json -> (
155+
match json |> Json.get "pkgs" |> bind Json.array with
156+
| None -> []
157+
| Some packages -> packages |> List.filter_map readPackageEntry))
158+
159+
let findPackageRoot ~base ~sourcedirsPackageRoots name =
160+
match List.assoc_opt name sourcedirsPackageRoots with
161+
| Some path when Files.exists path -> Some path
162+
| _ -> ModuleResolution.resolveNodeModulePath ~startPath:base name
163+
138164
(* returns a list of (absolute path to cmt(i), relative path from base to source file) *)
139165
let findProjectFiles ~public ~namespace ~path ~sourceDirectories ~libBs =
140166
let dirs =
@@ -233,12 +259,12 @@ let findDependencyFiles base config =
233259
in
234260
let deps = deps @ devDeps in
235261
Log.log ("Dependencies: " ^ String.concat " " deps);
262+
let sourcedirsPackageRoots = readSourcedirsPackageRoots base in
236263
let depFiles =
237264
deps
238265
|> List.map (fun name ->
239266
let result =
240-
Json.bind
241-
(ModuleResolution.resolveNodeModulePath ~startPath:base name)
267+
Json.bind (findPackageRoot ~base ~sourcedirsPackageRoots name)
242268
(fun path ->
243269
let rescriptJsonPath = path /+ "rescript.json" in
244270

tests/analysis_tests/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test-analysis-binary:
44
make -C tests test
55
make -C tests-generic-jsx-transform test
66
make -C tests-incremental-typechecking test
7+
make -C tests-sourcedirs-dependency test
78

89
test-reanalyze:
910
make -C tests-reanalyze test
@@ -14,6 +15,7 @@ clean:
1415
make -C tests clean
1516
make -C tests-generic-jsx-transform clean
1617
make -C tests-incremental-typechecking clean
18+
make -C tests-sourcedirs-dependency clean
1719
make -C tests-reanalyze clean
1820

1921
.PHONY: test-analysis-binary test-reanalyze clean test
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib/
2+
node_modules/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SHELL = /bin/bash
2+
3+
build:
4+
yarn build
5+
6+
test: build
7+
./test.sh
8+
9+
clean:
10+
yarn clean
11+
12+
.DEFAULT_GOAL := test
13+
14+
.PHONY: clean test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@tests/sourcedirs-dependency-lib",
3+
"private": true,
4+
"scripts": {
5+
"build": "rescript build",
6+
"clean": "rescript clean"
7+
},
8+
"dependencies": {
9+
"rescript": "workspace:^"
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@tests/sourcedirs-dependency-lib",
3+
"sources": [
4+
{
5+
"dir": "src",
6+
"subdirs": true
7+
}
8+
],
9+
"package-specs": [{ "module": "commonjs", "in-source": false }],
10+
"suffix": ".res.js"
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let valueFromDependency = 1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@tests/sourcedirs-dependency",
3+
"private": true,
4+
"scripts": {
5+
"build": "rescript build",
6+
"clean": "rescript clean"
7+
},
8+
"dependencies": {
9+
"@tests/sourcedirs-dependency-lib": "workspace:*",
10+
"rescript": "workspace:^"
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@tests/sourcedirs-dependency",
3+
"sources": [
4+
{
5+
"dir": "src",
6+
"subdirs": true
7+
}
8+
],
9+
"dependencies": ["@tests/sourcedirs-dependency-lib"],
10+
"package-specs": [{ "module": "commonjs", "in-source": false }],
11+
"suffix": ".res.js"
12+
}

0 commit comments

Comments
 (0)