fix(vscode): detect workspace before runtime registration

This commit is contained in:
Bohdan Triapitsyn
2026-07-21 21:24:06 +03:00
parent a0caec0984
commit d2efa707ff
3 changed files with 37 additions and 14 deletions
@@ -0,0 +1,15 @@
import { describe, expect, test } from 'bun:test';
import { isVSCodeRuntime } from './vscodeRuntime';
describe('VS Code runtime detection', () => {
test('uses extension-host bootstrap config before runtime APIs are registered', () => {
expect(isVSCodeRuntime(null, {
workspaceFolder: '/workspace/project-one',
workspaceFolders: [{ name: 'project-one', path: '/workspace/project-one' }],
})).toBe(true);
});
test('does not classify an unregistered web runtime as VS Code', () => {
expect(isVSCodeRuntime(null, null)).toBe(false);
});
});
@@ -0,0 +1,18 @@
import type { RuntimeAPIs } from '@/lib/api/types';
export interface VSCodeBootstrapConfig {
workspaceFolder?: unknown;
workspaceFolders?: unknown;
}
export const getVSCodeBootstrapConfig = (): VSCodeBootstrapConfig | null => {
if (typeof window === 'undefined') {
return null;
}
return (window as unknown as { __VSCODE_CONFIG__?: VSCodeBootstrapConfig }).__VSCODE_CONFIG__ ?? null;
};
export const isVSCodeRuntime = (
runtimeApis: RuntimeAPIs | null,
bootstrapConfig = getVSCodeBootstrapConfig(),
): boolean => Boolean(bootstrapConfig || runtimeApis?.runtime?.isVSCode);