diff --git a/CHANGELOG.md b/CHANGELOG.md index 95f81e5264..f6b1e55d5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ #### :house: Internal +- Editor: resolve @rescript/runtime via environment variable RESCRIPT_RUNTIME. https://github.com/rescript-lang/rescript/pull/8023 + # 12.0.0-rc.4 #### :boom: Breaking Change diff --git a/analysis/src/BuildSystem.ml b/analysis/src/BuildSystem.ml index 69eed6990b..de8f9c9bbe 100644 --- a/analysis/src/BuildSystem.ml +++ b/analysis/src/BuildSystem.ml @@ -5,19 +5,39 @@ let namespacedName namespace name = let ( /+ ) = Filename.concat +(* +Editor tooling can more accurately resolve the runtime path and will try and pass it via an environment variable. +Example path: "test-stdlib/node_modules/.pnpm/@rescript+runtime@12.0.0-rc.4/node_modules/@rescript/runtime" +*) + let getRuntimeDir rootPath = match !Cfg.isDocGenFromCompiler with | false -> ( - let result = - ModuleResolution.resolveNodeModulePath ~startPath:rootPath - "@rescript/runtime" - in - match result with - | Some path -> Some path - | None -> - let message = "@rescript/runtime could not be found" in - Log.log message; - None) + (* First check RESCRIPT_RUNTIME environment variable, like bsc does *) + match Sys.getenv_opt "RESCRIPT_RUNTIME" with + | Some envPath -> + if Debug.verbose () then + Printf.printf "[getRuntimeDir] Using RESCRIPT_RUNTIME=%s\n" envPath; + Some envPath + | None -> ( + let result = + ModuleResolution.resolveNodeModulePath ~startPath:rootPath + "@rescript/runtime" + in + match result with + | Some path -> + if Debug.verbose () then + Printf.printf "[getRuntimeDir] Resolved via node_modules: %s\n" path; + Some path + | None -> + let message = "@rescript/runtime could not be found" in + Log.log message; + if Debug.verbose () then + Printf.printf + "[getRuntimeDir] Failed to resolve @rescript/runtime from \ + rootPath=%s\n" + rootPath; + None)) | true -> Some rootPath let getLibBs path = Files.ifExists (path /+ "lib" /+ "bs")