fix(python): remember added interpreters after switching (#8228)

Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-06 20:35:58 +08:00
1 джерело 58ccb9dbc9
коміт fb6d60a577
3 files changed
+92 -5

No files matched your search

+33
Переглянути файл
@@ -8,6 +8,8 @@ const mocks = vi.hoisted(() => ({
projects: vi.fn(),
interpreter: vi.fn(),
createVenv: vi.fn(),
effective: vi.fn(),
packages: vi.fn(),
confirm: vi.fn(),
globalSettings: vi.fn(),
sysPath: vi.fn(),
@@ -26,6 +28,9 @@ vi.mock("@/platform/ipc", async (importOriginal) => {
sdkPythonProjectInterpreters: mocks.projects,
sdkPythonInterpreter: mocks.interpreter,
sdkPythonCreateVenv: mocks.createVenv,
sdkPythonEffective: mocks.effective,
sdkPythonPackages: mocks.packages,
sdkPythonHatchEnvs: vi.fn(async () => ({ status: "ok", data: [] })),
sdkPythonSysPath: mocks.sysPath,
sdkPythonUserPathProblems: mocks.pathProblems,
settingsGetGlobal: mocks.globalSettings,
@@ -37,6 +42,7 @@ vi.mock("@/platform/choosePath", () => ({ choosePath: mocks.choosePath }));
vi.mock("@/workbench/messages", () => ({ confirm: mocks.confirm, ask: vi.fn(), showError: vi.fn() }));
const { PythonInterpretersPage } = await import("../languages/PythonInterpretersPage");
const { PythonInterpreterPage } = await import("../languages/PythonInterpreterPage");
const { useSdks } = await import("../languages/api");
const { PageContext } = await import("../components/context");
const { DEFAULT_SETTINGS } = await import("../model/schema");
@@ -44,6 +50,7 @@ const { openSettings, useSettingsDialog } = await import("../model/staging");
const ok = <T,>(data: T) => Promise.resolve({ status: "ok" as const, data });
const PYTHON_INTERPRETERS_CONTEXT = { pageId: "python.interpreters", scope: "global" } as const;
const PYTHON_DEPENDENCIES_CONTEXT = { pageId: "python.interpreter", scope: "project" } as const;
const sdk = (path: string, name: string, home: string | null): Sdk => ({
path,
@@ -94,6 +101,8 @@ beforeEach(() => {
ok(path === PROJECT.path ? PROJECT : path === EXTRA.path ? EXTRA : null),
);
mocks.confirm.mockResolvedValue(true);
mocks.effective.mockResolvedValue(ok(PROJECT.path));
mocks.packages.mockResolvedValue(ok([]));
mocks.sysPath.mockImplementation(() => ok(["/usr/lib/python3.12", "/usr/lib/python3.12/site-packages"]));
mocks.pathProblems.mockImplementation((paths: string[]) =>
ok(paths.map((path) => (path.includes("gone") ? "missing" : path.includes(":") ? "invalid" : null))),
@@ -105,6 +114,30 @@ afterEach(() => {
cleanup();
});
describe("Python dependencies page", () => {
it("registers an added local environment globally so switching away does not forget it", async () => {
Object.assign(window, { __TAURI_INTERNALS__: { transformCallback: () => 1 } });
const custom = sdk("/project/.venv-alt/bin/python", "Python 3.12 (backend)", "/project/.venv-alt");
const base = settings({ interpreters: [EXTRA.path], hiddenInterpreters: [OLD.path, custom.path] });
useProject.setState({ settings: base, project: { root: "/project", name: "backend" } as ProjectInfo });
useSettingsDialog.setState({ base, globalBase: base, staged: {}, query: "" });
mocks.createVenv.mockResolvedValue(ok(custom));
render(
<PageContext.Provider value={PYTHON_DEPENDENCIES_CONTEXT}>
<PythonInterpreterPage />
</PageContext.Provider>,
);
fireEvent.click(screen.getByRole("button", { name: "Add Interpreter…" }));
await screen.findByRole("button", { name: "Base interpreter" });
fireEvent.change(screen.getByLabelText("Location:"), { target: { value: "/project/.venv-alt" } });
fireEvent.click(screen.getByRole("button", { name: "OK" }));
await waitFor(() => expect(stageOf("project|languages.python.interpreter")).toBe(custom.path));
expect(stageOf("global|languages.python.interpreters")).toEqual([EXTRA.path, custom.path]);
expect(stageOf("global|languages.python.hiddenInterpreters")).toEqual([OLD.path]);
expect(stageOf("project|languages.python.interpreters")).toBeUndefined();
});
});
describe("Python interpreters page", () => {
it("lists detected, other-project and added interpreters, minus the removed ones", async () => {
page({ interpreters: [EXTRA.path], hiddenInterpreters: [OLD.path] });
+5
Переглянути файл
@@ -101,6 +101,8 @@ export function PythonInterpreterPage() {
const [created, setCreated] = useState(0);
// Detected interpreters plus the ones added to (or hidden from) the IDE-wide list.
const { detected, rows, hidden } = usePythonInterpreters({ withProjects: false });
const [registered, setRegistered] = useSetting<string[]>("languages.python.interpreters", "global");
const [hiddenPaths, setHiddenPaths] = useSetting<string[]>("languages.python.hiddenInterpreters", "global");
const extras = useMemo(
() =>
rows
@@ -478,6 +480,9 @@ export function PythonInterpreterPage() {
root={root}
onClose={() => setAdding(false)}
onAdded={(path) => {
const same = (candidate: string) => sameInterpreterPath(candidate, path, null);
setHiddenPaths((hiddenPaths ?? []).filter((candidate) => !same(candidate)));
setRegistered([...(registered ?? []).filter((candidate) => !same(candidate)), path]);
setInterpreter(path);
setCreated((n) => n + 1);
reload();