From 1ff2675966eae7328789ef84a6cf6207148e1b2f Mon Sep 17 00:00:00 2001 From: eitsupi Date: Sun, 22 Nov 2026 15:20:43 +0000 Subject: [PATCH 1/2] fix(interactive): create a session from a picker for the folder it listed sessions for With no active editor, the Connect picker listed sessions with the first workspace folder's arf, but kept no resource. Choosing New persistent R session then read the active editor again, so a file opened in another folder while the picker was open decided the arf that was offered. The Connect picker and the execution target picker now resolve that folder when they open, and use it for both listing and creation. --- src/interactive/manager.ts | 17 +++++- src/test/suite/interactiveEditor.test.ts | 72 ++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/src/interactive/manager.ts b/src/interactive/manager.ts index 65e18e6da..44ab1748a 100644 --- a/src/interactive/manager.ts +++ b/src/interactive/manager.ts @@ -1254,6 +1254,15 @@ export class InteractiveManager * resource's, or else the first folder. Its arf setting applies, as its * directory does. */ + /** + * The resource that a picker lists and creates sessions for: `resource`, + * or else the folder that {@link sessionFolder} falls back to, resolved + * when the picker opens rather than once a choice is made. + */ + private pickerResource(resource?: vscode.Uri): vscode.Uri | undefined { + return resource ?? this.sessionFolder(); + } + private sessionFolder(resource?: vscode.Uri): vscode.Uri | undefined { return ( (resource && vscode.workspace.getWorkspaceFolder(resource)) ?? @@ -1400,8 +1409,9 @@ export class InteractiveManager } private async chooseExecutionTarget( - resource?: vscode.Uri, + code?: vscode.Uri, ): Promise { + const resource = this.pickerResource(code); const choices = this.sessionChoices(resource).then( (items) => [ @@ -1451,8 +1461,9 @@ export class InteractiveManager } private async pick(): Promise { - // Listing and creating use the same folder even if the editor changes meanwhile. - const resource = vscode.window.activeTextEditor?.document.uri; + // Listing and creating use the same folder even if the editor changes + // meanwhile, including the first folder when no editor is active. + const resource = this.pickerResource(vscode.window.activeTextEditor?.document.uri); // Passing a promise keeps the picker responsive while local agents are checked. const selected = await vscode.window.showQuickPick(this.sessionChoices(resource), { title: 'Connect to a persistent R session', diff --git a/src/test/suite/interactiveEditor.test.ts b/src/test/suite/interactiveEditor.test.ts index ffc176f48..58bb45913 100644 --- a/src/test/suite/interactiveEditor.test.ts +++ b/src/test/suite/interactiveEditor.test.ts @@ -807,6 +807,78 @@ suite('Interactive VS Code integration', function () { stubs.forEach((stub) => stub.restore()); } }); + test('a session created from Connect uses the folder that the picker listed for', async () => { + const folders = ['first', 'second'].map((name, index) => { + fs.mkdirSync(path.join(root, name), { recursive: true }); + return { uri: vscode.Uri.file(path.join(root, name)), name, index }; + }); + // Each folder sets an arf that does not exist, so creation offers to configure it. + const missing = folders.map((folder) => path.join(root, `${folder.name}-folder-arf`)); + const getConfiguration = vscode.workspace.getConfiguration.bind(vscode.workspace); + let active: vscode.TextEditor | undefined; + const sandbox = sinon.createSandbox(); + try { + sandbox.stub(vscode.workspace, 'workspaceFolders').value(folders); + sandbox + .stub(vscode.workspace, 'getWorkspaceFolder') + .callsFake((uri: vscode.Uri) => + folders.find( + (folder) => + uri.fsPath === folder.uri.fsPath || + uri.fsPath.startsWith(folder.uri.fsPath + path.sep), + ), + ); + sandbox + .stub(vscode.workspace, 'getConfiguration') + .callsFake((section?: string, scope?: vscode.ConfigurationScope | null) => { + const config = getConfiguration(section, scope); + const index = + section === 'r' && scope instanceof vscode.Uri + ? folders.findIndex( + (folder) => folder.uri.toString() === scope.toString(), + ) + : -1; + if (index < 0) { + return config; + } + // The configuration object is frozen, so a copy replaces `get`. + return { + ...config, + get: (key: string, fallback?: unknown) => + key === 'interactive.arfPath' + ? missing[index] + : config.get(key, fallback), + } as vscode.WorkspaceConfiguration; + }); + // No editor is active when the picker opens. + sandbox.stub(vscode.window, 'activeTextEditor').get(() => active); + const picker = sandbox.stub(vscode.window, 'showQuickPick'); + picker.onFirstCall().callsFake((async (items: unknown) => { + const choices = (await items) as (vscode.QuickPickItem & { create?: boolean })[]; + // While the picker is open, a file in the second folder becomes active. + active = { + document: { uri: vscode.Uri.file(path.join(folders[1].uri.fsPath, 'a.R')) }, + } as vscode.TextEditor; + return choices.find((choice) => choice.create); + }) as never); + picker.onSecondCall().resolves(undefined); + await vscode.commands.executeCommand('r.interactive.connect'); + sinon.assert.calledTwice(picker); + const providers = picker.secondCall.args[0] as (vscode.QuickPickItem & { + value: string; + })[]; + assert.deepStrictEqual( + providers.map((item) => item.value), + ['configure'], + ); + assert.ok(providers[0].detail?.includes(missing[0]), providers[0].detail); + } finally { + sandbox.restore(); + folders.forEach((folder) => + fs.rmSync(folder.uri.fsPath, { recursive: true, force: true }), + ); + } + }); test('usable custom arf is offered, but removal while naming a session prevents launch', async function () { if (process.platform === 'win32') { this.skip(); From bcc9451ceeeccf38c8aab202b4b06c594126fef5 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Sun, 22 Nov 2026 15:57:51 +0000 Subject: [PATCH 2/2] test(interactive): check that the Connect picker lists sessions with the folder it creates them for The test now also checks that listing reported the first folder's arf, so a change that lists sessions for another folder than creation uses fails it. The documentation of sessionFolder() is back above that method, after pickerResource() was inserted below it. --- src/interactive/manager.ts | 10 +++++----- src/test/suite/interactiveEditor.test.ts | 9 ++++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/interactive/manager.ts b/src/interactive/manager.ts index 44ab1748a..bfa4b5155 100644 --- a/src/interactive/manager.ts +++ b/src/interactive/manager.ts @@ -1249,11 +1249,6 @@ export class InteractiveManager }; } - /** - * The workspace folder that a session is created or listed for: the - * resource's, or else the first folder. Its arf setting applies, as its - * directory does. - */ /** * The resource that a picker lists and creates sessions for: `resource`, * or else the folder that {@link sessionFolder} falls back to, resolved @@ -1263,6 +1258,11 @@ export class InteractiveManager return resource ?? this.sessionFolder(); } + /** + * The workspace folder that a session is created or listed for: the + * resource's, or else the first folder. Its arf setting applies, as its + * directory does. + */ private sessionFolder(resource?: vscode.Uri): vscode.Uri | undefined { return ( (resource && vscode.workspace.getWorkspaceFolder(resource)) ?? diff --git a/src/test/suite/interactiveEditor.test.ts b/src/test/suite/interactiveEditor.test.ts index 58bb45913..f5cc670ab 100644 --- a/src/test/suite/interactiveEditor.test.ts +++ b/src/test/suite/interactiveEditor.test.ts @@ -853,8 +853,14 @@ suite('Interactive VS Code integration', function () { // No editor is active when the picker opens. sandbox.stub(vscode.window, 'activeTextEditor').get(() => active); const picker = sandbox.stub(vscode.window, 'showQuickPick'); + let listing: string | undefined; picker.onFirstCall().callsFake((async (items: unknown) => { - const choices = (await items) as (vscode.QuickPickItem & { create?: boolean })[]; + const choices = (await items) as (vscode.QuickPickItem & { + create?: boolean; + configureArf?: boolean; + })[]; + // Listing reports the arf it checked, as creation offers it. + listing = choices.find((choice) => choice.configureArf)?.detail; // While the picker is open, a file in the second folder becomes active. active = { document: { uri: vscode.Uri.file(path.join(folders[1].uri.fsPath, 'a.R')) }, @@ -864,6 +870,7 @@ suite('Interactive VS Code integration', function () { picker.onSecondCall().resolves(undefined); await vscode.commands.executeCommand('r.interactive.connect'); sinon.assert.calledTwice(picker); + assert.ok(listing?.includes(missing[0]), listing); const providers = picker.secondCall.args[0] as (vscode.QuickPickItem & { value: string; })[];