diff --git a/CHANGELOG.md b/CHANGELOG.md index 8afee1e..49d7a2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to the "comline-vscode" extension will be documented in this ## [Unreleased] ### Added +- The package status bar item's tooltip says which language server and CLI + are in use: each one's version with its `comline-core` commit, and its path + with how it was found (bundled, PATH, custom path, or on the host). The CLI's + comes from the one `comline --version` run the core mismatch warning already + makes. - A warning when the language server and the CLI were built with different `comline-core` commits, since they can then disagree about a file (the bundled server rejected grammar `comline generation` accepted). Shown once diff --git a/scripts/flatpak-e2e.cjs b/scripts/flatpak-e2e.cjs index 94dc746..00553d0 100644 --- a/scripts/flatpak-e2e.cjs +++ b/scripts/flatpak-e2e.cjs @@ -324,8 +324,8 @@ scenario('3. host probe: ok, missing, and flatpak-spawn itself failing', async ( return r.interpretHostProbe({ exitCode: e.code, stdout: e.stdout ?? '', stderr: e.stderr ?? '' }); } }; - assert.deepEqual(await run('comline-lsp'), { ok: true }); - assert.deepEqual(await run(config.hostLspHome), { ok: true }); + assert.deepEqual(await run('comline-lsp'), { ok: true, found: config.hostLspUsr }); + assert.deepEqual(await run(config.hostLspHome), { ok: true, found: config.hostLspHome }); assert.deepEqual(await run('comline-no-such-program'), { ok: false, reason: 'missing' }); assert.deepEqual(await run('/no/such/dir/comline-lsp'), { ok: false, reason: 'missing' }); // No session bus to talk to: flatpak-spawn fails, and says why. @@ -355,7 +355,9 @@ scenario('4. path inside the sandbox is the host', async () => { const plan = r.planProgram({ tool, mode: 'path', customPath: '', bundled: null }, probe); assert.deepEqual(plan, { kind: 'host', spawn: spawnPath, program }); const verified = await r.verifyPlan(tool, plan); - assert.deepEqual(verified, plan, `${program} is not on the host: ${verified.message}`); + assert.equal(verified.kind, 'host', `${program} is not on the host: ${verified.message}`); + assert.deepEqual({ ...verified, found: undefined }, { ...plan, found: undefined }); + assert.ok(path.isAbsolute(verified.found), `found ${verified.found}`); } }); diff --git a/src/coreRev.test.ts b/src/coreRev.test.ts index dcca94c..3c4545d 100644 --- a/src/coreRev.test.ts +++ b/src/coreRev.test.ts @@ -2,7 +2,7 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { cliCoreRev, coreRevMismatch, mismatchMessage, serverCoreRev } from './coreRev'; +import { cliCoreRev, cliDisplayVersion, coreRevMismatch, displayVersion, mismatchMessage, serverCoreRev } from './coreRev'; const A = '14ce54658455206d639ddc4fbe6d609d20292587'; const B = '3b9cc9d0a1f2e4c5b6a798011223344556677889'; @@ -59,3 +59,19 @@ test('the message names both cores, short', () => { 'diagnostics may differ from what `comline generation` reports.' ); }); + +test('a server version for display, with its core commit short', () => { + assert.equal(displayVersion(`0.1.0 (core ${'a'.repeat(40)})`), '0.1.0 (core aaaaaaa)'); + assert.equal(displayVersion('0.1.0'), '0.1.0'); + assert.equal(displayVersion('0.1.0 (core unknown)'), '0.1.0 (core unknown)'); +}); + +test('a CLI version for display: its first line, and its core commit short', () => { + const sha = 'b'.repeat(40); + assert.equal( + cliDisplayVersion(`comline 0.1.0\ncommit-hash: abc\nbuild-date: 2026-11-16\ncore-rev: ${sha}\n`), + 'comline 0.1.0 (core bbbbbbb)' + ); + assert.equal(cliDisplayVersion('comline 0.1.0\ncommit-hash: abc\n'), 'comline 0.1.0'); + assert.equal(cliDisplayVersion('\n'), undefined); +}); diff --git a/src/coreRev.ts b/src/coreRev.ts index 0ab7f3d..147a3c3 100644 --- a/src/coreRev.ts +++ b/src/coreRev.ts @@ -49,3 +49,21 @@ export function mismatchMessage({ server, cli }: CoreRevs): string { `${short(cli)}; diagnostics may differ from what \`comline generation\` reports.` ); } + +/** A server's `serverInfo.version` with its core commit shortened, for + * display: `0.1.0 (core 14ce546)`. Anything else is returned as it is. */ +export function displayVersion(version: string): string { + return version.replace(new RegExp(`\\(core (${SHA})\\)`), (_, rev: string) => `(core ${short(rev)})`); +} + +/** `comline --version`'s first line with the core commit after it, for + * display: `comline 0.1.0 (core 14ce546)`, or just the first line for a CLI + * that doesn't report one. Undefined for empty output. */ +export function cliDisplayVersion(output: string): string | undefined { + const first = output.split('\n').find((line) => line.trim() !== '')?.trim(); + if (first === undefined) { + return undefined; + } + const rev = cliCoreRev(output); + return rev ? `${first} (core ${short(rev)})` : first; +} diff --git a/src/extension.ts b/src/extension.ts index b317caa..1728e6d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -52,10 +52,11 @@ import { effectiveMode, planProgram, launchFor, + planLocation, systemProbe, verifyPlan, } from './resolve'; -import { coreRevMismatch, mismatchMessage, serverCoreRev } from './coreRev'; +import { cliDisplayVersion, coreRevMismatch, displayVersion, mismatchMessage, serverCoreRev } from './coreRev'; const execFileAsync = promisify(execFile); @@ -70,6 +71,16 @@ let extensionContext: ExtensionContext; let packageStatusBarItem: StatusBarItem; let discoveredPackageRoots: string[] = []; +/** What the status bar's tooltip says about the language server: where it + * runs (`planLocation`), and its `serverInfo.version` once it has started, + * or what went wrong. */ +const serverStatus: { location?: string; version?: string; problem?: string } = {}; + +/** The same for the CLI, from one `comline --version` run (`loadCliStatus`), + * whose output the core mismatch warning reads too. */ +let cliStatus: { location?: string; version?: string; problem?: string } = {}; +let cliVersionPromise: Promise | undefined; + interface GenerationTarget { name: string; version: string; @@ -144,6 +155,7 @@ export async function activate(context: ExtensionContext): Promise { // language server's diagnostics and hover: it resolves through the same // registry `comline build` does. void ensureTargetsLoaded(); + cliVersionPromise = loadCliStatus(); context.subscriptions.push( languages.registerCompletionItemProvider( @@ -208,17 +220,24 @@ export async function activate(context: ExtensionContext): Promise { if (e.affectsConfiguration('comline.cli.mode') || e.affectsConfiguration('comline.cli.customPath')) { targetsPromise = undefined; void ensureTargetsLoaded(); + cliVersionPromise = loadCliStatus(); } }) ); const plan = await resolveProgram('server', context); if (plan.kind === 'error') { + serverStatus.problem = 'not found'; + updatePackageStatusBarItem(); window.showErrorMessage(plan.message); return; } const config = workspace.getConfiguration('comline'); + serverStatus.location = planLocation( + plan, + effectiveMode('server', config.get('server.mode', TOOLS.server.defaultMode), config.get('server.customPath', '').trim()).mode + ); const debug = config.get('server.debug', false); // `comline-lsp` builds its tracing subscriber from `RUST_LOG` (see @@ -300,6 +319,8 @@ export async function activate(context: ExtensionContext): Promise { } await client.stop(); await client.start(); + serverStatus.version = client.initializeResult?.serverInfo?.version; + updatePackageStatusBarItem(); window.showInformationMessage('Comline Language Server restarted.'); }), ); @@ -307,8 +328,14 @@ export async function activate(context: ExtensionContext): Promise { try { await client.start(); console.log('Comline Language Server started successfully'); - void warnOnCoreMismatch(client.initializeResult?.serverInfo?.version); + serverStatus.version = client.initializeResult?.serverInfo?.version; + serverStatus.problem = undefined; + updatePackageStatusBarItem(); + const serverVersion = client.initializeResult?.serverInfo?.version; + void cliVersionPromise?.then((cliVersion) => warnOnCoreMismatch(serverVersion, cliVersion)); } catch (error) { + serverStatus.problem = 'failed to start'; + updatePackageStatusBarItem(); window.showErrorMessage( `Failed to start Comline Language Server: ${error}` ); @@ -322,7 +349,10 @@ let coreRevChecked = false; * disagree about the same file. Says nothing when either doesn't report its * commit, when there is no CLI to ask, or when `comline.warnOnCoreMismatch` * is off. Never throws. */ -async function warnOnCoreMismatch(serverVersion: string | undefined): Promise { +async function warnOnCoreMismatch( + serverVersion: string | undefined, + cliVersionOutput: string | undefined +): Promise { if (coreRevChecked) { return; } @@ -332,16 +362,7 @@ async function warnOnCoreMismatch(serverVersion: string | undefined): Promise('warnOnCoreMismatch', true) || serverCoreRev(serverVersion) === undefined) { return; } - const plan = await resolveProgram('cli'); - if (plan.kind === 'error') { - return; - } - const launch = launchFor(plan, ['--version'], {}, process.env); - const { stdout } = await execFileAsync(launch.command, launch.args, { - timeout: 5000, - env: launch.env, - }); - const mismatch = coreRevMismatch(serverVersion, stdout); + const mismatch = coreRevMismatch(serverVersion, cliVersionOutput); if (!mismatch) { return; } @@ -350,7 +371,41 @@ async function warnOnCoreMismatch(serverVersion: string | undefined): Promise { + const config = workspace.getConfiguration('comline'); + const mode = effectiveMode( + 'cli', + config.get('cli.mode', TOOLS.cli.defaultMode), + config.get('cli.customPath', '').trim() + ).mode; + const plan = await resolveProgram('cli'); + if (plan.kind === 'error') { + cliStatus = { problem: 'not found' }; + updatePackageStatusBarItem(); + return undefined; + } + cliStatus = { location: planLocation(plan, mode) }; + try { + const launch = launchFor(plan, ['--version'], {}, process.env); + const { stdout } = await execFileAsync(launch.command, launch.args, { + timeout: 5000, + env: launch.env, + }); + cliStatus.version = cliDisplayVersion(stdout); + return stdout; + } catch (err) { + cliStatus.problem = "'comline --version' failed"; + console.warn(`Comline: 'comline --version' failed: ${err}`); + return undefined; + } finally { + updatePackageStatusBarItem(); } } @@ -442,10 +497,24 @@ function updatePackageStatusBarItem(): void { rootContainingActiveFile(discoveredPackageRoots) ?? discoveredPackageRoots[0]; packageStatusBarItem.text = `$(comline-mark) $(package) ${packageLabel(current)}`; - packageStatusBarItem.tooltip = `Currently active/selected package:\n${current}\n\nClick to open menu`; + packageStatusBarItem.tooltip = + `Currently active/selected package:\n${current}\n\n` + + `Language server: ${serverStatusLine()}\n` + + (serverStatus.location ? `${serverStatus.location}\n` : '') + + `\nCLI: ${cliStatus.version ?? cliStatus.problem ?? 'checking'}\n` + + (cliStatus.location ? `${cliStatus.location}\n` : '') + + `\nClick to open menu`; packageStatusBarItem.show(); } +/** `comline-lsp 0.1.0 (core 14ce546)`, or why there is none. */ +function serverStatusLine(): string { + if (serverStatus.version) { + return `comline-lsp ${displayVersion(serverStatus.version)}`; + } + return serverStatus.problem ?? (serverStatus.location ? 'starting' : 'not started'); +} + function toPackageQuickPickItem(root: string): PackageQuickPickItem { const base = workspace.getWorkspaceFolder(Uri.file(root)); return { diff --git a/src/resolve.test.ts b/src/resolve.test.ts index c11cc24..346f332 100644 --- a/src/resolve.test.ts +++ b/src/resolve.test.ts @@ -11,6 +11,7 @@ import { interpretHostProbe, launchFor, pathDirectories, + planLocation, planProgram, resolveExecutable, } from './resolve'; @@ -331,6 +332,7 @@ test('the host probe asks the host\'s shell, with the program as a plain argumen test('the host probe: found, missing, or flatpak-spawn itself failing', () => { assert.deepEqual(interpretHostProbe({ exitCode: 0, stdout: '/usr/bin/comline-lsp\n', stderr: '' }), { ok: true, + found: '/usr/bin/comline-lsp', }); assert.deepEqual(interpretHostProbe({ exitCode: 1, stdout: '', stderr: '' }), { ok: false, @@ -383,3 +385,16 @@ test('the host messages say what to install and which setting to change', () => assert.match(failed, /denied/); assert.match(failed, /--talk-name=org\.freedesktop\.Flatpak com\.vscodium\.codium/); }); + +test('where a plan runs, for the status bar', () => { + assert.equal( + planLocation({ kind: 'direct', command: '/ext/bin/linux-x64/comline-lsp' }, 'bundled'), + '/ext/bin/linux-x64/comline-lsp (bundled)' + ); + assert.equal(planLocation({ kind: 'direct', command: '/usr/bin/comline-lsp' }, 'path'), '/usr/bin/comline-lsp (PATH)'); + assert.equal(planLocation({ kind: 'direct', command: '/opt/comline-lsp' }, 'custom'), '/opt/comline-lsp (custom path)'); + const host = { kind: 'host' as const, spawn: '/usr/bin/flatpak-spawn', program: 'comline-lsp' }; + assert.equal(planLocation(host, 'path'), 'comline-lsp (on the host)'); + assert.equal(planLocation({ ...host, found: '/usr/bin/comline-lsp' }, 'path'), '/usr/bin/comline-lsp (on the host)'); + assert.equal(planLocation({ kind: 'error', message: 'no' }, 'path'), undefined); +}); diff --git a/src/resolve.ts b/src/resolve.ts index 179507d..4acc311 100644 --- a/src/resolve.ts +++ b/src/resolve.ts @@ -212,8 +212,9 @@ export function effectiveMode(tool: Tool, mode: string, customPath: string): Eff export type Plan = /** Run this file as it is. */ | { kind: 'direct'; command: string } - /** Run `program` on the host: `spawn` (flatpak-spawn) with `--host`. */ - | { kind: 'host'; spawn: string; program: string } + /** Run `program` on the host: `spawn` (flatpak-spawn) with `--host`. + * `found` is where the host check (`verifyPlan`) found it. */ + | { kind: 'host'; spawn: string; program: string; found?: string } | { kind: 'error'; message: string }; export interface PlanInput { @@ -403,7 +404,8 @@ export function hostProbeArgs(program: string): string[] { } export type HostProbe = - | { ok: true } + /** `found`: the program's path on the host, as `command -v` printed it. */ + | { ok: true; found: string } | { ok: false; reason: 'missing' } | { ok: false; reason: 'failed'; detail: string }; @@ -418,7 +420,8 @@ export function interpretHostProbe(result: { }): HostProbe { const stderr = result.stderr.trim(); if (result.exitCode === 0) { - return result.stdout.trim() !== '' ? { ok: true } : { ok: false, reason: 'missing' }; + const found = result.stdout.trim().split('\n')[0]; + return found !== '' ? { ok: true, found } : { ok: false, reason: 'missing' }; } if (typeof result.exitCode === 'number' && stderr === '') { return { ok: false, reason: 'missing' }; @@ -475,7 +478,24 @@ export async function verifyPlan( }); } if (result.ok) { - return plan; + return { ...plan, found: result.found }; } return { kind: 'error', message: hostProbeMessage(tool, plan.program, result, env.FLATPAK_ID) }; } + +// -- Saying where it runs ----------------------------------------------- + +/** Where a plan runs the program, for the status bar's tooltip: + * `/path/to/comline-lsp (bundled)`, `... (PATH)`, `... (custom path)`, or + * `... (on the host)` for a host plan, with the path the host check found. + * Undefined for an error plan. */ +export function planLocation(plan: Plan, mode: Mode): string | undefined { + switch (plan.kind) { + case 'direct': + return `${plan.command} (${mode === 'path' ? 'PATH' : mode === 'custom' ? 'custom path' : mode})`; + case 'host': + return `${plan.found ?? plan.program} (on the host)`; + case 'error': + return undefined; + } +}