Skip to content

Commit

Permalink
Show error when selecting a bad installation
Browse files Browse the repository at this point in the history
Error is triggered when selecting an R installation that cannot be opened
  • Loading branch information
ElianHugh committed Jul 19, 2022
1 parent 37a3a09 commit 0fd7424
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/executables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export class RExecutableManager {
}
}),
this.executableService,
this.statusBar,
this.quickPick
this.statusBar
);
this.reload();
}
Expand Down Expand Up @@ -95,7 +94,7 @@ export class RExecutableManager {
export function validateRExecutablePath(execPath: string): boolean {
try {
const basename = process.platform === 'win32' ? 'R.exe' : 'R';
fs.accessSync(execPath, fs.constants.X_OK);
fs.accessSync(execPath, fs.constants.X_OK && fs.constants.R_OK);
return (path.basename(execPath) === basename);
} catch (error) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/executables/service/locator/unix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class UnixExecLocator extends AbstractLocatorService {
new Set([
...this.getPathFromDirs(),
...this.getPathFromEnv(),
... this.getPathFromConda()
...this.getPathFromConda()
])
));
this.emitter.fire(this._executablePaths);
Expand Down
6 changes: 3 additions & 3 deletions src/executables/service/locator/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import os = require('os');
import path = require('path');
import winreg = require('winreg');
import { getUniquePaths, AbstractLocatorService } from './shared';
import { validateRExecutablePath } from '../..';


const WindowsKnownPaths: string[] = [];

Expand Down Expand Up @@ -88,11 +88,11 @@ export class WindowsExecLocator extends AbstractLocatorService {
const i386 = `${rPath}\\${dir}\\bin\\i386\\R.exe`;
const x64 = `${rPath}\\${dir}\\bin\\x64\\R.exe`;

if (validateRExecutablePath(i386)) {
if (fs.existsSync(i386)) {
execPaths.push(i386);
}

if (validateRExecutablePath(x64)) {
if (fs.existsSync(x64)) {
execPaths.push(x64);
}
}
Expand Down
23 changes: 14 additions & 9 deletions src/executables/ui/quickpick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { config, getCurrentWorkspaceFolder, getRPathConfigEntry, isMultiRoot } f
import { isVirtual, ExecutableType } from '../service';
import { RExecutableService } from '../service';
import { getRenvVersion } from '../virtual';
import { extensionContext } from '../../extension';

class ExecutableQuickPickItem implements vscode.QuickPickItem {
public recommended: boolean;
Expand Down Expand Up @@ -54,18 +55,16 @@ enum PathQuickPickMenu {
configuration = '$(settings-gear) Configuration path'
}

export class ExecutableQuickPick implements vscode.Disposable {

export class ExecutableQuickPick {
private readonly service: RExecutableService;
private quickpick: vscode.QuickPick<vscode.QuickPickItem>;
private quickpick: vscode.QuickPick<vscode.QuickPickItem | ExecutableQuickPickItem>;
private currentFolder: vscode.WorkspaceFolder;

public constructor(service: RExecutableService) {
this.service = service;
this.currentFolder = getCurrentWorkspaceFolder();
}

public dispose(): void {
this.quickpick.dispose();
extensionContext.subscriptions.push(this.quickpick);
}

private setItems(): void {
Expand Down Expand Up @@ -178,7 +177,7 @@ export class ExecutableQuickPick implements vscode.Disposable {
this.quickpick.hide();
}
});
this.quickpick.onDidChangeSelection((items: vscode.QuickPickItem[]) => {
this.quickpick.onDidChangeSelection((items: vscode.QuickPickItem[] | ExecutableQuickPickItem[]) => {
const qpItem = items[0];
if (qpItem.label) {
switch (qpItem.label) {
Expand All @@ -189,7 +188,7 @@ export class ExecutableQuickPick implements vscode.Disposable {
canSelectMany: false,
title: ' R executable file'
};
void vscode.window.showOpenDialog(opts).then((epath) => {
void vscode.window.showOpenDialog(opts).then((epath: vscode.Uri[]) => {
if (epath) {
const execPath = path.normalize(epath?.[0].fsPath);
if (execPath && validateRExecutablePath(execPath)) {
Expand All @@ -215,7 +214,13 @@ export class ExecutableQuickPick implements vscode.Disposable {
break;
}
default: {
this.service.setWorkspaceExecutable(this.currentFolder?.uri?.fsPath, (qpItem as ExecutableQuickPickItem).executable);
const executable = (qpItem as ExecutableQuickPickItem).executable;
if (executable?.rVersion) {
this.service.setWorkspaceExecutable(this.currentFolder?.uri?.fsPath, executable);
} else {
void vscode.window.showErrorMessage(ExecutableNotifications.badInstallation);
this.service.setWorkspaceExecutable(this.currentFolder?.uri?.fsPath, undefined);
}
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/executables/ui/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum ExecutableNotifications {
badFolder = 'Supplied R executable path is not a valid R path.',
badConfig = 'Configured path is not a valid R executable path.'
badConfig = 'Configured path is not a valid R executable path.',
badInstallation = 'Supplied R executable cannot be launched on this operating system.'
}

0 comments on commit 0fd7424

Please sign in to comment.