From d5ba63e3336d065720819ae16ecfb1f676d7b017 Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Sat, 28 Nov 2026 15:57:21 +0900 Subject: [PATCH] fix(python): accept direct references and bzr+lp URLs, report a missing path, and keep editable cleared after an archive (#6650) Follow-up to #6617. The version control dialog now accepts pip's direct references (name @ git+https://..., with extras) and bzr+lp:project. Installing from a local path that does not exist fails before pip runs, with the path in the message; a trailing [extras] is not part of the path, and a folder whose name has brackets is still a folder. Typing an archive path clears the editable choice, so a directory typed after it starts unticked. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- crates/ide-sdk/src/python.rs | 10 ++++++- crates/ide-sdk/src/tests.rs | 26 +++++++++++++++++++ .../python-packages/PackagesView.test.tsx | 5 ++++ src/features/python-packages/PackagesView.tsx | 13 +++++----- src/features/python-packages/model.test.ts | 16 ++++++++++++ src/features/python-packages/model.ts | 8 ++++-- 6 files changed, 68 insertions(+), 10 deletions(-) diff --git a/crates/ide-sdk/src/python.rs b/crates/ide-sdk/src/python.rs index 093066253..00da59f91 100644 --- a/crates/ide-sdk/src/python.rs +++ b/crates/ide-sdk/src/python.rs @@ -637,8 +637,16 @@ pub fn install_package( return Err(IdeError::invalid("enter a package name or requirement")); } let requirement = resolve_requirement(requirement, project_root)?; + // A path (a requirement never starts with a root), maybe with `[extras]` after it. + let path = Path::new(&requirement).has_root().then_some(requirement.as_str()); + if let Some(path) = path { + let without_extras = path.strip_suffix(']').and_then(|p| p.rsplit_once('[')).map(|(p, _)| p); + if !Path::new(path).exists() && !without_extras.is_some_and(|p| Path::new(p).exists()) { + return Err(IdeError::not_found(format!("{} does not exist", without_extras.unwrap_or(path)))); + } + } // An editable clone of a repository would go where pip runs, a temporary folder. - if editable && !Path::new(&requirement).is_absolute() { + if editable && path.is_none() { return Err(IdeError::invalid("only a local directory can be installed in editable mode")); } let installer = installer(python)?; diff --git a/crates/ide-sdk/src/tests.rs b/crates/ide-sdk/src/tests.rs index 1185d317b..cc5ce7dc2 100644 --- a/crates/ide-sdk/src/tests.rs +++ b/crates/ide-sdk/src/tests.rs @@ -1919,6 +1919,32 @@ fn editable_installs_put_e_right_before_the_path() { assert_eq!(install_args("six", true, false), ["install", "--upgrade", "six"]); } +#[test] +fn a_missing_local_path_is_reported_before_pip_runs() { + let root = tempfile::tempdir().unwrap(); + let install = |requirement: &str| { + crate::python::install_package( + Path::new("/nonexistent/python"), + requirement, + false, + false, + Some(root.path()), + &CancellationToken::new(), + &|_| {}, + ) + .unwrap_err() + .to_string() + }; + assert!(install("./mylib").contains("does not exist"), "{}", install("./mylib")); + assert!(install("./mylib[dev]").contains("does not exist")); + // An existing folder gets as far as looking for pip. + std::fs::create_dir(root.path().join("mylib")).unwrap(); + assert!(!install("./mylib[dev]").contains("does not exist")); + // A folder whose name has brackets is a folder, not extras. + std::fs::create_dir(root.path().join("lib[1]")).unwrap(); + assert!(!install("./lib[1]").contains("does not exist"), "{}", install("./lib[1]")); +} + #[test] fn only_a_local_directory_is_installed_in_editable_mode() { let err = crate::python::install_package( diff --git a/src/features/python-packages/PackagesView.test.tsx b/src/features/python-packages/PackagesView.test.tsx index 9689a4280..6a4687c99 100644 --- a/src/features/python-packages/PackagesView.test.tsx +++ b/src/features/python-packages/PackagesView.test.tsx @@ -156,8 +156,13 @@ describe("Python Packages", () => { const path = await screen.findByRole("textbox", { name: "Path" }); fireEvent.change(path, { target: { value: "--index-url=https://evil.example" } }); expect(screen.getByRole("button", { name: "Install" })).toBeDisabled(); + // Editable, then an archive, then a directory again: the archive cleared the choice. + fireEvent.change(path, { target: { value: "pkgs/mylib" } }); + fireEvent.click(screen.getByRole("checkbox", { name: "Install as editable (-e)" })); fireEvent.change(path, { target: { value: "dist/mylib-0.1.0-py3-none-any.whl" } }); expect(screen.getByRole("checkbox", { name: "Install as editable (-e)" })).toBeDisabled(); + fireEvent.change(path, { target: { value: "pkgs/mylib" } }); + expect(screen.getByRole("checkbox", { name: "Install as editable (-e)" })).not.toBeChecked(); // A bare name is a directory here, never a PyPI package. fireEvent.change(path, { target: { value: "mylib" } }); fireEvent.click(screen.getByRole("checkbox", { name: "Install as editable (-e)" })); diff --git a/src/features/python-packages/PackagesView.tsx b/src/features/python-packages/PackagesView.tsx index 1d6708ef3..ead1193b7 100644 --- a/src/features/python-packages/PackagesView.tsx +++ b/src/features/python-packages/PackagesView.tsx @@ -546,7 +546,11 @@ function InstallFromDialog({ source === "vcs" ? "git+https://github.com/owner/project.git" : tx("Project directory or archive") } aria-label={source === "vcs" ? "Repository URL" : "Path"} - onChange={(e) => setValue(e.target.value)} + onChange={(e) => { + setValue(e.target.value); + // An archive can't be editable; a directory typed after it starts unticked. + if (isArchive(e.target.value)) setEditable(false); + }} onKeyDown={(e) => e.key === "Enter" && install()} /> {source === "disk" && ( @@ -562,12 +566,7 @@ function InstallFromDialog({ )} {source === "disk" && ( - + )} diff --git a/src/features/python-packages/model.test.ts b/src/features/python-packages/model.test.ts index 4c0f11dfc..c3efb48ec 100644 --- a/src/features/python-packages/model.test.ts +++ b/src/features/python-packages/model.test.ts @@ -100,6 +100,21 @@ describe("install from version control or disk", () => { expect(isVcsUrl("hg+https://hg.example.org/demo")).toBe(true); expect(isVcsUrl(vcsRequirement("requests"))).toBe(false); expect(isVcsUrl(vcsRequirement("file:///tmp/pkg"))).toBe(false); + for (const url of [ + "git+ssh://git@github.com/psf/requests.git", + "git+file:///work/repo", + "hg+https://hg.example.org/demo", + "svn+svn://svn.example.org/demo/trunk", + "bzr+lp:demo", + "git+https://github.com/psf/requests.git@v2.32.0", + "git+https://github.com/o/r.git#egg=demo&subdirectory=pkgs/demo", + "requests @ git+https://github.com/psf/requests.git", + "demo[cli] @ git+https://github.com/o/r.git", + ]) + expect(isVcsUrl(url), url).toBe(true); + expect(isVcsUrl("requests @ https://example.org/requests.zip")).toBe(false); + for (const url of ["git+https:foo", "git+ssh:git@host:o/r", "requests @ git+https:foo"]) + expect(isVcsUrl(url), url).toBe(false); }); it("never lets a typed path read as a PyPI name", () => { @@ -112,5 +127,6 @@ describe("install from version control or disk", () => { expect(isArchive("dist/demo-1.0.tar.gz")).toBe(true); expect(isArchive("dist/demo-1.0-py3-none-any.whl")).toBe(true); expect(isArchive("pkgs/demo")).toBe(false); + expect(isArchive(" dist/demo-1.0-py3-none-any.whl ")).toBe(true); }); }); diff --git a/src/features/python-packages/model.ts b/src/features/python-packages/model.ts index a10376074..80bb3244b 100644 --- a/src/features/python-packages/model.ts +++ b/src/features/python-packages/model.ts @@ -68,8 +68,12 @@ export function vcsRequirement(url: string) { return scp ? `git+ssh://${scp[1]}/${scp[2]}` : text; } -/** Whether `url` (after {@link vcsRequirement}) is a version control URL pip can install. */ -export const isVcsUrl = (url: string) => /^(git|hg|svn|bzr)\+[a-z]+:\/\/\S+$/i.test(url); +/** + * Whether `url` (after {@link vcsRequirement}) is a version control URL pip can install, bare + * (`git+https://…`, `bzr+lp:project`) or as a direct reference (`name @ git+https://…`). + */ +export const isVcsUrl = (url: string) => + /^([A-Za-z0-9][\w.-]*(\[[^\]]*\])?\s*@\s*)?((git|hg|svn|bzr)\+[a-z]+:\/\/|bzr\+lp:)\S+$/i.test(url); /** * A path typed for Install from Disk, made explicit so pip never reads it as a PyPI name: a