Skip to content

Commit

Permalink
add option for renv package cache in lsp et al (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
nateybear authored Oct 7, 2023
1 parent ad966f3 commit 42fd198
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
17 changes: 16 additions & 1 deletion R/help/getAliases.R
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
17 changes: 16 additions & 1 deletion R/help/helpServer.R
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
17 changes: 16 additions & 1 deletion R/languageServer.R
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/helpViewer/helpProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<boolean>('useRenvLibPath') ? 'TRUE' : 'FALSE'
},
};

Expand Down Expand Up @@ -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<boolean>('useRenvLibPath') ? 'TRUE' : 'FALSE'
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class LanguageService implements Disposable {
let client: LanguageClient;

const debug = config.get<boolean>('lsp.debug');
const useRenvLibPath = config.get<boolean>('useRenvLibPath') ?? false;
const rPath = await getRpath() || ''; // TODO: Abort gracefully
if (debug) {
console.log(`R path: ${rPath}`);
Expand All @@ -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<string>('lsp.lang');
if (lang !== '') {
Expand Down

0 comments on commit 42fd198

Please sign in to comment.