@@ -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 {
303297export 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