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

Do not try to acquire dotnet runtime if we are on M1 macs #291

Merged
merged 4 commits into from
Jun 23, 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
13 changes: 12 additions & 1 deletion src/languageServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ServerOptions,
Trace,
} from 'vscode-languageclient';
import {OSType, getOSType} from './utils';
import {Telemetry} from './telemetry';

export class StripeLanguageClient {
Expand Down Expand Up @@ -65,6 +66,7 @@ export class StripeLanguageClient {

universalClient.start();
outputChannel.appendLine('Universal language server is running');
telemetry.sendEvent('universalLanguageServerStarted');
}

static async activateDotNetServer(
Expand All @@ -74,8 +76,16 @@ export class StripeLanguageClient {
telemetry: Telemetry,
) {
outputChannel.appendLine('Detected C# Project file: ' + projectFile);

const dotnetRuntimeVersion = '5.0';

// Applie Silicon is not supported for dotnet < 6.0:
// https://github.com/dotnet/core/issues/4879#issuecomment-729046912
if (getOSType() === OSType.macOSarm) {
outputChannel.appendLine(`.NET runtime v${dotnetRuntimeVersion} is not supported for M1`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to say Apple Silicon

telemetry.sendEvent('dotnetRuntimeAcquisitionSkippedForM1');
return;
}

const result = await vscode.commands.executeCommand<{dotnetPath: string}>('dotnet.acquire', {
version: dotnetRuntimeVersion,
requestingExtensionId: 'stripe.vscode-stripe',
Expand Down Expand Up @@ -140,6 +150,7 @@ export class StripeLanguageClient {

await dotnetClient.onReady();
outputChannel.appendLine('C# language service is running.');
telemetry.sendEvent('dotnetServerStarted');
}

/**
Expand Down
239 changes: 239 additions & 0 deletions test/suite/languageServerClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as utils from '../../src/utils';
import * as vscode from 'vscode';
import {BaseLanguageClient, MessageTransports} from 'vscode-languageclient';
import {NoOpTelemetry, Telemetry} from '../../src/telemetry';
import {StripeLanguageClient} from '../../src/languageServerClient';
import {mocks} from '../mocks/vscode';

const proxyquire = require('proxyquire');
const modulePath = '../../src/languageServerClient';

const setupProxies = (proxies: any) => proxyquire(modulePath, proxies);

class TestLanguageClient extends BaseLanguageClient {
constructor() {
super('foo', 'foo', {});
}
protected createMessageTransports(): Promise<MessageTransports> {
throw new Error('Method not implemented.');
}
start() {
return {dispose: () => {}};
}

onReady() {
return Promise.resolve();
}
}

const vscodeStub = {
LanguageClient: TestLanguageClient,
};

suite('languageServerClient', function () {
this.timeout(20000);
let extensionContext: vscode.ExtensionContext;
let outputChannel: Partial<vscode.OutputChannel>;
let telemetry: Telemetry;
let sandbox: sinon.SinonSandbox;

setup(() => {
sandbox = sinon.createSandbox();
outputChannel = {appendLine: (value: string) => {}, show: () => {}};
extensionContext = {...mocks.extensionContextMock};
telemetry = new NoOpTelemetry();
});

teardown(() => {
sandbox.restore();
});

suite('activateDotNetServer', () => {
const module = setupProxies({'vscode-languageclient': vscodeStub});
const dotnetPath = '/my/executable/path';
let telemetrySpy: sinon.SinonSpy;

const stubDotnetAcquire = (result: {} | undefined) => {
return sandbox
.stub(vscode.commands, 'executeCommand')
.withArgs('dotnet.acquire', {
version: '5.0',
requestingExtensionId: 'stripe.vscode-stripe',
})
.resolves(result);
};

setup(() => {
telemetrySpy = sandbox.spy(telemetry, 'sendEvent');
});

test('calls activate on dotnet client', async () => {
sandbox.stub(utils, 'getOSType').returns(utils.OSType.macOSintel);
const dotnetAcquireStub = stubDotnetAcquire({dotnetPath: dotnetPath});
const projectFile = 'project/file.sln';

await module.StripeLanguageClient.activateDotNetServer(
extensionContext,
<any>outputChannel,
projectFile,
telemetry,
);

assert.deepStrictEqual(dotnetAcquireStub.calledOnce, true);
assert.deepStrictEqual(
telemetrySpy.calledWith('dotnetRuntimeAcquisitionSkippedForM1'),
false,
);
assert.deepStrictEqual(telemetrySpy.calledWith('dotnetRuntimeAcquisitionFailed'), false);
assert.deepStrictEqual(telemetrySpy.calledWith('dotnetServerStarted'), true);
});

test('does not start server if on M1', async () => {
sandbox.stub(utils, 'getOSType').returns(utils.OSType.macOSarm);
const dotnetAcquireStub = stubDotnetAcquire({dotnetPath: dotnetPath});

const projectFile = 'project/file.sln';

await module.StripeLanguageClient.activateDotNetServer(
extensionContext,
<any>outputChannel,
projectFile,
telemetry,
);

assert.deepStrictEqual(telemetrySpy.calledWith('dotnetRuntimeAcquisitionSkippedForM1'), true);
assert.deepStrictEqual(dotnetAcquireStub.calledOnce, false);
assert.deepStrictEqual(telemetrySpy.calledWith('dotnetRuntimeAcquisitionFailed'), false);
assert.deepStrictEqual(telemetrySpy.calledWith('dotnetServerStarted'), false);
});

test('does not start server error while acquiring dotnet', async () => {
sandbox.stub(utils, 'getOSType').returns(utils.OSType.macOSintel);
const dotnetAcquireStub = stubDotnetAcquire(undefined);

const projectFile = 'project/file.sln';

await module.StripeLanguageClient.activateDotNetServer(
extensionContext,
<any>outputChannel,
projectFile,
telemetry,
);

assert.deepStrictEqual(
telemetrySpy.calledWith('dotnetRuntimeAcquisitionSkippedForM1'),
false,
);
assert.deepStrictEqual(dotnetAcquireStub.calledOnce, true);
assert.deepStrictEqual(telemetrySpy.calledWith('dotnetRuntimeAcquisitionFailed'), true);
assert.deepStrictEqual(telemetrySpy.calledWith('dotnetServerStarted'), false);
});
});

suite('getDotnetProjectFiles', () => {
const workspaceRoot = vscode.Uri.file('my/path');
const workspacePath = workspaceRoot.fsPath;

test('returns empty when no workspaces', async () => {
sandbox.stub(vscode.workspace, 'workspaceFolders').value([]);
const projectFiles = await StripeLanguageClient.getDotnetProjectFiles();
assert.deepStrictEqual(projectFiles, []);
});

test('returns empty when workspaceFolder is undefined', async () => {
sandbox.stub(vscode.workspace, 'workspaceFolders').value(undefined);
const projectFiles = await StripeLanguageClient.getDotnetProjectFiles();
assert.deepStrictEqual(projectFiles, []);
});

test('returns sln project if it exists', async () => {
const slnFile = vscode.Uri.file('project.sln');
sandbox.stub(vscode.workspace, 'workspaceFolders').value([
{
index: 0,
name: 'workspace',
uri: vscode.Uri.file(workspacePath),
},
]);
sandbox.stub(vscode.workspace, 'findFiles').returns(Promise.resolve([slnFile]));

const projectFiles = await StripeLanguageClient.getDotnetProjectFiles();
assert.deepStrictEqual(projectFiles, [slnFile.fsPath]);
});

test('returns csproj if it exists', async () => {
const csprojFile = vscode.Uri.file('project.csproj');
sandbox.stub(vscode.workspace, 'workspaceFolders').value([
{
index: 0,
name: 'workspace',
uri: vscode.Uri.file(workspacePath),
},
]);
const fileFilesStub = sandbox.stub(vscode.workspace, 'findFiles');
fileFilesStub
.withArgs(new vscode.RelativePattern(workspacePath, '**/*.sln'))
.returns(Promise.resolve([]));
fileFilesStub
.withArgs(new vscode.RelativePattern(workspacePath, '**/*.csproj'))
.returns(Promise.resolve([csprojFile]));

const projectFiles = await StripeLanguageClient.getDotnetProjectFiles();
assert.deepStrictEqual(projectFiles, [csprojFile.fsPath]);
});

test('returns empty when no dotnet projects', async () => {
sandbox.stub(vscode.workspace, 'workspaceFolders').value([
{
index: 0,
name: 'workspace',
uri: vscode.Uri.file(workspacePath),
},
]);
const fileFilesStub = sandbox.stub(vscode.workspace, 'findFiles');
fileFilesStub.onCall(0).returns(Promise.resolve([]));
fileFilesStub.onCall(1).returns(Promise.resolve([]));

const projectFiles = await StripeLanguageClient.getDotnetProjectFiles();
assert.deepStrictEqual(projectFiles, []);
});

test('filters out non-dotnet projects', async () => {
const slnFile = vscode.Uri.file('project.sln');
const workspacePath1 = vscode.Uri.file('path/to/workspace/1');
const workspacePath2 = vscode.Uri.file('path/to/workspace/2');

sandbox.stub(vscode.workspace, 'workspaceFolders').value([
{
index: 0,
name: 'workspace',
uri: workspacePath1,
},
{
index: 1,
name: 'another workspace',
uri: workspacePath2,
},
]);
const fileFilesStub = sandbox.stub(vscode.workspace, 'findFiles');

// first workspace has a solution
fileFilesStub
.withArgs(new vscode.RelativePattern(workspacePath1.fsPath, '**/*.sln'))
.returns(Promise.resolve([slnFile]));

// second workspace is not a dotnet project
fileFilesStub
.withArgs(new vscode.RelativePattern(workspacePath2.fsPath, '**/*.sln'))
.returns(Promise.resolve([]));
fileFilesStub
.withArgs(new vscode.RelativePattern(workspacePath2.fsPath, '**/*.csproj'))
.returns(Promise.resolve([]));

const projectFiles = await StripeLanguageClient.getDotnetProjectFiles();
assert.deepStrictEqual(projectFiles, [slnFile.fsPath]);
});
});
});