Skip to content

Commit d1ddf51

Browse files
committed
Update install_sess
1 parent 9c1d75b commit d1ddf51

2 files changed

Lines changed: 35 additions & 22 deletions

File tree

R/install_sess.R

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
local({
22
args <- commandArgs(trailingOnly = TRUE)
3-
if (length(args) < 2) {
4-
stop("Missing arguments: pkg_path and repo")
3+
pkg_path <- Sys.getenv("VSCODE_R_SESS_PKG_PATH", unset = "")
4+
if (!nzchar(pkg_path) && length(args) >= 1) {
5+
pkg_path <- args[1]
6+
}
7+
8+
if (!nzchar(pkg_path)) {
9+
stop("Missing pkg_path (set VSCODE_R_SESS_PKG_PATH or pass as first command arg)")
10+
}
11+
12+
repo <- Sys.getenv("VSCODE_R_SESS_REPO", unset = "")
13+
if (!nzchar(repo) && length(args) >= 2) {
14+
repo <- args[2]
15+
}
16+
if (!nzchar(repo)) {
17+
repo <- getOption("repos")[["CRAN"]]
18+
}
19+
if (is.na(repo) || identical(repo, "@CRAN@")) {
20+
repo <- ""
521
}
6-
pkg_path <- args[1]
7-
repo <- args[2]
822

923
if (!file.exists(file.path(pkg_path, "DESCRIPTION"))) {
1024
stop(paste("DESCRIPTION file not found in", pkg_path))
@@ -23,7 +37,11 @@ local({
2337

2438
if (length(deps) > 0) {
2539
message("Installing dependencies: ", paste(deps, collapse = ", "))
26-
install.packages(deps, repos = repo)
40+
if (nzchar(repo)) {
41+
install.packages(deps, repos = repo)
42+
} else {
43+
install.packages(deps)
44+
}
2745
}
2846

2947
message("Installing sess package from: ", pkg_path)

src/session.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ export async function getGlobalPipePath(): Promise<string> {
201201

202202
for (let i = 0; i < lines.length - 1; i++) {
203203
const line = lines[i].trim();
204-
if (!line) continue;
204+
if (!line) {
205+
continue;
206+
}
205207
void (async () => {
206208
try {
207209
const message = JSON.parse(line) as Record<string, unknown>;
@@ -269,30 +271,22 @@ function getAttachSessionScriptPath(pipePath: string): string {
269271
return path.join(tmpDir(), `${scriptBase}.R`);
270272
}
271273

272-
function buildAttachSessionScript(pipePath: string, sessPath: string): string {
274+
function buildAttachSessionScript(pipePath: string, sessPath: string, installSessScriptPath: string): string {
273275
return [
274276
'local({',
275277
` pipe_path <- ${asRStringLiteral(pipePath)}`,
276278
` sess_src <- ${asRStringLiteral(sessPath)}`,
279+
` install_sess_script <- ${asRStringLiteral(installSessScriptPath)}`,
277280
' bundled_version <- tryCatch(read.dcf(file.path(sess_src, "DESCRIPTION"))[1, "Version"], error = function(e) NA_character_)',
278281
' installed_version <- suppressWarnings(tryCatch(as.character(utils::packageVersion("sess")), error = function(e) NA_character_))',
279282
' needs_install <- is.na(installed_version) || (!is.na(bundled_version) && utils::compareVersion(installed_version, bundled_version) < 0)',
280283
' if (needs_install) {',
281-
' lib_candidates <- unique(c(.libPaths(), Sys.getenv("R_LIBS_USER", unset = "")))',
282-
' lib_candidates <- lib_candidates[nzchar(lib_candidates)]',
283-
' writable <- lib_candidates[vapply(lib_candidates, function(lib) {',
284-
' if (!dir.exists(lib)) {',
285-
' dir.create(lib, recursive = TRUE, showWarnings = FALSE)',
286-
' }',
287-
' file.access(lib, 2) == 0',
288-
' }, logical(1))]',
289-
' if (length(writable) < 1) {',
290-
' message("[vscode-R] Could not find a writable library path for this terminal.")',
291-
' message("[vscode-R] Install the bundled sess package manually:")',
292-
' message(sprintf("install.packages(%s, repos = NULL, type = \\"source\\")", shQuote(sess_src)))',
293-
' stop("No writable R library path available for installing sess")',
284+
' if (!file.exists(install_sess_script)) {',
285+
' stop(sprintf("install_sess.R not found: %s", install_sess_script))',
294286
' }',
295-
' install.packages(sess_src, repos = NULL, type = "source", lib = writable[[1]])',
287+
' Sys.setenv(VSCODE_R_SESS_PKG_PATH = sess_src)',
288+
' on.exit(Sys.unsetenv(c("VSCODE_R_SESS_PKG_PATH", "VSCODE_R_SESS_REPO")), add = TRUE)',
289+
' source(install_sess_script, local = TRUE)',
296290
' }',
297291
' sess::connect(pipe_path = pipe_path)',
298292
'})',
@@ -303,8 +297,9 @@ function buildAttachSessionScript(pipePath: string, sessPath: string): string {
303297
export async function getAttachSessionCommand(): Promise<string> {
304298
const pipePath = await getGlobalPipePath();
305299
const sessPath = extensionContext.asAbsolutePath('sess').replace(/\\/g, '/');
300+
const installSessScriptPath = extensionContext.asAbsolutePath(path.join('R', 'install_sess.R')).replace(/\\/g, '/');
306301
const scriptPath = getAttachSessionScriptPath(pipePath);
307-
await fs.writeFile(scriptPath, buildAttachSessionScript(pipePath, sessPath), { encoding: 'utf-8' });
302+
await fs.writeFile(scriptPath, buildAttachSessionScript(pipePath, sessPath, installSessScriptPath), { encoding: 'utf-8' });
308303
attachSessionScriptPath = scriptPath;
309304

310305
return `source(${asRStringLiteral(scriptPath)})`;

0 commit comments

Comments
 (0)