-
Notifications
You must be signed in to change notification settings - Fork 169
Contribute R path, home, etc. to terminals #14886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+268
−11
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce0d07f
contribute R to terminals
jmcphers 373ce44
Merge remote-tracking branch 'origin/main' into feature/terminal-cons…
jmcphers 1c20bd1
you know what, forget R_HOME
jmcphers 2b0e9ad
Merge remote-tracking branch 'origin/main' into feature/terminal-cons…
jmcphers 173ec3c
fix windows paths
jmcphers 785ec2d
align session activation semantics
jmcphers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (C) 2026 Posit Software, PBC. All rights reserved. | ||
| * Licensed under the Elastic License 2.0. See LICENSE.txt for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { dirname } from 'path'; | ||
| import { RMetadataExtra } from './r-installation'; | ||
|
|
||
| /** | ||
| * The kind of mutation to apply to a terminal environment variable. Mirrors the | ||
| * `replace`/`prepend`/`append` methods on VS Code's | ||
| * `EnvironmentVariableCollection`. | ||
| */ | ||
| export type TerminalEnvironmentAction = 'replace' | 'prepend' | 'append'; | ||
|
|
||
| /** | ||
| * A single mutation to apply to the contributed terminal environment. | ||
| */ | ||
| export interface TerminalEnvironmentMutation { | ||
| readonly action: TerminalEnvironmentAction; | ||
| readonly variable: string; | ||
| readonly value: string; | ||
| } | ||
|
|
||
| /** | ||
| * Compute the terminal environment variable mutations that make a terminal use | ||
| * the same R installation as the active console. | ||
| * | ||
| * Several extensions (Quarto Preview, Shiny Run App, etc.) start R in a terminal | ||
| * to perform background tasks. Without these mutations, those tasks run against | ||
| * the system default R rather than the version selected in Positron's console. | ||
| * | ||
| * When multiple consoles run different R versions, whichever is activated last | ||
| * wins; this ambiguity is expected and acceptable (see | ||
| * https://github.com/posit-dev/positron/issues/7403). | ||
| * | ||
| * @param metadataExtra Extra metadata for the active R installation. | ||
| * @param platform The platform to compute mutations for. Defaults to the | ||
| * current platform; overridable for testing. | ||
| * @returns The mutations to apply to the terminal environment collection. | ||
| */ | ||
| export function getRTerminalEnvironmentMutations( | ||
| metadataExtra: RMetadataExtra, | ||
| platform: NodeJS.Platform = process.platform | ||
| ): TerminalEnvironmentMutation[] { | ||
| const mutations: TerminalEnvironmentMutation[] = []; | ||
| const pathSeparator = platform === 'win32' ? ';' : ':'; | ||
|
|
||
| // Prepend the directory containing the selected R binary to PATH so that | ||
| // `R`, `Rscript`, and tools that shell out to them resolve to the version | ||
| // selected in the console rather than the system default. This is the | ||
| // primary mechanism by which the terminal's R matches the console's R, and | ||
| // mirrors how rig makes a selected R version available (symlinks on PATH). | ||
| if (metadataExtra.binpath) { | ||
| mutations.push({ | ||
| action: 'prepend', | ||
| variable: 'PATH', | ||
| value: dirname(metadataExtra.binpath) + pathSeparator, | ||
| }); | ||
| } | ||
|
|
||
| // Set R_HOME so tools that read it (rather than deriving it from the R | ||
| // binary) find the selected installation. R_HOME is R-specific, so setting | ||
| // it in the terminal does not affect unrelated programs. | ||
| if (metadataExtra.homepath) { | ||
| mutations.push({ | ||
| action: 'replace', | ||
| variable: 'R_HOME', | ||
| value: metadataExtra.homepath, | ||
| }); | ||
| } | ||
|
|
||
| // Point QUARTO_R at the directory containing Rscript so that `quarto render` | ||
| // (and the bundled Quarto extension) use the selected R version. Note that | ||
| // `scriptpath` is the full path to the Rscript binary (foo/bar/Rscript), but | ||
| // Quarto expects the directory (foo/bar). | ||
| if (metadataExtra.scriptpath) { | ||
| mutations.push({ | ||
| action: 'replace', | ||
| variable: 'QUARTO_R', | ||
| value: dirname(metadataExtra.scriptpath), | ||
| }); | ||
| } | ||
|
|
||
| // We intentionally do NOT set DYLD_LIBRARY_PATH (macOS) or LD_LIBRARY_PATH | ||
| // (Linux) here, even though the Ark kernel sets them. Those variables affect | ||
| // dynamic linking for *every* program run in the terminal, not just R, and | ||
| // can cause unrelated tools to load R's bundled copies of common libraries | ||
| // (libcurl, libz, etc.). R's own launcher scripts (`R`/`Rscript`) already | ||
| // configure their library paths, so the variables are unnecessary for R to | ||
| // work from the terminal. This mirrors rig, which scopes DYLD_LIBRARY_PATH to | ||
| // R's launcher script rather than exporting it to the shell. Ark needs the | ||
| // variables because it is a compiled binary that loads libR directly, | ||
| // bypassing the launcher scripts. | ||
|
|
||
| return mutations; | ||
| } |
116 changes: 116 additions & 0 deletions
116
extensions/positron-r/src/test/terminal-environment.unit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,116 @@ | ||||
| /*--------------------------------------------------------------------------------------------- | ||||
| * Copyright (C) 2026 Posit Software, PBC. All rights reserved. | ||||
| * Licensed under the Elastic License 2.0. See LICENSE.txt for license information. | ||||
| *--------------------------------------------------------------------------------------------*/ | ||||
|
|
||||
| import * as assert from 'assert'; | ||||
| import { RMetadataExtra } from '../r-installation'; | ||||
| import { getRTerminalEnvironmentMutations, TerminalEnvironmentMutation } from '../terminal-environment'; | ||||
|
|
||||
| /** | ||||
| * Build an RMetadataExtra with sensible defaults for the paths under test. | ||||
| */ | ||||
| function makeMetadataExtra(overrides: Partial<RMetadataExtra> = {}): RMetadataExtra { | ||||
| return { | ||||
| homepath: '/opt/R/4.4.0/lib/R', | ||||
| binpath: '/opt/R/4.4.0/bin/R', | ||||
| scriptpath: '/opt/R/4.4.0/bin/Rscript', | ||||
| current: false, | ||||
| default: false, | ||||
| reasonDiscovered: null, | ||||
| ...overrides, | ||||
| }; | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Find the mutation for a given variable, or undefined if none exists. | ||||
| */ | ||||
| function find(mutations: TerminalEnvironmentMutation[], variable: string): TerminalEnvironmentMutation | undefined { | ||||
| return mutations.find(m => m.variable === variable); | ||||
| } | ||||
|
|
||||
| suite('getRTerminalEnvironmentMutations', () => { | ||||
|
|
||||
| test('prepends the R binary directory to PATH (posix separator)', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations(makeMetadataExtra(), 'darwin'); | ||||
| const path = find(mutations, 'PATH'); | ||||
|
|
||||
| assert.ok(path, 'expected a PATH mutation'); | ||||
| assert.strictEqual(path!.action, 'prepend'); | ||||
| assert.strictEqual(path!.value, '/opt/R/4.4.0/bin:'); | ||||
| }); | ||||
|
|
||||
| test('prepends the R binary directory to PATH (windows separator)', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations( | ||||
| makeMetadataExtra({ binpath: 'C:/R/R-4.4.0/bin/x64/R.exe' }), | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in 173ec3c! |
||||
| 'win32' | ||||
| ); | ||||
| const path = find(mutations, 'PATH'); | ||||
|
|
||||
| assert.ok(path, 'expected a PATH mutation'); | ||||
| assert.strictEqual(path!.action, 'prepend'); | ||||
| assert.strictEqual(path!.value, 'C:/R/R-4.4.0/bin/x64;'); | ||||
| }); | ||||
|
|
||||
| test('replaces R_HOME with the home path', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations(makeMetadataExtra(), 'darwin'); | ||||
| const rHome = find(mutations, 'R_HOME'); | ||||
|
|
||||
| assert.ok(rHome, 'expected an R_HOME mutation'); | ||||
| assert.strictEqual(rHome!.action, 'replace'); | ||||
| assert.strictEqual(rHome!.value, '/opt/R/4.4.0/lib/R'); | ||||
| }); | ||||
|
|
||||
| test('sets QUARTO_R to the directory containing Rscript, not Rscript itself', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations(makeMetadataExtra(), 'darwin'); | ||||
| const quartoR = find(mutations, 'QUARTO_R'); | ||||
|
|
||||
| assert.ok(quartoR, 'expected a QUARTO_R mutation'); | ||||
| assert.strictEqual(quartoR!.action, 'replace'); | ||||
| assert.strictEqual(quartoR!.value, '/opt/R/4.4.0/bin'); | ||||
| }); | ||||
|
|
||||
| test('does not set library-path variables on macOS', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations(makeMetadataExtra(), 'darwin'); | ||||
|
|
||||
| assert.strictEqual(find(mutations, 'DYLD_LIBRARY_PATH'), undefined); | ||||
| assert.strictEqual(find(mutations, 'LD_LIBRARY_PATH'), undefined); | ||||
| }); | ||||
|
|
||||
| test('does not set library-path variables on Linux', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations(makeMetadataExtra(), 'linux'); | ||||
|
|
||||
| assert.strictEqual(find(mutations, 'LD_LIBRARY_PATH'), undefined); | ||||
| assert.strictEqual(find(mutations, 'DYLD_LIBRARY_PATH'), undefined); | ||||
| }); | ||||
|
|
||||
| test('omits PATH when there is no binary path', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations( | ||||
| makeMetadataExtra({ binpath: '' }), | ||||
| 'darwin' | ||||
| ); | ||||
|
|
||||
| assert.strictEqual(find(mutations, 'PATH'), undefined); | ||||
| // The other variables are still contributed. | ||||
| assert.ok(find(mutations, 'R_HOME')); | ||||
| assert.ok(find(mutations, 'QUARTO_R')); | ||||
| }); | ||||
|
|
||||
| test('omits QUARTO_R when there is no script path', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations( | ||||
| makeMetadataExtra({ scriptpath: '' }), | ||||
| 'darwin' | ||||
| ); | ||||
|
|
||||
| assert.strictEqual(find(mutations, 'QUARTO_R'), undefined); | ||||
| assert.ok(find(mutations, 'PATH')); | ||||
| assert.ok(find(mutations, 'R_HOME')); | ||||
| }); | ||||
|
|
||||
| test('contributes only the expected variables', () => { | ||||
| const mutations = getRTerminalEnvironmentMutations(makeMetadataExtra(), 'darwin'); | ||||
| const variables = mutations.map(m => m.variable).sort(); | ||||
|
|
||||
| assert.deepStrictEqual(variables, ['PATH', 'QUARTO_R', 'R_HOME']); | ||||
| }); | ||||
| }); | ||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to think about how this relates to the pre-existing handler for
onDidChangeForegroundSession()? Should they be combined? Or if not, should they look more similar?positron/extensions/positron-r/src/session-manager.ts
Line 110 in 86be3d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think they should be more similar, after evaluating them! I addressed this by adding a new event so that these codepaths agree on when we should be responding to changes. 785ec2d