diff --git a/R/help/getAliases.R b/R/help/getAliases.R index 153137d9a..d76e67c3a 100644 --- a/R/help/getAliases.R +++ b/R/help/getAliases.R @@ -1,9 +1,24 @@ +.paths <- .libPaths() + add_lib_paths <- Sys.getenv("VSCR_LIB_PATHS") if (nzchar(add_lib_paths)) { add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]] - .libPaths(c(.libPaths(), add_lib_paths)) + .paths <- c(.paths, add_lib_paths) } +use_renv_lib_path <- Sys.getenv("VSCR_USE_RENV_LIB_PATH") +use_renv_lib_path <- if (nzchar(use_renv_lib_path)) as.logical(use_renv_lib_path) else FALSE +if (use_renv_lib_path) { + if (requireNamespace("renv", quietly = TRUE)) { + .paths <- c(.paths, renv::paths$cache()) + } else { + warning("renv package is not installed. Please install renv to use renv library path.") + } +} + +.libPaths(.paths) +message("R library paths: ", paste(.libPaths(), collapse = "\n")) + loadNamespace("jsonlite") ip <- installed.packages() diff --git a/R/help/helpServer.R b/R/help/helpServer.R index 5593f1126..006dd72ad 100644 --- a/R/help/helpServer.R +++ b/R/help/helpServer.R @@ -1,9 +1,24 @@ +.paths <- .libPaths() + add_lib_paths <- Sys.getenv("VSCR_LIB_PATHS") if (nzchar(add_lib_paths)) { add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]] - .libPaths(c(.libPaths(), add_lib_paths)) + .paths <- c(.paths, add_lib_paths) +} + +use_renv_lib_path <- Sys.getenv("VSCR_USE_RENV_LIB_PATH") +use_renv_lib_path <- if (nzchar(use_renv_lib_path)) as.logical(use_renv_lib_path) else FALSE +if (use_renv_lib_path) { + if (requireNamespace("renv", quietly = TRUE)) { + .paths <- c(.paths, renv::paths$cache()) + } else { + warning("renv package is not installed. Please install renv to use renv library path.") + } } +.libPaths(.paths) +message("R library paths: ", paste(.libPaths(), collapse = "\n")) + lim <- Sys.getenv("VSCR_LIM") NEW_PACKAGE_STRING <- "NEW_PACKAGES" diff --git a/R/languageServer.R b/R/languageServer.R index 2d98b2596..88030cb81 100644 --- a/R/languageServer.R +++ b/R/languageServer.R @@ -1,9 +1,24 @@ +.paths <- .libPaths() + add_lib_paths <- Sys.getenv("VSCR_LIB_PATHS") if (nzchar(add_lib_paths)) { add_lib_paths <- strsplit(add_lib_paths, "\n", fixed = TRUE)[[1L]] - .libPaths(c(.libPaths(), add_lib_paths)) + .paths <- c(.paths, add_lib_paths) +} + +use_renv_lib_path <- Sys.getenv("VSCR_USE_RENV_LIB_PATH") +use_renv_lib_path <- if (nzchar(use_renv_lib_path)) as.logical(use_renv_lib_path) else FALSE +if (use_renv_lib_path) { + if (requireNamespace("renv", quietly = TRUE)) { + .paths <- c(.paths, renv::paths$cache()) + } else { + warning("renv package is not installed. Please install renv to use renv library path.") + } } +.libPaths(.paths) +message("R library paths: ", paste(.libPaths(), collapse = "\n")) + if (!requireNamespace("languageserver", quietly = TRUE)) { q(save = "no", status = 10) } diff --git a/package.json b/package.json index 23c830459..630e59bd1 100644 --- a/package.json +++ b/package.json @@ -1468,6 +1468,11 @@ "default": [], "markdownDescription": "Additional library paths to launch R background processes (R languageserver, help server, etc.). These paths will be appended to `.libPaths()` on process startup. It could be useful for projects with [renv](https://rstudio.github.io/renv/index.html) enabled." }, + "r.useRenvLibPath" : { + "type": "boolean", + "default": false, + "markdownDescription": "Use renv library paths to launch R background processes (R languageserver, help server, etc.)." + }, "r.lsp.enabled": { "type": "boolean", "default": true, diff --git a/src/helpViewer/helpProvider.ts b/src/helpViewer/helpProvider.ts index 2ff391a7b..20bcac9de 100644 --- a/src/helpViewer/helpProvider.ts +++ b/src/helpViewer/helpProvider.ts @@ -4,7 +4,7 @@ import * as cp from 'child_process'; import * as rHelp from '.'; import { extensionContext } from '../extension'; -import { catchAsError, DisposableProcess, getRLibPaths, spawn, spawnAsync } from '../util'; +import { catchAsError, config, DisposableProcess, getRLibPaths, spawn, spawnAsync } from '../util'; export interface RHelpProviderOptions { // path of the R executable @@ -62,7 +62,8 @@ export class HelpProvider { env: { ...process.env, VSCR_LIB_PATHS: getRLibPaths(), - VSCR_LIM: lim + VSCR_LIM: lim, + VSCR_USE_RENV_LIB_PATH: config().get('useRenvLibPath') ? 'TRUE' : 'FALSE' }, }; @@ -251,7 +252,8 @@ export class AliasProvider { env: { ...process.env, VSCR_LIB_PATHS: getRLibPaths(), - VSCR_LIM: lim + VSCR_LIM: lim, + VSCR_USE_RENV_LIB_PATH: config().get('useRenvLibPath') ? 'TRUE' : 'FALSE' } }; diff --git a/src/languageService.ts b/src/languageService.ts index 661cabf37..4f893ea98 100644 --- a/src/languageService.ts +++ b/src/languageService.ts @@ -59,6 +59,7 @@ export class LanguageService implements Disposable { let client: LanguageClient; const debug = config.get('lsp.debug'); + const useRenvLibPath = config.get('useRenvLibPath') ?? false; const rPath = await getRpath() || ''; // TODO: Abort gracefully if (debug) { console.log(`R path: ${rPath}`); @@ -67,6 +68,7 @@ export class LanguageService implements Disposable { const env = Object.create(process.env) as NodeJS.ProcessEnv; env.VSCR_LSP_DEBUG = debug ? 'TRUE' : 'FALSE'; env.VSCR_LIB_PATHS = getRLibPaths(); + env.VSCR_USE_RENV_LIB_PATH = useRenvLibPath ? 'TRUE' : 'FALSE'; const lang = config.get('lsp.lang'); if (lang !== '') {