Skip to content
Open
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
30 changes: 28 additions & 2 deletions analysis/src/FindFiles.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ let collectFiles directory =
| None -> None
| Some res -> Some (modName, SharedTypes.Impl {cmt; res}))

(* Dependency resolution uses the package graph recorded by the build system in
.sourcedirs.json when available. If a package is not listed there, analysis
falls back to walking up node_modules from the project root. *)
let readSourcedirsPackageRoots base =
let sourceDirsFile = base /+ "lib" /+ "bs" /+ ".sourcedirs.json" in
let readPackageEntry = function
| Json.Array [Json.String name; Json.String path] ->
let path = if Filename.is_relative path then base /+ path else path in
Some (name, path)
| _ -> None
in
match Files.readFile sourceDirsFile with
| None -> []
| Some text -> (
match Json.parse text with
| None -> []
| Some json -> (
match json |> Json.get "pkgs" |> bind Json.array with
| None -> []
| Some packages -> packages |> List.filter_map readPackageEntry))

let findPackageRoot ~base ~sourcedirsPackageRoots name =
match List.assoc_opt name sourcedirsPackageRoots with
| Some path when Files.exists path -> Some path
| _ -> ModuleResolution.resolveNodeModulePath ~startPath:base name

(* returns a list of (absolute path to cmt(i), relative path from base to source file) *)
let findProjectFiles ~public ~namespace ~path ~sourceDirectories ~libBs =
let dirs =
Expand Down Expand Up @@ -233,12 +259,12 @@ let findDependencyFiles base config =
in
let deps = deps @ devDeps in
Log.log ("Dependencies: " ^ String.concat " " deps);
let sourcedirsPackageRoots = readSourcedirsPackageRoots base in
let depFiles =
deps
|> List.map (fun name ->
let result =
Json.bind
(ModuleResolution.resolveNodeModulePath ~startPath:base name)
Json.bind (findPackageRoot ~base ~sourcedirsPackageRoots name)
(fun path ->
let rescriptJsonPath = path /+ "rescript.json" in

Expand Down
2 changes: 2 additions & 0 deletions tests/analysis_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ test-analysis-binary:
make -C tests test
make -C tests-generic-jsx-transform test
make -C tests-incremental-typechecking test
make -C tests-sourcedirs-dependency test

test-reanalyze:
make -C tests-reanalyze test
Expand All @@ -14,6 +15,7 @@ clean:
make -C tests clean
make -C tests-generic-jsx-transform clean
make -C tests-incremental-typechecking clean
make -C tests-sourcedirs-dependency clean
make -C tests-reanalyze clean

.PHONY: test-analysis-binary test-reanalyze clean test
2 changes: 2 additions & 0 deletions tests/analysis_tests/tests-sourcedirs-dependency/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/
node_modules/
14 changes: 14 additions & 0 deletions tests/analysis_tests/tests-sourcedirs-dependency/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SHELL = /bin/bash

build:
yarn build

test: build
./test.sh

clean:
yarn clean

.DEFAULT_GOAL := test

.PHONY: clean test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@tests/sourcedirs-dependency-lib",
"private": true,
"scripts": {
"build": "rescript build",
"clean": "rescript clean"
},
"dependencies": {
"rescript": "workspace:^"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@tests/sourcedirs-dependency-lib",
"sources": [
{
"dir": "src",
"subdirs": true
}
],
"package-specs": [{ "module": "commonjs", "in-source": false }],
"suffix": ".res.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let valueFromDependency = 1
12 changes: 12 additions & 0 deletions tests/analysis_tests/tests-sourcedirs-dependency/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@tests/sourcedirs-dependency",
"private": true,
"scripts": {
"build": "rescript build",
"clean": "rescript clean"
},
"dependencies": {
"@tests/sourcedirs-dependency-lib": "workspace:*",
"rescript": "workspace:^"
}
}
12 changes: 12 additions & 0 deletions tests/analysis_tests/tests-sourcedirs-dependency/rescript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@tests/sourcedirs-dependency",
"sources": [
{
"dir": "src",
"subdirs": true
}
],
"dependencies": ["@tests/sourcedirs-dependency-lib"],
"package-specs": [{ "module": "commonjs", "in-source": false }],
"suffix": ".res.js"
}
4 changes: 4 additions & 0 deletions tests/analysis_tests/tests-sourcedirs-dependency/src/Main.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let _ = WorkspaceDep.valueFromDependency

// WorkspaceDep.value
// ^com
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Complete src/Main.res 2:16
posCursor:[2:16] posNoWhite:[2:15] Found expr:[2:3->2:21]
Pexp_ident WorkspaceDep.value:[2:3->2:21]
Completable: Cpath Value[WorkspaceDep, value]
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
Resolved opens 1 Stdlib
ContextPath Value[WorkspaceDep, value]
Path WorkspaceDep.value
[{
"label": "valueFromDependency",
"kind": 12,
"tags": [],
"detail": "int",
"documentation": null
}]

21 changes: 21 additions & 0 deletions tests/analysis_tests/tests-sourcedirs-dependency/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
for file in src/*.res; do
output="$(dirname $file)/expected/$(basename $file).txt"
../../../_build/install/default/bin/rescript-editor-analysis test $file &> $output
# CI. We use LF, and the CI OCaml fork prints CRLF. Convert.
if [ "$RUNNER_OS" == "Windows" ]; then
perl -pi -e 's/\r\n/\n/g' -- $output
fi
done

warningYellow='\033[0;33m'
successGreen='\033[0;32m'
reset='\033[0m'

diff=$(git ls-files --modified src/expected)
if [[ $diff = "" ]]; then
printf "${successGreen}✅ No unstaged tests difference.${reset}\n"
else
printf "${warningYellow}⚠️ There are unstaged differences in tests/! Did you break a test?\n${diff}\n${reset}"
git --no-pager diff src/expected
exit 1
fi
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,23 @@ __metadata:
languageName: unknown
linkType: soft

"@tests/sourcedirs-dependency-lib@workspace:*, @tests/sourcedirs-dependency-lib@workspace:tests/analysis_tests/tests-sourcedirs-dependency/dependency":
version: 0.0.0-use.local
resolution: "@tests/sourcedirs-dependency-lib@workspace:tests/analysis_tests/tests-sourcedirs-dependency/dependency"
dependencies:
rescript: "workspace:^"
languageName: unknown
linkType: soft

"@tests/sourcedirs-dependency@workspace:tests/analysis_tests/tests-sourcedirs-dependency":
version: 0.0.0-use.local
resolution: "@tests/sourcedirs-dependency@workspace:tests/analysis_tests/tests-sourcedirs-dependency"
dependencies:
"@tests/sourcedirs-dependency-lib": "workspace:*"
rescript: "workspace:^"
languageName: unknown
linkType: soft

"@tests/tools@workspace:tests/tools_tests":
version: 0.0.0-use.local
resolution: "@tests/tools@workspace:tests/tools_tests"
Expand Down
Loading