Skip to content

Commit 90bbd9a

Browse files
Add R path to debug config
1 parent 1d0082d commit 90bbd9a

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@
207207
"default": true,
208208
"markdownDescription": "Whether to allow debugging in the global workspace after running the function/file."
209209
},
210+
"rPath": {
211+
"type": "string",
212+
"markdownDescription": "Path to an R executable. Set to the empty string to use the path from the settings.",
213+
"default": ""
214+
},
210215
"workingDirectory": {
211216
"type": "string",
212217
"markdownDescription": "Absolute path to a directory. Is switched to using `setwd()` after launching R.",

src/debugConfig.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { HelpPanel } from './rExtensionApi';
1212

1313
import * as fs from 'fs';
1414
import * as path from 'path';
15+
import { getRpathFromConfig } from './utils';
1516

1617

1718
export class DebugAdapterDescriptorFactory implements vscode.DebugAdapterDescriptorFactory {
@@ -214,8 +215,10 @@ export class DebugConfigurationResolver implements vscode.DebugConfigurationProv
214215
config.debugMode ||= (docValid ? 'file' : 'workspace');
215216
config.allowGlobalDebugging ??= true;
216217

217-
// fill custom capabilities/socket info
218+
// fill custom capabilities/socket info/rPath
218219
if(config.request === 'launch'){
220+
// if not specified, set rPath from config
221+
config.rPath ||= getRpathFromConfig();
219222
// capabilities that are always true for this extension:
220223
config.supportsStdoutReading = true;
221224
config.supportsWriteToStdinEvent = true;

src/debugProtocolModifications.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export interface DebugConfiguration extends vsCode.DebugConfiguration {
6464

6565
export interface LaunchConfiguration extends DebugConfiguration {
6666
request: 'launch';
67+
rPath?: string;
6768
commandLineArgs?: string[];
6869
env?: {
6970
[key: string]: string;

src/utils.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function getRfromEnvPath(platform: string) {
5050
return '';
5151
}
5252

53-
export async function getRpathFromSystem(): Promise<string> {
53+
async function getRpathFromSystem(): Promise<string> {
5454

5555
let rpath = '';
5656
const platform: string = process.platform;
@@ -75,41 +75,49 @@ export async function getRpathFromSystem(): Promise<string> {
7575
return rpath;
7676
}
7777

78-
export async function getRpath(quote: boolean=false, overwriteConfig?: string): Promise<string> {
79-
let rpath: string | undefined = undefined;
80-
78+
export function getRpathFromConfig(): string | undefined {
79+
const platform: string = process.platform;
8180
const configEntry = (
82-
process.platform === 'win32' ? 'rpath.windows' :
83-
process.platform === 'darwin' ? 'rpath.mac' :
81+
platform === 'win32' ? 'rpath.windows' :
82+
platform === 'darwin' ? 'rpath.mac' :
8483
'rpath.linux'
8584
);
85+
return config(false).get<string>(configEntry);
86+
}
8687

87-
// try the config entry specified in the function arg:
88-
if(overwriteConfig){
89-
rpath = config().get<string>(overwriteConfig);
88+
export function quoteRPathIfNeeded(rpath: string): string {
89+
if (/^'.* .*'$/.exec(rpath) || /^".* .*"$/.exec(rpath)) {
90+
// already quoted
91+
return rpath;
92+
} else if (/.* .*/.exec(rpath)) {
93+
// contains spaces, add quotes
94+
if (process.platform === 'win32') {
95+
return `"${rpath}"`;
96+
} else {
97+
return `'${rpath}'`;
98+
}
99+
} else {
100+
// no spaces, no quotes needed
101+
return rpath;
90102
}
103+
}
91104

105+
export async function getRpath(): Promise<string> {
106+
let rpath: string | undefined;
107+
92108
// try the os-specific config entry for the rpath:
93-
rpath ||= config(false).get<string>(configEntry);
109+
rpath = getRpathFromConfig();
94110

95111
// read from path/registry:
96112
rpath ||= await getRpathFromSystem();
97113

98-
// represent all invalid paths (undefined, '', null) as '':
99-
rpath ||= '';
100-
101114
if(!rpath){
102115
// inform user about missing R path:
103-
void vscode.window.showErrorMessage(`${process.platform} can't use R`);
104-
} else if(quote && /^[^'"].* .*[^'"]$/.exec(rpath)){
105-
// if requested and rpath contains spaces, add quotes:
106-
rpath = `"${rpath}"`;
107-
} else if(process.platform === 'win32' && /^'.* .*'$/.exec(rpath)){
108-
// replace single quotes with double quotes on windows
109-
rpath = rpath.replace(/^'(.*)'$/, '"$1"');
116+
void vscode.window.showErrorMessage(`No R executable found. Please set the path in the settings r.rPath.xxx!`);
110117
}
111118

112-
return rpath;
119+
// represent all invalid paths (undefined, '', null) as '':
120+
return rpath || '';
113121
}
114122

115123
export function getPortNumber(server?: net.Server): number {
@@ -131,10 +139,13 @@ export async function getRStartupArguments(launchConfig: {
131139
env?: {[key: string]: string};
132140
commandLineArgs?: string[];
133141
launchDirectory?: string;
142+
rPath?: string;
134143
} = {}): Promise<RStartupArguments> {
135144
const platform: string = process.platform;
136145

137-
const rpath = await getRpath(true);
146+
let rPath = launchConfig.rPath;
147+
rPath ||= await getRpath();
148+
rPath = quoteRPathIfNeeded(rPath);
138149

139150
const rArgs: string[] = [
140151
'--quiet',
@@ -148,13 +159,13 @@ export async function getRStartupArguments(launchConfig: {
148159
rArgs.push(...(launchConfig.commandLineArgs || []));
149160

150161
const ret: RStartupArguments = {
151-
path: rpath,
162+
path: rPath,
152163
args: rArgs,
153164
cwd: launchConfig.launchDirectory,
154165
env: launchConfig.env
155166
};
156167

157-
if(rpath === ''){
168+
if(rPath === ''){
158169
void vscode.window.showErrorMessage(`${process.platform} can't find R`);
159170
}
160171
return ret;

test/R/.vscode/launch.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
"workingDirectory": "${workspaceFolder}",
1313
"allowGlobalDebugging": true,
1414
"env": {
15-
"abc": "123"
16-
}
15+
"abc": "123",
16+
"dcUserProfile": "${env:USERPROFILE}",
17+
"dcUserHome": "${userHome}",
18+
"dcRPath": "${userHome}/../../Program Files/R/R-4.3.3/bin/x64/R.exe",
19+
},
20+
"rPath": "${userHome}/../../Program Files/R/R-4.4.3/bin/x64/R.exe",
1721
},
1822
{
1923
"type": "R-Debugger",

0 commit comments

Comments
 (0)