Skip to content
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

ensure a non-existant dotnet always returns something actionable #1101

Merged
merged 2 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/dotnet-interactive-vscode/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "run tests",
"type": "npm",
"script": "test",
"group": "test"
}
]
}
11 changes: 10 additions & 1 deletion src/dotnet-interactive-vscode/src/tests/unit/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from 'chai';
import { NotebookCellDisplayOutput, NotebookCellErrorOutput, NotebookCellTextOutput } from 'dotnet-interactive-vscode-interfaces/out/contracts';
import { isDisplayOutput, isErrorOutput, isTextOutput, reshapeOutputValueForVsCode } from 'dotnet-interactive-vscode-interfaces/out/utilities';
import { requiredKernelspecData } from '../../ipynbUtilities';
import { debounce, isDotNetKernelPreferred, parse, processArguments, stringify } from '../../utilities';
import { debounce, executeSafe, isDotNetKernelPreferred, parse, processArguments, stringify } from '../../utilities';

import * as notebook from 'dotnet-interactive-vscode-interfaces/out/notebook';

Expand Down Expand Up @@ -296,4 +296,13 @@ describe('Miscellaneous tests', () => {
expect(reshaped).to.equal('some error message');
});
});

it('executing a non-existent process still returns', async () => {
const result = await executeSafe('this-is-a-command-that-will-fail', []);
expect(result).to.deep.equal({
code: -1,
output: '',
error: 'Error: spawn this-is-a-command-that-will-fail ENOENT',
});
});
});
37 changes: 37 additions & 0 deletions src/dotnet-interactive-vscode/src/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import * as cp from 'child_process';
import * as path from 'path';
import { InstallInteractiveArgs, ProcessStart } from "./interfaces";
import { Uri } from 'dotnet-interactive-vscode-interfaces/out/notebook';

export function executeSafe(command: string, args: Array<string>, workingDirectory?: string | undefined): Promise<{ code: number, output: string, error: string }> {
return new Promise<{ code: number, output: string, error: string }>(resolve => {
try {
let output = '';
let error = '';

function exitOrClose(code: number, _signal: string) {
resolve({
code,
output: output.trim(),
error: error.trim(),
});
}

let childProcess = cp.spawn(command, args, { cwd: workingDirectory });
childProcess.stdout.on('data', data => output += data);
childProcess.stderr.on('data', data => error += data);
childProcess.on('error', err => {
resolve({
code: -1,
output: '',
error: '' + err,
});
});
childProcess.on('close', exitOrClose);
childProcess.on('exit', exitOrClose);
} catch (err) {
resolve({
code: -1,
output: '',
error: '' + err,
});
}
});
}

