@@ -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 ~start Path:base name
163+
138164(* returns a list of (absolute path to cmt(i), relative path from base to source file) *)
139165let 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 ~start Path:base name)
267+ Json. bind (findPackageRoot ~base ~sourcedirs PackageRoots name)
242268 (fun path ->
243269 let rescriptJsonPath = path /+ " rescript.json" in
244270
0 commit comments