Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions scripts/flatpak-e2e.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}`);
}
});

Expand Down
18 changes: 17 additions & 1 deletion src/coreRev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
18 changes: 18 additions & 0 deletions src/coreRev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
99 changes: 84 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<string | undefined> | undefined;

interface GenerationTarget {
name: string;
version: string;
Expand Down Expand Up @@ -144,6 +155,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
// language server's diagnostics and hover: it resolves through the same
// registry `comline build` does.
void ensureTargetsLoaded();
cliVersionPromise = loadCliStatus();

context.subscriptions.push(
languages.registerCompletionItemProvider(
Expand Down Expand Up @@ -208,17 +220,24 @@ export async function activate(context: ExtensionContext): Promise<void> {
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<string>('server.mode', TOOLS.server.defaultMode), config.get<string>('server.customPath', '').trim()).mode
);
const debug = config.get<boolean>('server.debug', false);

// `comline-lsp` builds its tracing subscriber from `RUST_LOG` (see
Expand Down Expand Up @@ -300,15 +319,23 @@ export async function activate(context: ExtensionContext): Promise<void> {
}
await client.stop();
await client.start();
serverStatus.version = client.initializeResult?.serverInfo?.version;
updatePackageStatusBarItem();
window.showInformationMessage('Comline Language Server restarted.');
}),
);

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}`
);
Expand All @@ -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<void> {
async function warnOnCoreMismatch(
serverVersion: string | undefined,
cliVersionOutput: string | undefined
): Promise<void> {
if (coreRevChecked) {
return;
}
Expand All @@ -332,16 +362,7 @@ async function warnOnCoreMismatch(serverVersion: string | undefined): Promise<vo
if (!config.get<boolean>('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;
}
Expand All @@ -350,7 +371,41 @@ async function warnOnCoreMismatch(serverVersion: string | undefined): Promise<vo
await config.update('warnOnCoreMismatch', false, ConfigurationTarget.Global);
}
} catch (err) {
console.warn(`Comline: couldn't compare the language server's and the CLI's core ('comline --version' failed): ${err}`);
console.warn(`Comline: couldn't warn about the language server's and the CLI's core: ${err}`);
}
}

/** Finds the CLI and runs `comline --version` once, for the status bar's
* tooltip; resolves to its output, or undefined when there is no CLI or it
* failed. Never throws. */
async function loadCliStatus(): Promise<string | undefined> {
const config = workspace.getConfiguration('comline');
const mode = effectiveMode(
'cli',
config.get<string>('cli.mode', TOOLS.cli.defaultMode),
config.get<string>('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();
}
}

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions src/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
interpretHostProbe,
launchFor,
pathDirectories,
planLocation,
planProgram,
resolveExecutable,
} from './resolve';
Expand All @@ -31,11 +32,11 @@
}

const linux = (files: string[], env: NodeJS.ProcessEnv = {}): Probe =>
machine('linux', files, { PATH: '/usr/local/bin:/usr/bin', ...env });

Check warning on line 35 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `PATH` must match one of the following formats: camelCase

// A Flatpak sandbox: the runtime's PATH, FLATPAK_ID, flatpak-spawn present.
const flatpak = (files: string[] = ['/usr/bin/flatpak-spawn'], env: NodeJS.ProcessEnv = {}): Probe =>
machine('linux', files, { PATH: '/app/bin:/usr/bin', FLATPAK_ID: 'com.vscodium.codium', ...env });

Check warning on line 39 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `FLATPAK_ID` must match one of the following formats: camelCase

Check warning on line 39 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `PATH` must match one of the following formats: camelCase

// -- PATH ---------------------------------------------------------------

Expand All @@ -61,7 +62,7 @@
});

test('empty PATH entries are dropped; an unset PATH is empty', () => {
assert.deepEqual(pathDirectories({ PATH: '/a::/b:' }, 'linux'), ['/a', '/b']);

Check warning on line 65 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `PATH` must match one of the following formats: camelCase
assert.deepEqual(pathDirectories({}, 'linux'), []);
});

Expand All @@ -76,8 +77,8 @@

test('Windows: PATHEXT supplies the extension, Path is case-insensitive', () => {
const probe = machine('win32', ['C:\\tools\\comline-lsp.exe'], {
Path: 'C:\\Windows;"C:\\tools"',

Check warning on line 80 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `Path` must match one of the following formats: camelCase
PATHEXT: '.com;.exe;.bat',

Check warning on line 81 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `PATHEXT` must match one of the following formats: camelCase
});
assert.deepEqual(resolveExecutable('comline-lsp', probe), {
found: 'C:\\tools\\comline-lsp.exe',
Expand All @@ -87,16 +88,16 @@

test('Windows: extensions are tried in PATHEXT order, a bare file without one is not a program', () => {
const both = machine('win32', ['C:\\t\\comline.bat', 'C:\\t\\comline.exe'], {
PATH: 'C:\\t',

Check warning on line 91 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `PATH` must match one of the following formats: camelCase
PATHEXT: '.exe;.bat',

Check warning on line 92 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `PATHEXT` must match one of the following formats: camelCase
});
assert.equal(resolveExecutable('comline', both).found, 'C:\\t\\comline.exe');
const bare = machine('win32', ['C:\\t\\comline'], { PATH: 'C:\\t' });

Check warning on line 95 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `PATH` must match one of the following formats: camelCase
assert.equal(resolveExecutable('comline', bare).found, undefined);
});

test('Windows: a name that already has a PATHEXT extension is tried as it is', () => {
const probe = machine('win32', ['C:\\t\\comline-lsp.exe'], { PATH: 'C:\\t' });

Check warning on line 100 in src/resolve.test.ts

View workflow job for this annotation

GitHub Actions / Lint and Compile

Object Literal Property name `PATH` must match one of the following formats: camelCase
assert.equal(resolveExecutable('comline-lsp.exe', probe).found, 'C:\\t\\comline-lsp.exe');
});

Expand Down Expand Up @@ -331,6 +332,7 @@
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,
Expand Down Expand Up @@ -383,3 +385,16 @@
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);
});
30 changes: 25 additions & 5 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 };

Expand All @@ -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' };
Expand Down Expand Up @@ -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;
}
}
Loading