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
+4 -14
View File
@@ -13,6 +13,7 @@ import { PROJECT_COLORS } from '@/lib/projectMeta';
import { useSessionUIStore } from '@/sync/session-ui-store';
import { runtimeFetch } from '@/lib/runtime-fetch';
import { getRuntimeApiBaseUrl } from '@/lib/runtime-switch';
import { getVSCodeBootstrapConfig, isVSCodeRuntime } from './utils/vscodeRuntime';
/** Pick a color key that's least used among existing projects */
const pickAutoColor = (projects: ProjectEntry[]): string => {
@@ -416,21 +417,11 @@ const createVSCodeWorkspaceProject = (
};
const getVSCodeWorkspaceFolders = (): VSCodeWorkspaceFolderConfig[] | null => {
if (typeof window === 'undefined') {
return null;
}
const runtimeApis = getRegisteredRuntimeAPIs();
if (!runtimeApis?.runtime?.isVSCode) {
const config = getVSCodeBootstrapConfig();
if (!isVSCodeRuntime(runtimeApis, config)) {
return null;
}
const config = (window as unknown as {
__VSCODE_CONFIG__?: {
workspaceFolder?: unknown;
workspaceFolders?: unknown;
};
}).__VSCODE_CONFIG__;
const folders = Array.isArray(config?.workspaceFolders)
? config.workspaceFolders
.map((entry) => {
@@ -542,8 +533,7 @@ const getVSCodeWorkspaceProject = (): { projects: ProjectEntry[]; activeProjectI
// Always prefer the VS Code workspace projects over any persisted multi-project registry.
const vscodeWorkspace = getVSCodeWorkspaceProject();
const isVSCodeProjectsRuntime = (() => {
if (typeof window === 'undefined') return false;
return Boolean(getRegisteredRuntimeAPIs()?.runtime?.isVSCode);
return isVSCodeRuntime(getRegisteredRuntimeAPIs(), getVSCodeBootstrapConfig());
})();
const effectiveInitialProjects = vscodeWorkspace?.projects ?? (isVSCodeProjectsRuntime ? [] : initialProjects);
const persistedInitialActiveProjectId = vscodeWorkspace?.activeProjectId ?? (isVSCodeProjectsRuntime ? null : readPersistedActiveProjectId());
@@ -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);