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

Fix bugs in the language server version picker #568

Merged
merged 3 commits into from
May 16, 2024
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
50 changes: 34 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"open": "^8.4.2",
"postman-request": "^2.88.1-postman.32",
"pretty-bytes": "^5.6.0",
"resolve": "^1.22.8",
"roku-debug": "^0.21.7",
"roku-deploy": "^3.12.0",
"roku-test-automation": "^2.0.6",
Expand All @@ -100,6 +101,7 @@
"@types/node": "^12.12.0",
"@types/node-ssdp": "^3.3.0",
"@types/prompt": "^1.1.2",
"@types/resolve": "^1.20.6",
"@types/semver": "^7.1.0",
"@types/sinon": "7.0.6",
"@types/vscode": "^1.53.0",
Expand Down
4 changes: 2 additions & 2 deletions src/LanguageServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ export class LanguageServerManager {
this.selectedBscInfo = {
path: bsdkPath,
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
version: require(`${bsdkPath}/package.json`).version
version: fsExtra.readJsonSync(`${bsdkPath}/package.json`).version
};
} catch (e) {
console.error(e);
//fall back to the embedded version, and show a popup
await vscode.window.showErrorMessage(`Can't find language server at "${bsdkPath}". Using embedded version v${this.embeddedBscInfo.version} instead.`);
await vscode.window.showErrorMessage(`Can't find language server at "${bsdkPath}". Did you forget to run \`npm install\`? Using embedded version v${this.embeddedBscInfo.version} instead.`);
this.selectedBscInfo = this.embeddedBscInfo;
}

Expand Down
144 changes: 144 additions & 0 deletions src/commands/LanguageServerInfoCommand.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import * as fsExtra from 'fs-extra';
import { standardizePath as s } from 'brighterscript';
import { LanguageServerInfoCommand } from './LanguageServerInfoCommand';
import { expect } from 'chai';
import { createSandbox } from 'sinon';
import * as resolve from 'resolve';

const sinon = createSandbox();

const cwd = s`${__dirname}../../../`;
const tempDir = s`${cwd}/.tmp`;
const embeddedBscVersion = require('brighterscript/package.json').version;

describe('LanguageServerInfoCommand', () => {
let command: LanguageServerInfoCommand;
beforeEach(() => {
sinon.restore();
fsExtra.ensureDirSync(tempDir);
command = new LanguageServerInfoCommand();
const orig = resolve.sync.bind(resolve) as typeof resolve['sync'];
sinon.stub(resolve, 'sync').callsFake((moduleName: string, options?: any) => {
return orig(moduleName, {
...options ?? {},
packageIterator: (request, start, getPackageCandidates, opts) => {
const candidates = getPackageCandidates();
const filtered = candidates.filter(candidate => s(candidate).startsWith(tempDir));
return filtered;
}
});
});
});

afterEach(() => {
sinon.restore();
fsExtra.removeSync(tempDir);
});

describe('discoverBrighterScriptVersions', () => {
function writePackage(version: string) {
fsExtra.outputJsonSync(s`${tempDir}/package.json`, {
name: 'vscode-tests',
private: true,
dependencies: {
brighterscript: version
}
});
fsExtra.outputFileSync(s`${tempDir}/node_modules/brighterscript/dist/index.js`, '');
fsExtra.outputJsonSync(s`${tempDir}/node_modules/brighterscript/package.json`, {
name: 'brighterscript',
version: version,
main: 'dist/index.js',
dependencies: {}
});
}
it('finds embedded version when node_modules is not present', () => {
expect(
command['discoverBrighterScriptVersions']([tempDir])
).to.eql([{
label: `Use VSCode's version`,
description: embeddedBscVersion
}]);
});

it('finds embedded version when node_modules is present but brighterscript is not available', () => {
fsExtra.outputJsonSync(s`${tempDir}/package.json`, {
name: 'vscode-tests',
private: true,
dependencies: {}
});
fsExtra.outputJsonSync(s`${tempDir}/node_modules/is-number/package.json`, {
name: 'is-number',
dependencies: {}
});
expect(
command['discoverBrighterScriptVersions']([tempDir])
).to.eql([{
label: `Use VSCode's version`,
description: embeddedBscVersion
}]);
});

it('finds brighterscript version from node_modules', () => {
writePackage('1.2.3');
expect(
command['discoverBrighterScriptVersions']([tempDir])
).to.eql([{
label: `Use VSCode's version`,
description: embeddedBscVersion
}, {
label: `Use Workspace Version`,
description: '1.2.3',
detail: 'node_modules/brighterscript'
}]);
});

it('does not cache brighterscript version from node_modules in subsequent calls', () => {
writePackage('1.2.3');
expect(
command['discoverBrighterScriptVersions']([tempDir])
).to.eql([{
label: `Use VSCode's version`,
description: embeddedBscVersion
}, {
label: `Use Workspace Version`,
description: '1.2.3',
detail: 'node_modules/brighterscript'
}]);

writePackage('2.3.4');
expect(
command['discoverBrighterScriptVersions']([tempDir])
).to.eql([{
label: `Use VSCode's version`,
description: embeddedBscVersion
}, {
label: `Use Workspace Version`,
description: '2.3.4',
detail: 'node_modules/brighterscript'
}]);
});

it('excludes value when module is deleted since last time', () => {
writePackage('1.2.3');
expect(
command['discoverBrighterScriptVersions']([tempDir])
).to.eql([{
label: `Use VSCode's version`,
description: embeddedBscVersion
}, {
label: `Use Workspace Version`,
description: '1.2.3',
detail: 'node_modules/brighterscript'
}]);

fsExtra.removeSync(`${tempDir}/node_modules`);
expect(
command['discoverBrighterScriptVersions']([tempDir])
).to.eql([{
label: `Use VSCode's version`,
description: embeddedBscVersion
}]);
});
});
});
Loading