@@ -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
115123export 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 ;
0 commit comments