fix(python): install from disk never falls back to PyPI, editable only for a local directory (#6617)

Install from Disk resolved only paths starting with '.', and pip runs in a temporary folder, so a bare name like 'mylib' installed the PyPI package of that name. A typed relative path is now sent as './...' (resolved against the project) and '~/' expands to the home folder, so pip always gets an absolute path. Editable installs are offered and accepted only for a local directory: an editable clone of a repository would land in pip's temporary folder. The version control dialog accepts only vcs+scheme:// URLs, and an archive disables editable.

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-28 11:09:48 +08:00
1 джерело 0b06d3313f
коміт 0b1a675fab
10 files changed
+137 -22

No files matched your search

+8 -1
Переглянути файл
@@ -132,6 +132,10 @@ describe("Python Packages", () => {
fireEvent.click(screen.getByRole("button", { name: "Install from Version Control or Disk" }));
act(() => pick("Install from Version Control…"));
const url = await screen.findByRole("textbox", { name: "Repository URL" });
// A name is not a repository, and editable is offered only for a local directory.
fireEvent.change(url, { target: { value: "requests" } });
expect(screen.getByRole("button", { name: "Install" })).toBeDisabled();
expect(screen.queryByRole("checkbox", { name: "Install as editable (-e)" })).toBeNull();
fireEvent.change(url, { target: { value: "https://github.com/benjaminp/six.git" } });
fireEvent.click(screen.getByRole("button", { name: "Install" }));
expect(mocks.pip).toHaveBeenLastCalledWith(
@@ -152,7 +156,10 @@ 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();
fireEvent.change(path, { target: { value: "./mylib" } });
fireEvent.change(path, { target: { value: "dist/mylib-0.1.0-py3-none-any.whl" } });
expect(screen.getByRole("checkbox", { name: "Install as editable (-e)" })).toBeDisabled();
// 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)" }));
fireEvent.click(screen.getByRole("button", { name: "Install" }));
expect(mocks.pip).toHaveBeenLastCalledWith(
+28 -9
Переглянути файл
@@ -30,7 +30,18 @@ import {
T,
} from "@/ui";
import { editorService } from "@/workbench/contributions";
import { isPackageName, listRows, normalize, requirementOf, rowKey, vcsRequirement, type Row } from "./model";
import {
diskRequirement,
isArchive,
isPackageName,
isVcsUrl,
listRows,
normalize,
requirementOf,
rowKey,
vcsRequirement,
type Row,
} from "./model";
import styles from "./PythonPackages.module.css";
type Op = "install" | "upgrade" | "uninstall";
@@ -492,7 +503,11 @@ export function PackagesView() {
type Source = "vcs" | "disk";
/** A repository URL or a local directory or archive to install, as PyCharm's Advanced Package Install. */
/**
* A repository URL or a local directory or archive to install, as PyCharm's Advanced Package
* Install. Only a local directory can be editable: an editable clone of a repository would go
* where pip runs, a temporary folder, unless the interpreter is a virtualenv, and uv can't do it.
*/
function InstallFromDialog({
source,
onClose,
@@ -506,9 +521,11 @@ function InstallFromDialog({
const [value, setValue] = useState("");
const [editable, setEditable] = useState(false);
const text = value.trim();
const requirement = source === "vcs" ? vcsRequirement(text) : diskRequirement(text);
// pip would read a leading `-` as one of its options.
const valid = !!text && !text.startsWith("-");
const install = () => valid && onInstall(source === "vcs" ? vcsRequirement(text) : text, editable);
const valid = !!text && !text.startsWith("-") && (source === "disk" || isVcsUrl(requirement));
const canEdit = source === "disk" && !isArchive(text);
const install = () => valid && onInstall(requirement, canEdit && editable);
return (
<Dialog
open
@@ -544,11 +561,13 @@ function InstallFromDialog({
</Button>
)}
</div>
<Checkbox checked={editable} onChange={setEditable} label="Install as editable (-e)" />
{source === "vcs" && editable && (
<div className={styles.note}>
<T>An editable install from version control needs the package name at the end of the URL: #egg=name.</T>
</div>
{source === "disk" && (
<Checkbox
checked={canEdit && editable}
onChange={setEditable}
disabled={!canEdit}
label="Install as editable (-e)"
/>
)}
</div>
</Dialog>
+32 -1
Переглянути файл
@@ -1,5 +1,15 @@
import { describe, expect, it } from "vitest";
import { isPackageName, listRows, normalize, requirementOf, rowKey, vcsRequirement } from "./model";
import {
isPackageName,
listRows,
normalize,
requirementOf,
rowKey,
vcsRequirement,
diskRequirement,
isArchive,
isVcsUrl,
} from "./model";
const INSTALLED = [
{ name: "requests", version: "2.31.0" },
@@ -83,3 +93,24 @@ describe("vcsRequirement", () => {
expect(vcsRequirement("hg+https://hg.example.org/demo")).toBe("hg+https://hg.example.org/demo");
});
});
describe("install from version control or disk", () => {
it("accepts only version control URLs", () => {
expect(isVcsUrl(vcsRequirement("https://github.com/psf/requests.git"))).toBe(true);
expect(isVcsUrl("hg+https://hg.example.org/demo")).toBe(true);
expect(isVcsUrl(vcsRequirement("requests"))).toBe(false);
expect(isVcsUrl(vcsRequirement("file:///tmp/pkg"))).toBe(false);
});
it("never lets a typed path read as a PyPI name", () => {
expect(diskRequirement(" mylib ")).toBe("./mylib");
expect(diskRequirement("pkgs/mylib")).toBe("./pkgs/mylib");
expect(diskRequirement("src\\mylib")).toBe("./src\\mylib");
for (const path of ["./mylib", "../lib", "/work/lib", "~/lib", "C:\\work\\lib", "\\\\server\\lib"]) {
expect(diskRequirement(path)).toBe(path);
}
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);
});
});
+15
Переглянути файл
@@ -67,3 +67,18 @@ export function vcsRequirement(url: string) {
const scp = /^([\w.-]+@[\w.-]+):(?!\/\/)(.+)$/.exec(text);
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);
/**
* A path typed for Install from Disk, made explicit so pip never reads it as a PyPI name: a
* relative one starts with `./` (the host resolves it against the project, and `~/` against home).
*/
export function diskRequirement(path: string) {
const text = path.trim();
return /^([./\\~]|[A-Za-z]:[\\/])/.test(text) ? text : `./${text}`;
}
/** A wheel or source archive, which pip can install but not in editable mode. */
export const isArchive = (path: string) => /\.(whl|zip|tar\.gz|tgz|tar\.bz2|tar\.xz)$/i.test(path.trim());