fix(ui): detect VS Code from bootstrap config in shared runtime helpers
d2efa707 fixed projects-store detection via __VSCODE_CONFIG__, but
lib/desktop.isVSCodeRuntime (used by useDirectoryStore) still required
RuntimeAPIs. At webview startup that left directory init on stale
localStorage paths from other windows (#2359).
Share bootstrap detection in lib/vscodeBootstrap and use it from both
desktop runtime checks and the projects-store helper.
Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
This commit is contained in:
co-authored by
Serhii Dziupin
parent
8801d69c66
commit
ccb74d8366
@@ -4,6 +4,7 @@ import type { DraftStarterRef } from '@/lib/draftStarters';
|
||||
import type { MobileKeyboardMode } from '@/lib/mobileKeyboardMode';
|
||||
import { getRuntimeApiBaseUrl, getRuntimeKey } from '@/lib/runtime-switch';
|
||||
import { getRegisteredRuntimeAPIs } from '@/contexts/runtimeAPIRegistry';
|
||||
import { isVSCodeBootstrapPresent } from '@/lib/vscodeBootstrap';
|
||||
|
||||
type ManagedRemoteTunnelPreset = {
|
||||
id: string;
|
||||
@@ -538,6 +539,12 @@ export const startDesktopWindowDrag = async (): Promise<boolean> => {
|
||||
};
|
||||
|
||||
export const isVSCodeRuntime = (): boolean => {
|
||||
// Prefer extension-host bootstrap config: it is injected in webview HTML
|
||||
// before any store module evaluates, so startup does not depend on
|
||||
// RuntimeAPIs registration order (see #2359).
|
||||
if (isVSCodeBootstrapPresent()) {
|
||||
return true;
|
||||
}
|
||||
const apis = getRegisteredRuntimeAPIs();
|
||||
return apis?.runtime?.isVSCode === true;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { afterEach, describe, expect, mock, test } from 'bun:test';
|
||||
|
||||
type RuntimeApisStub = { runtime?: { isVSCode?: boolean } } | null;
|
||||
|
||||
let registeredRuntimeApis: RuntimeApisStub = null;
|
||||
|
||||
mock.module('@/contexts/runtimeAPIRegistry', () => ({
|
||||
getRegisteredRuntimeAPIs: (): RuntimeApisStub => registeredRuntimeApis,
|
||||
}));
|
||||
|
||||
const { isVSCodeRuntime } = await import('./desktop');
|
||||
|
||||
describe('desktop isVSCodeRuntime bootstrap detection', () => {
|
||||
afterEach(() => {
|
||||
registeredRuntimeApis = null;
|
||||
delete (globalThis as { window?: unknown }).window;
|
||||
});
|
||||
|
||||
test('detects VS Code from bootstrap config before RuntimeAPIs register', () => {
|
||||
registeredRuntimeApis = null;
|
||||
(globalThis as { window: unknown }).window = {
|
||||
__VSCODE_CONFIG__: {
|
||||
workspaceFolder: '/Users/me/project-a',
|
||||
workspaceFolders: [{ name: 'project-a', path: '/Users/me/project-a' }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(isVSCodeRuntime()).toBe(true);
|
||||
});
|
||||
|
||||
test('falls back to registered RuntimeAPIs when bootstrap is absent', () => {
|
||||
registeredRuntimeApis = {
|
||||
runtime: { isVSCode: true },
|
||||
};
|
||||
(globalThis as { window: unknown }).window = {};
|
||||
|
||||
expect(isVSCodeRuntime()).toBe(true);
|
||||
});
|
||||
|
||||
test('does not classify an unregistered web runtime as VS Code', () => {
|
||||
registeredRuntimeApis = null;
|
||||
(globalThis as { window: unknown }).window = {};
|
||||
|
||||
expect(isVSCodeRuntime()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import { getVSCodeBootstrapConfig, isVSCodeBootstrapPresent } from './vscodeBootstrap';
|
||||
|
||||
describe('VS Code bootstrap config', () => {
|
||||
afterEach(() => {
|
||||
delete (globalThis as { window?: unknown }).window;
|
||||
});
|
||||
|
||||
test('reads extension-host __VSCODE_CONFIG__ before RuntimeAPIs exist', () => {
|
||||
(globalThis as { window: unknown }).window = {
|
||||
__VSCODE_CONFIG__: {
|
||||
workspaceFolder: '/workspace/project-one',
|
||||
workspaceFolders: [{ name: 'project-one', path: '/workspace/project-one' }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(getVSCodeBootstrapConfig()).toEqual({
|
||||
workspaceFolder: '/workspace/project-one',
|
||||
workspaceFolders: [{ name: 'project-one', path: '/workspace/project-one' }],
|
||||
});
|
||||
expect(isVSCodeBootstrapPresent()).toBe(true);
|
||||
});
|
||||
|
||||
test('treats missing window/bootstrap as not VS Code', () => {
|
||||
expect(getVSCodeBootstrapConfig()).toBeNull();
|
||||
expect(isVSCodeBootstrapPresent()).toBe(false);
|
||||
expect(isVSCodeBootstrapPresent(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Extension-host bootstrap config injected into the VS Code webview HTML
|
||||
* before any bundled module evaluates. Prefer this over RuntimeAPIs for
|
||||
* early VS Code detection during store module initialization.
|
||||
*/
|
||||
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 isVSCodeBootstrapPresent = (
|
||||
bootstrapConfig: VSCodeBootstrapConfig | null = getVSCodeBootstrapConfig(),
|
||||
): boolean => Boolean(bootstrapConfig);
|
||||
@@ -2,6 +2,7 @@ import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { opencodeClient } from '@/lib/opencode/client';
|
||||
import { getDesktopHomeDirectory, isVSCodeRuntime } from '@/lib/desktop';
|
||||
import { getVSCodeBootstrapConfig } from '@/lib/vscodeBootstrap';
|
||||
import { subscribeRuntimeEndpointChanged } from '@/lib/runtime-switch';
|
||||
import { updateDesktopSettings } from '@/lib/persistence';
|
||||
import { useFileSearchStore } from '@/stores/useFileSearchStore';
|
||||
@@ -227,7 +228,7 @@ const getVsCodeWorkspaceFolder = (): string | null => {
|
||||
if (!isVSCodeRuntime()) {
|
||||
return null;
|
||||
}
|
||||
const workspaceFolder = (window as unknown as { __VSCODE_CONFIG__?: { workspaceFolder?: unknown } }).__VSCODE_CONFIG__?.workspaceFolder;
|
||||
const workspaceFolder = getVSCodeBootstrapConfig()?.workspaceFolder;
|
||||
if (typeof workspaceFolder !== 'string' || workspaceFolder.trim().length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import type { RuntimeAPIs } from '@/lib/api/types';
|
||||
import { isVSCodeRuntime } from './vscodeRuntime';
|
||||
|
||||
describe('VS Code runtime detection', () => {
|
||||
@@ -9,6 +10,13 @@ describe('VS Code runtime detection', () => {
|
||||
})).toBe(true);
|
||||
});
|
||||
|
||||
test('uses registered runtime APIs when bootstrap is absent', () => {
|
||||
const runtimeApis = {
|
||||
runtime: { platform: 'vscode', isDesktop: false, isVSCode: true },
|
||||
} as RuntimeAPIs;
|
||||
expect(isVSCodeRuntime(runtimeApis, null)).toBe(true);
|
||||
});
|
||||
|
||||
test('does not classify an unregistered web runtime as VS Code', () => {
|
||||
expect(isVSCodeRuntime(null, null)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
import type { RuntimeAPIs } from '@/lib/api/types';
|
||||
import {
|
||||
getVSCodeBootstrapConfig,
|
||||
isVSCodeBootstrapPresent,
|
||||
type VSCodeBootstrapConfig,
|
||||
} from '@/lib/vscodeBootstrap';
|
||||
|
||||
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 type { VSCodeBootstrapConfig };
|
||||
export { getVSCodeBootstrapConfig };
|
||||
|
||||
export const isVSCodeRuntime = (
|
||||
runtimeApis: RuntimeAPIs | null,
|
||||
bootstrapConfig = getVSCodeBootstrapConfig(),
|
||||
): boolean => Boolean(bootstrapConfig || runtimeApis?.runtime?.isVSCode);
|
||||
bootstrapConfig: VSCodeBootstrapConfig | null = getVSCodeBootstrapConfig(),
|
||||
): boolean => Boolean(isVSCodeBootstrapPresent(bootstrapConfig) || runtimeApis?.runtime?.isVSCode);
|
||||
|
||||
Reference in New Issue
Block a user