diff --git a/src/features/settings/__tests__/containerInterpreter.test.tsx b/src/features/settings/__tests__/containerInterpreter.test.tsx index f07a093d4..45d88340f 100644 --- a/src/features/settings/__tests__/containerInterpreter.test.tsx +++ b/src/features/settings/__tests__/containerInterpreter.test.tsx @@ -104,3 +104,16 @@ describe("run target interpreter", () => { expect(mocks.container).not.toHaveBeenCalled(); }); }); + +describe("missing interpreter", () => { + it("reports the applied interpreter gone, not one chosen and not applied yet", async () => { + const { stage } = await import("../model/staging"); + mocks.effective.mockResolvedValue(ok("/usr/bin/python3")); + mount("/gone/bin/python"); + expect(await screen.findByRole("alert")).toHaveTextContent("no longer exists"); + // An environment just created, staged until Apply: the backend still resolves the old setting. + stage("python.interpreter", "project", "languages.python.interpreter", "/p/.venv/bin/python"); + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(screen.queryByRole("alert")).toBeNull(); + }); +}); diff --git a/src/features/settings/components/context.ts b/src/features/settings/components/context.ts index d8422c3f1..ee9837348 100644 --- a/src/features/settings/components/context.ts +++ b/src/features/settings/components/context.ts @@ -33,6 +33,15 @@ export function useSetting(key: string, scopeOverride?: Scope): [T, (value: T return [value, set]; } +/** The value of a settings key as last applied, without this dialog's staged edits. */ +export function useAppliedSetting(key: string, scopeOverride?: Scope): T { + const { scope: pageScope } = usePageContext(); + const scope = scopeOverride ?? pageScope; + const base = useSettingsDialog((s) => s.base); + const globalBase = useSettingsDialog((s) => s.globalBase); + return useMemo(() => stagedValue({ base, globalBase, staged: {} }, scope, key), [base, globalBase, scope, key]) as T; +} + /** `stagedValue` for components; recomputed only when the base or the staged edits change. */ export function useStagedValue(scope: Scope, key: string): unknown { const base = useSettingsDialog((s) => (scope === "global" ? s.globalBase : s.base)); diff --git a/src/features/settings/languages/PythonInterpreterPage.tsx b/src/features/settings/languages/PythonInterpreterPage.tsx index 45d4cc97f..467d7b7ff 100644 --- a/src/features/settings/languages/PythonInterpreterPage.tsx +++ b/src/features/settings/languages/PythonInterpreterPage.tsx @@ -24,7 +24,7 @@ import { } from "./api"; import { validateRunTargets, type RunTargetValid } from "@/platform/remoteSettings"; import { AddInterpreterDialog } from "./AddInterpreterDialog"; -import { useSetting } from "../components/context"; +import { useAppliedSetting, useSetting } from "../components/context"; import { SdkField, tildify } from "./components"; import { interpreterIcon, sameInterpreterPath, usePythonInterpreters } from "./interpreters"; import styles from "./Sdk.module.css"; @@ -160,21 +160,25 @@ export function PythonInterpreterPage() { }, [root, created]); // The interpreter runs resolve to: the configured one while it exists, else the fallback. + // The backend resolves the applied setting, so only that one can be judged here: a choice + // not applied yet (an environment just created) is not reported missing. + const applied = useAppliedSetting("languages.python.interpreter"); const [resolved, setResolved] = useState<{ interpreter: string; path: string | null } | null>(null); useEffect(() => { - if (!interpreter) return; + if (!applied) return; let live = true; effectivePython() - .then((path) => live && setResolved({ interpreter, path })) + .then((path) => live && setResolved({ interpreter: applied, path })) .catch(() => live && setResolved(null)); return () => { live = false; }; - }, [root, interpreter, created]); + }, [root, applied, created]); // A configured interpreter whose executable is gone (PyCharm's "Invalid"): runs fall back. const invalid = !target && !!interpreter && + interpreter === applied && resolved?.interpreter === interpreter && !sameInterpreter(resolved.path, interpreter, root);