export function processArguments(template: { args: Array<string>, workingDirectory: string }, notebookPath: string, fallbackWorkingDirectory: string, dotnetPath: string, globalStoragePath: string): ProcessStart {
let workingDirectory = path.parse(notebookPath).dir;
if (workingDirectory === '') {
Expand Down
29 changes: 5 additions & 24 deletions src/dotnet-interactive-vscode/src/vscode/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import * as vscode from 'vscode';
import * as cp from 'child_process';
import * as path from 'path';
import { acquireDotnetInteractive } from '../acquisition';
import { InstallInteractiveArgs, InteractiveLaunchOptions } from '../interfaces';
Expand All @@ -11,7 +10,7 @@ import { getEol, isUnsavedNotebook } from './vscodeUtilities';
import { toNotebookDocument } from './notebookContentProvider';
import { KernelId, updateCellMetadata } from './notebookKernel';
import { setGlobalDotnetPath } from './extension';
import { computeToolInstallArguments } from '../utilities';
import { computeToolInstallArguments, executeSafe } from '../utilities';

export function registerAcquisitionCommands(context: vscode.ExtensionContext) {
const config = vscode.workspace.getConfiguration('dotnet-interactive');
Expand Down Expand Up @@ -41,7 +40,7 @@ export function registerAcquisitionCommands(context: vscode.ExtensionContext) {
'uninstall',
'Microsoft.dotnet-interactive'
];
await execute(args.dotnetPath, uninstallArgs, globalStoragePath);
await executeSafe(args.dotnetPath, uninstallArgs, globalStoragePath);

let toolArgs = [
'tool',
Expand All @@ -56,7 +55,7 @@ export function registerAcquisitionCommands(context: vscode.ExtensionContext) {
}

return new Promise(async (resolve, reject) => {
const result = await execute(args.dotnetPath, toolArgs, globalStoragePath);
const result = await executeSafe(args.dotnetPath, toolArgs, globalStoragePath);
if (result.code === 0) {
resolve();
} else {
Expand Down Expand Up @@ -204,7 +203,7 @@ async function switchToInteractiveKernel() {
// callbacks used to install interactive tool

async function getInteractiveVersion(dotnetPath: string, globalStoragePath: string): Promise<string | undefined> {
const result = await execute(dotnetPath, ['tool', 'run', 'dotnet-interactive', '--', '--version'], globalStoragePath);
const result = await executeSafe(dotnetPath, ['tool', 'run', 'dotnet-interactive', '--', '--version'], globalStoragePath);
if (result.code === 0) {
return result.output;
}
Expand All @@ -213,26 +212,8 @@ async function getInteractiveVersion(dotnetPath: string, globalStoragePath: stri
}

async function createToolManifest(dotnetPath: string, globalStoragePath: string): Promise<void> {
const result = await execute(dotnetPath, ['new', 'tool-manifest'], globalStoragePath);
const result = await executeSafe(dotnetPath, ['new', 'tool-manifest'], globalStoragePath);
if (result.code !== 0) {
throw new Error(`Unable to create local tool manifest. Command failed with code ${result.code}.\n\nSTDOUT:\n${result.output}\n\nSTDERR:\n${result.error}`);
}
}

export function execute(command: string, args: Array<string>, workingDirectory?: string | undefined): Promise<{ code: number, output: string, error: string }> {
return new Promise<{ code: number, output: string, error: string }>((resolve, reject) => {
let output = '';
let error = '';

let childProcess = cp.spawn(command, args, { cwd: workingDirectory });
childProcess.stdout.on('data', data => output += data);
childProcess.stderr.on('data', data => error += data);
childProcess.on('exit', (code: number, _signal: string) => {
resolve({
code: code,
output: output.trim(),
error: error.trim()
});
});
});
}
8 changes: 4 additions & 4 deletions src/dotnet-interactive-vscode/src/vscode/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { ClientMapper } from '../clientMapper';
import { DotNetInteractiveNotebookContentProvider } from './notebookContentProvider';
import { StdioKernelTransport } from '../stdioKernelTransport';
import { registerLanguageProviders } from './languageProvider';
import { execute, registerAcquisitionCommands, registerKernelCommands, registerFileCommands } from './commands';
import { registerAcquisitionCommands, registerKernelCommands, registerFileCommands } from './commands';

import { getSimpleLanguage, isDotnetInteractiveLanguage, notebookCellLanguages } from '../interactiveNotebook';
import { IDotnetAcquireResult } from 'dotnet-interactive-vscode-interfaces/out/dotnet';
import { InteractiveLaunchOptions, InstallInteractiveArgs } from '../interfaces';

import compareVersions = require("compare-versions");
import { DotNetCellMetadata, withDotNetMetadata } from '../ipynbUtilities';
import { processArguments } from '../utilities';
import { executeSafe, processArguments } from '../utilities';
import { OutputChannelAdapter } from './OutputChannelAdapter';
import { KernelId, updateCellLanguages, updateDocumentKernelspecMetadata } from './notebookKernel';
import { DotNetInteractiveNotebookKernelProvider } from './notebookKernelProvider';
Expand Down Expand Up @@ -186,7 +186,7 @@ async function computeDotnetPath(outputChannel: OutputChannelAdapter): Promise<v
const minDotNetSdkVersion = config.get<string>('minimumDotNetSdkVersion');
let dotnetPath: string;
if (await isDotnetUpToDate(minDotNetSdkVersion!)) {
dotnetPath = 'dotnet';
dotnetPath = cachedDotnetPath;
} else {
const commandResult = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', { version: minDotNetSdkVersion, requestingExtensionId: 'ms-dotnettools.dotnet-interactive-vscode' });
dotnetPath = commandResult!.dotnetPath;
Expand All @@ -206,6 +206,6 @@ async function getInteractiveLaunchOptions(dotnetPath: string): Promise<Interact
}

async function isDotnetUpToDate(minVersion: string): Promise<boolean> {
const result = await execute('dotnet', ['--version']);
const result = await executeSafe(cachedDotnetPath, ['--version']);
return result.code === 0 && compareVersions.compare(result.output, minVersion, '>=');
}