diff --git a/CHANGELOG.md b/CHANGELOG.md index d087aef5..3a8edba7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - User's `snippetTemplates` config is ignored - Default `glue` globs miss `.cjs`, `.mjs`, `.cts` and `.mts` files - Glue and feature files not found when root is not the current directory +- WASM files not found when `@cucumber/language-service` is hoisted ([#159](https://github.com/cucumber/language-server/pull/159)) ### Removed - BREAKING CHANGE: Dropped support for end-of-life Node versions 16, 18 and 20 diff --git a/bin/cucumber-language-server.js b/bin/cucumber-language-server.js index f5ab62e2..92de3dda 100755 --- a/bin/cucumber-language-server.js +++ b/bin/cucumber-language-server.js @@ -1,13 +1,12 @@ #!/usr/bin/env node import 'source-map-support/register.js' import path from 'node:path' +import { fileURLToPath } from 'node:url' import { NodeFiles } from '../dist/node/NodeFiles.js' import { version } from '../dist/version.js' import { startStandaloneServer } from '../dist/wasm/startStandaloneServer.js' -const wasmBasePath = path.resolve( - `${import.meta.dirname}/../node_modules/@cucumber/language-service/dist` -) +const wasmBasePath = path.dirname(fileURLToPath(import.meta.resolve('@cucumber/language-service'))) const { connection } = startStandaloneServer(wasmBasePath, (rootUri) => new NodeFiles(rootUri)) // Don't die on unhandled Promise rejections diff --git a/test/standalone.test.ts b/test/standalone.test.ts index 4e248236..2d638783 100644 --- a/test/standalone.test.ts +++ b/test/standalone.test.ts @@ -1,5 +1,8 @@ import assert from 'node:assert' import { type ChildProcess, fork } from 'node:child_process' +import fs from 'node:fs' +import os from 'node:os' +import path from 'node:path' import type { Duplex } from 'node:stream' import { NullLogger, StreamMessageReader, StreamMessageWriter } from 'vscode-jsonrpc/node' import { @@ -15,111 +18,161 @@ import { import type { Settings } from '../src/types.js' -describe('Standalone', () => { - let serverFork: ChildProcess - let logMessages: LogMessageParams[] - let clientConnection: ProtocolConnection +const tempDirs: string[] = [] - beforeEach(async () => { - logMessages = [] - serverFork = fork('./bin/cucumber-language-server.js', ['--stdio'], { - silent: true, +function makeHoistedInstall(): string { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'cucumber-language-server-')) + tempDirs.push(root) + const nodeModules = path.join(root, 'node_modules') + const serverDir = path.join(nodeModules, '@cucumber', 'language-server') + fs.mkdirSync(serverDir, { recursive: true }) + for (const entry of ['bin', 'dist', 'package.json']) { + fs.cpSync(entry, path.join(serverDir, entry), { recursive: true }) + } + for (const entry of fs.readdirSync('node_modules')) { + if (entry.startsWith('.')) { + continue + } + if (entry === '@cucumber') { + for (const scoped of fs.readdirSync('node_modules/@cucumber')) { + fs.symlinkSync( + path.resolve('node_modules/@cucumber', scoped), + path.join(nodeModules, '@cucumber', scoped) + ) + } + } else { + fs.symlinkSync(path.resolve('node_modules', entry), path.join(nodeModules, entry)) + } + } + return path.join(serverDir, 'bin', 'cucumber-language-server.js') +} + +const layouts: Record string> = { + 'local checkout': () => './bin/cucumber-language-server.js', + 'hoisted dependencies': makeHoistedInstall, +} + +for (const [layout, getBinPath] of Object.entries(layouts)) { + describe(`Standalone (${layout})`, () => { + let serverFork: ChildProcess + let logMessages: LogMessageParams[] + let clientConnection: ProtocolConnection + let binPath: string + + before(() => { + binPath = getBinPath() }) - const initializeParams: InitializeParams = { - rootUri: `file://${process.cwd()}`, - processId: NaN, // This id is used by vscode-languageserver. Set as NaN so that the watchdog responsible for watching this process does not run. - capabilities: { - workspace: { - configuration: true, - didChangeWatchedFiles: { - dynamicRegistration: true, - }, - }, - textDocument: { - moniker: { - dynamicRegistration: false, - }, - completion: { - completionItem: { - snippetSupport: true, + after(() => { + for (const dir of tempDirs.splice(0)) { + fs.rmSync(dir, { recursive: true, force: true }) + } + }) + + beforeEach(async () => { + logMessages = [] + serverFork = fork(binPath, ['--stdio'], { + silent: true, + }) + + const initializeParams: InitializeParams = { + rootUri: `file://${process.cwd()}`, + processId: NaN, // This id is used by vscode-languageserver. Set as NaN so that the watchdog responsible for watching this process does not run. + capabilities: { + workspace: { + configuration: true, + didChangeWatchedFiles: { + dynamicRegistration: true, }, }, - semanticTokens: { - tokenTypes: [], - tokenModifiers: [], - formats: [], - requests: {}, - }, - formatting: { - dynamicRegistration: true, + textDocument: { + moniker: { + dynamicRegistration: false, + }, + completion: { + completionItem: { + snippetSupport: true, + }, + }, + semanticTokens: { + tokenTypes: [], + tokenModifiers: [], + formats: [], + requests: {}, + }, + formatting: { + dynamicRegistration: true, + }, }, }, - }, - workspaceFolders: null, - } - if (!serverFork.stdin || !serverFork.stdout) { - throw 'Process created without stdio streams' - } - clientConnection = createProtocolConnection( - new StreamMessageReader(serverFork.stdout as Duplex), - new StreamMessageWriter(serverFork.stdin as Duplex), - NullLogger - ) - clientConnection.onError((err) => { - console.error('ERROR', err) - }) - clientConnection.onNotification(LogMessageNotification.type, (params) => { - if (params.type !== 3) { - logMessages.push(params) + workspaceFolders: null, } + if (!serverFork.stdin || !serverFork.stdout) { + throw 'Process created without stdio streams' + } + clientConnection = createProtocolConnection( + new StreamMessageReader(serverFork.stdout as Duplex), + new StreamMessageWriter(serverFork.stdin as Duplex), + NullLogger + ) + clientConnection.onError((err) => { + console.error('ERROR', err) + }) + clientConnection.onNotification(LogMessageNotification.type, (params) => { + if (params.type !== 3) { + logMessages.push(params) + } + }) + clientConnection.onUnhandledNotification((n) => { + console.error('Unhandled notification', n) + }) + clientConnection.listen() + const { serverInfo } = await clientConnection.sendRequest( + InitializeRequest.type, + initializeParams + ) + assert.strictEqual(serverInfo?.name, 'Cucumber Language Server') }) - clientConnection.onUnhandledNotification((n) => { - console.error('Unhandled notification', n) - }) - clientConnection.listen() - const { serverInfo } = await clientConnection.sendRequest( - InitializeRequest.type, - initializeParams - ) - assert.strictEqual(serverInfo?.name, 'Cucumber Language Server') - }) - afterEach(() => { - clientConnection.end() - clientConnection.dispose() - serverFork.kill('SIGTERM') // Try to terminate first - serverFork.kill('SIGKILL') // Then try to kill if it is not killed - }) + afterEach(() => { + clientConnection.end() + clientConnection.dispose() + serverFork.kill('SIGTERM') // Try to terminate first + serverFork.kill('SIGKILL') // Then try to kill if it is not killed + }) - context('workspace/didChangeConfiguration', () => { - it(`startup success`, async () => { - // First we need to configure the server, telling it where to find Gherkin documents and Glue code. - // Note that *pushing* settings from the client to the server is deprecated in the LSP. We're only using it - // here because it's easier to implement in the test. - const settings: Settings = { - features: ['testdata/**/*.feature'], - glue: ['testdata/**/*.js'], - parameterTypes: [], - snippetTemplates: {}, - } - const configParams: DidChangeConfigurationParams = { - settings, - } + context('workspace/didChangeConfiguration', () => { + it(`startup success`, async () => { + // First we need to configure the server, telling it where to find Gherkin documents and Glue code. + // Note that *pushing* settings from the client to the server is deprecated in the LSP. We're only using it + // here because it's easier to implement in the test. + const settings: Settings = { + features: ['testdata/**/*.feature'], + glue: ['testdata/**/*.js'], + parameterTypes: [], + snippetTemplates: {}, + } + const configParams: DidChangeConfigurationParams = { + settings, + } - await clientConnection.sendNotification(DidChangeConfigurationNotification.type, configParams) + await clientConnection.sendNotification( + DidChangeConfigurationNotification.type, + configParams + ) - await new Promise((resolve) => setTimeout(resolve, 1000)) + await new Promise((resolve) => setTimeout(resolve, 1000)) - assert.strictEqual( - logMessages.length, - // TODO: change this to 0 when `workspace/semanticTokens/refresh` issue was solved - 1, - // print readable log messages - logMessages - .map(({ type, message }) => `**Type**: ${type}\n**Message**:\n${message}\n`) - .join('\n--------------------\n') - ) + assert.strictEqual( + logMessages.length, + // TODO: change this to 0 when `workspace/semanticTokens/refresh` issue was solved + 1, + // print readable log messages + logMessages + .map(({ type, message }) => `**Type**: ${type}\n**Message**:\n${message}\n`) + .join('\n--------------------\n') + ) + }) }) }) -}) +}