@@ -6677,6 +6677,9 @@ module Bs_pkg : sig
66776677val resolve_bs_package :
66786678 cwd :string -> string -> string option
66796679
6680+
6681+ val resolve_npm_package_file :
6682+ cwd :string -> string -> string option
66806683end = struct
66816684#1 " bs_pkg.ml"
66826685
@@ -6691,13 +6694,13 @@ let resolve_bs_package
66916694 name =
66926695 let marker = Literals. bsconfig_json in
66936696 let sub_path = name // marker in
6694- let rec aux origin cwd name =
6697+ let rec aux cwd =
66956698 let abs_marker = cwd // Literals. node_modules // sub_path in
66966699 if Sys. file_exists abs_marker then Some (Filename. dirname abs_marker)
66976700 else
6698- let cwd' = Filename. dirname cwd in
6701+ let cwd' = Filename. dirname cwd in (* TODO: may non-terminating when see symlinks *)
66996702 if String. length cwd' < String. length cwd then
6700- aux origin cwd' name
6703+ aux cwd'
67016704 else
67026705 try
67036706 let abs_marker =
@@ -6711,8 +6714,38 @@ let resolve_bs_package
67116714 Not_found -> None
67126715 (* Bs_exception.error (Bs_package_not_found name) *)
67136716 in
6714- aux cwd cwd name
6717+ aux cwd
6718+
67156719
6720+ (* * The package does not need to be a bspackage
6721+ example:
6722+ {[
6723+ resolve_npm_package_file ~cwd "reason/refmt"
6724+ ]}
6725+ It also returns the path name
6726+ *)
6727+ let resolve_npm_package_file ~cwd sub_path =
6728+ let rec aux cwd =
6729+ let abs_marker = cwd // Literals. node_modules // sub_path in
6730+ if Sys. file_exists abs_marker then Some abs_marker
6731+ else
6732+ let cwd' = Filename. dirname cwd in
6733+ if String. length cwd' < String. length cwd then
6734+ aux cwd'
6735+ else
6736+ try
6737+ let abs_marker =
6738+ Sys. getenv " npm_config_prefix"
6739+ // " lib" // Literals. node_modules // sub_path in
6740+ if Sys. file_exists abs_marker
6741+ then Some abs_marker
6742+ else None
6743+ (* Bs_exception.error (Bs_package_not_found name) *)
6744+ with
6745+ Not_found -> None
6746+ (* Bs_exception.error (Bs_package_not_found name) *)
6747+ in
6748+ aux cwd
67166749end
67176750module Bsb_default : sig
67186751#1 " bsb_default.mli"
@@ -6832,11 +6865,10 @@ let resolve_bsb_magic_file ~cwd ~desc p =
68326865 else if Filename. is_relative p &&
68336866 p_len > 0 &&
68346867 String. unsafe_get p 0 <> '.' then
6835- let name = String. sub p 0 (String. index p '/' ) in
6836- let package = (Bs_pkg. resolve_bs_package ~cwd name) in
6837- match package with
6838- | None -> failwith (name ^ " not found when resolving " ^ desc)
6839- | Some package -> Bsb_build_util. convert_and_resolve_path (Filename. dirname package // p)
6868+ let p = if Ext_sys. is_windows_or_cygwin then Ext_string. replace_slash_backward p else p in
6869+ match Bs_pkg. resolve_npm_package_file ~cwd p with
6870+ | None -> failwith (p ^ " not found when resolving " ^ desc)
6871+ | Some v -> v
68406872 else
68416873 Bsb_build_util. convert_and_resolve_path p
68426874
0 commit comments