дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
fix(python): remember added interpreters after switching (#8228)
Цей коміт міститься в:
1 джерело
58ccb9dbc9
коміт
fb6d60a577
3 files changed
+92
-5
No files matched your search
@@ -50,13 +50,23 @@ pub async fn sdk_detect(host: &Host, window: &str, kind: SdkKind) -> IdeResult<V
|
||||
(Some(p), SdkKind::Python) => ide_sdk::apply::python_interpreter(&p.root, &p.settings.effective()),
|
||||
_ => None,
|
||||
};
|
||||
// Explicitly added interpreters belong to the user, not to the project being opened.
|
||||
// Include them in discovery for every selector, even after a different interpreter is chosen.
|
||||
let registered = if kind == SdkKind::Python {
|
||||
host.global_settings.effective().languages.python.interpreters
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
blocking_value(move || {
|
||||
let mut sdks = ide_sdk::detect(kind, root.as_deref());
|
||||
if let Some(path) = current
|
||||
&& !sdks.iter().any(|s| *s.path == *path)
|
||||
&& let Some(sdk) = ide_sdk::python_interpreter_sdk(&path, root.as_deref())
|
||||
{
|
||||
sdks.push(sdk);
|
||||
let registered = registered.into_iter().map(PathBuf::from).filter(|path| path.is_absolute());
|
||||
for path in current.into_iter().chain(registered) {
|
||||
if !sdks.iter().any(|s| *s.path == *path)
|
||||
&& let Some(sdk) = ide_sdk::python_interpreter_sdk(&path, root.as_deref())
|
||||
&& !sdks.iter().any(|s| s.path == sdk.path)
|
||||
{
|
||||
sdks.push(sdk);
|
||||
}
|
||||
}
|
||||
sdks
|
||||
})
|
||||
@@ -674,6 +684,45 @@ mod tests {
|
||||
host.close_project("main");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn registered_python_interpreters_remain_detectable_after_switch_and_reopen() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let base = dunce::canonicalize(temp.path()).unwrap();
|
||||
let root = base.join("project");
|
||||
let a = fake_venv(&root, ".venv");
|
||||
let b = fake_venv(&root, ".venv-alt");
|
||||
let missing = root.join("missing/bin/python");
|
||||
let host = Host::new(HostConfig { local_data_dir: base.join("local"), ..HostConfig::for_test(&base) });
|
||||
host.trust.decide(&root, true, false).unwrap();
|
||||
host.global_settings
|
||||
.set(
|
||||
ide_workspace::SettingsScope::Global,
|
||||
"languages.python.interpreters",
|
||||
serde_json::json!([a, b, b, missing]),
|
||||
)
|
||||
.unwrap();
|
||||
host.open_project("main", &root).unwrap();
|
||||
let project = host.project("main").unwrap();
|
||||
for python in [&b, &a] {
|
||||
project
|
||||
.settings
|
||||
.set(
|
||||
ide_workspace::SettingsScope::Project,
|
||||
"languages.python.interpreter",
|
||||
serde_json::to_value(python).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
host.close_project("main");
|
||||
host.open_project("main", &root).unwrap();
|
||||
let detected = sdk_detect(&host, "main", SdkKind::Python).await.unwrap();
|
||||
assert_eq!(detected.iter().filter(|s| *s.path == a).count(), 1);
|
||||
assert_eq!(detected.iter().filter(|s| *s.path == b).count(), 1);
|
||||
assert!(!detected.iter().any(|s| *s.path == missing));
|
||||
assert_eq!(sdk_python_effective(&host, "main", None).await.unwrap(), Some(a.into()));
|
||||
host.close_project("main");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn another_projects_interpreter_is_listed_with_its_project() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -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] });
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Заблокувати користувача