@@ -8548,26 +8548,82 @@ let cache : string Coll.t = Coll.create 0
85488548let to_list cb =
85498549 Coll.to_list cache cb
85508550
8551+ (* Some package managers will implement "postinstall" caches, that do not
8552+ * keep their build artifacts in the local node_modules. Similar to
8553+ * npm_config_prefix, bs_custom_resolution allows these to specify the
8554+ * exact location of build cache, but on a per-package basis. Implemented as
8555+ * environment lookup to avoid invasive changes to bsconfig and mandates. *)
8556+ let custom_resolution = lazy
8557+ (match Sys.getenv "bs_custom_resolution" with
8558+ | exception Not_found -> false
8559+ | "true" -> true
8560+ | _ -> false)
8561+
8562+ let pkg_name_as_variable package =
8563+ Bsb_pkg_types.to_string package
8564+ |> fun s -> Ext_string.split s '@'
8565+ |> String.concat ""
8566+ |> fun s -> Ext_string.split s '_'
8567+ |> String.concat "__"
8568+ |> fun s -> Ext_string.split s '/'
8569+ |> String.concat "__slash__"
8570+ |> fun s -> Ext_string.split s '.'
8571+ |> String.concat "__dot__"
8572+ |> fun s -> Ext_string.split s '-'
8573+ |> String.concat "_"
8574+
85518575(** TODO: collect all warnings and print later *)
85528576let resolve_bs_package ~cwd (package : t) =
8553- match Coll.find_opt cache package with
8554- | None ->
8555- let result = resolve_bs_package_aux ~cwd package in
8556- Bsb_log.info "@{<info>Package@} %a -> %s@." Bsb_pkg_types.print package result ;
8557- Coll.add cache package result ;
8558- result
8559- | Some x
8560- ->
8561- let result = resolve_bs_package_aux ~cwd package in
8562- if result <> x then
8577+ if Lazy.force custom_resolution then
8578+ begin
8579+ Bsb_log.info "@{<info>Using Custom Resolution@}@.";
8580+ let custom_pkg_loc = pkg_name_as_variable package ^ "__install" in
8581+ let custom_pkg_location = lazy (Sys.getenv custom_pkg_loc) in
8582+ match Lazy.force custom_pkg_location with
8583+ | exception Not_found ->
8584+ begin
8585+ Bsb_log.error
8586+ "@{<error>Custom resolution of package %s does not exist in var %s @}@."
8587+ (Bsb_pkg_types.to_string package)
8588+ custom_pkg_loc;
8589+ Bsb_exception.package_not_found ~pkg:package ~json:None
8590+ end
8591+ | path when not (Sys.file_exists path) ->
8592+ begin
8593+ Bsb_log.error
8594+ "@{<error>Custom resolution of package %s does not exist on disk: %s=%s @}@."
8595+ (Bsb_pkg_types.to_string package)
8596+ custom_pkg_loc
8597+ path;
8598+ Bsb_exception.package_not_found ~pkg:package ~json:None
8599+ end
8600+ | path ->
85638601 begin
8564- Bsb_log.warn
8565- "@{<warning>Duplicated package:@} %a %s (chosen) vs %s in %s @."
8566- Bsb_pkg_types.print package x result cwd;
8567- end;
8568- x
8569-
8570-
8602+ Bsb_log.info
8603+ "@{<info>Custom Resolution of package %s in var %s found at %s@}@."
8604+ (Bsb_pkg_types.to_string package)
8605+ custom_pkg_loc
8606+ path;
8607+ path
8608+ end
8609+ end
8610+ else
8611+ match Coll.find_opt cache package with
8612+ | None ->
8613+ let result = resolve_bs_package_aux ~cwd package in
8614+ Bsb_log.info "@{<info>Package@} %a -> %s@." Bsb_pkg_types.print package result ;
8615+ Coll.add cache package result ;
8616+ result
8617+ | Some x
8618+ ->
8619+ let result = resolve_bs_package_aux ~cwd package in
8620+ if result <> x then
8621+ begin
8622+ Bsb_log.warn
8623+ "@{<warning>Duplicated package:@} %a %s (chosen) vs %s in %s @."
8624+ Bsb_pkg_types.print package x result cwd;
8625+ end;
8626+ x
85718627
85728628
85738629(** The package does not need to be a bspackage
0 commit comments