diff --git a/analysis/src/BuildSystem.ml b/analysis/src/BuildSystem.ml index 02dff9bde..342acfb93 100644 --- a/analysis/src/BuildSystem.ml +++ b/analysis/src/BuildSystem.ml @@ -6,15 +6,18 @@ let namespacedName namespace name = let ( /+ ) = Filename.concat let getBsPlatformDir rootPath = - let result = - ModuleResolution.resolveNodeModulePath ~startPath:rootPath "rescript" - in - match result with - | Some path -> Some path - | None -> - let message = "rescript could not be found" in - Log.log message; - None + match !Cfg.isDocGenFromCompiler with + | false -> ( + let result = + ModuleResolution.resolveNodeModulePath ~startPath:rootPath "rescript" + in + match result with + | Some path -> Some path + | None -> + let message = "rescript could not be found" in + Log.log message; + None) + | true -> Some rootPath let getLibBs root = Files.ifExists (root /+ "lib" /+ "bs") diff --git a/analysis/src/Cfg.ml b/analysis/src/Cfg.ml index 38b3e9aa7..777d76a97 100644 --- a/analysis/src/Cfg.ml +++ b/analysis/src/Cfg.ml @@ -1,3 +1,5 @@ let supportsSnippets = ref false let debugFollowCtxPath = ref false + +let isDocGenFromCompiler = ref false diff --git a/analysis/src/Cli.ml b/analysis/src/Cli.ml index a96adbed5..dcb86bdd6 100644 --- a/analysis/src/Cli.ml +++ b/analysis/src/Cli.ml @@ -143,7 +143,13 @@ let main () = ~pos:(int_of_string line_start, int_of_string line_end) ~maxLength ~debug | [_; "codeLens"; path] -> Commands.codeLens ~path ~debug - | [_; "extractDocs"; path] -> DocExtraction.extractDocs ~path ~debug + | [_; "extractDocs"; path] -> + + let () = match Sys.getenv_opt "FROM_COMPILER" with + | Some("true") -> Cfg.isDocGenFromCompiler := true + | _ -> () in + + DocExtraction.extractDocs ~path ~debug | [_; "codeAction"; path; startLine; startCol; endLine; endCol; currentFile] -> Commands.codeAction ~path diff --git a/analysis/src/Packages.ml b/analysis/src/Packages.ml index b330eac99..0ac43bef8 100644 --- a/analysis/src/Packages.ml +++ b/analysis/src/Packages.ml @@ -32,7 +32,10 @@ let newBsPackage ~rootPath = let bsconfigJson = Filename.concat rootPath "bsconfig.json" in let parseRaw raw = - let libBs = BuildSystem.getLibBs rootPath in + let libBs = match !Cfg.isDocGenFromCompiler with + | true -> BuildSystem.getStdlib rootPath + | false -> BuildSystem.getLibBs rootPath + in match Json.parse raw with | Some config -> ( match FindFiles.findDependencyFiles rootPath config with diff --git a/tools/CHANGELOG.md b/tools/CHANGELOG.md index b132821ea..7674479dc 100644 --- a/tools/CHANGELOG.md +++ b/tools/CHANGELOG.md @@ -16,6 +16,11 @@ - Expose more `decode` functions. https://github.com/rescript-lang/rescript-vscode/pull/866 +#### :house: [Internal] + +- Add env var `FROM_COMPILER` to extract docstrings from compiler repo. https://github.com/rescript-lang/rescript-vscode/pull/868 + #### :bug: Bug Fix - Fix tagged variant for `Module` and add attr to interface files. https://github.com/rescript-lang/rescript-vscode/pull/866 +- Fix output truncate when run `rescript-tools doc path/to/file.res` in a separate process. https://github.com/rescript-lang/rescript-vscode/pull/868 diff --git a/tools/src/Cli.res b/tools/src/Cli.res index d4d72b4dd..0dfcc046d 100644 --- a/tools/src/Cli.res +++ b/tools/src/Cli.res @@ -11,13 +11,16 @@ module Buffer = { @send external toString: t => string = "toString" } +type processEnvOptions = {stdio?: string} type spawnSyncResult = { stdout: Buffer.t, stderr: Buffer.t, status: Js.Null.t, } + @module("child_process") -external spawnSync: (string, array) => spawnSyncResult = "spawnSync" +external spawnSync: (string, array, option) => spawnSyncResult = + "spawnSync" @val @scope("process") external exit: int => unit = "exit" @@ -68,29 +71,21 @@ switch args->Belt.List.fromArray { switch rest { | list{"-h" | "--help"} => logAndExit(~log=docHelp, ~code=0) | list{filePath} => - let spawn = spawnSync(analysisProdPath, ["extractDocs", filePath]) + let spawn = spawnSync(analysisProdPath, ["extractDocs", filePath], Some({stdio: "inherit"})) switch spawn.status->Js.Null.toOption { - | Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code) - | Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code) - | None => logAndExit(~log=`error: unexpected error to extract docs for ${filePath}`, ~code=1) + | Some(code) => exit(code) + | None => () } | _ => logAndExit(~log=docHelp, ~code=1) } | list{"reanalyze", ...rest} => let args = ["reanalyze"]->Js.Array2.concat(Belt.List.toArray(rest)) - let spawn = spawnSync(analysisProdPath, args) + let spawn = spawnSync(analysisProdPath, args, Some({stdio: "inherit"})) switch spawn.status->Js.Null.toOption { - | Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code) - | Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code) - | None => - logAndExit( - ~log=`error: unexpected error to run reanalyze with arguments: ${args->Js.Array2.joinWith( - " ", - )}`, - ~code=1, - ) + | Some(code) => exit(code) + | None => () } | list{"-h" | "--help"} => logAndExit(~log=help, ~code=0) | list{"-v" | "--version"} =>