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