Skip to content

Commit a18cfab

Browse files
committed
fix(python): isolate dependency inspection from user site
1 parent 8041143 commit a18cfab

5 files changed

Lines changed: 38 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Python Support v1.0.0 requires Lithe 0.8.9 or later. Lithe 0.8.9 can install the
3535
- `scripts/run_entrypoint.py` 随插件 ZIP 安装,运行时只读。它在宿主管理的进程树内转发解释器覆盖,并检查包入口的实际来源。包名被其他模块遮蔽时给出错误。旧宿主未传界面语言时,入口错误同时提供中英文。
3636
- 仅用 `environment.yml` 或 `environment.yaml` 管理 Conda 环境的项目也会识别为 Python 构建实例。
3737
- 在「运行/调试配置 → + → Python」手动配置脚本路径或模块名。可设置解释器选项、脚本参数、工作目录和环境变量。运行时沿用项目的 Python 工具链及解释器覆盖设置,支持选用 Conda、uv 和普通虚拟环境。
38-
- 构建面板的「依赖」显示所选解释器中已安装的 Python 包版本与依赖关系。展开或重新加载时读取发行包元数据,支持无 pip 的环境。包共享的子树只展开一次,循环依赖以叶节点结束。
38+
- 构建面板的「依赖」显示所选解释器中已安装的 Python 包版本与依赖关系。展开或重新加载时用 `python -I -B` 读取发行包元数据,跳过用户级 site-packages 及其启动钩子,支持无 pip 的环境。包共享的子树只展开一次,循环依赖以叶节点结束。
3939
- 同时保留声明式任务 `pip install -r requirements.txt` 与 `mypy`(带诊断 `lineMatcher`)。
4040
- `uv`(`uv.lock`):`uv sync`、`uv lock`。
4141
- `conda`(`environment.yml` / `environment.yaml`):`conda env update --file environment.yml --prune`。

‎docs/feature-matrix.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
| 构建模型与运行 (`buildSystems: python` + `provider`) | `scripts/build_provider.py`(`python -I -B`,`lithe/build/model`)复用 `scripts/build_model.py` 纯 AST + `tomllib` / `vendor/tomli` 静态提取 `[project.scripts]`、包 `__main__.py`、顶层 `__main__` 脚本、Django `manage.py`、FastAPI ASGI、Flask WSGI 并派生 7 类声明式 `run` 配置;支持损坏 `pyproject.toml` 降级与修复后恢复诊断,并在协商 `litheBuild` 1 时支持 `lithe/build/runEntrypoints` | ✅ 已验证 | ⏳ pending(归档已校验) | ⏳ pending(归档已校验) | ⏳ pending(归档已校验) | `2026-11-18`(`darwin-arm64`) | `io.github.lithe-idea.python-support/scripts/build_model.py`<br>`io.github.lithe-idea.python-support/scripts/build_provider.py`<br>`io.github.lithe-idea.python-support/plugin.json`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go` |
2020
| 当前文件运行 (`buildProviders: 1`) | 工具栏支持带 `__main__` 入口的脚本或包内 `__main__.py`。使用项目 Python 或经宿主解析的显式解释器。包入口执行前核对文件来源。 | ✅ 已验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | 2026-11-20(macOS ARM64,Lithe 0.8.9、0.8.11 工作台端到端) | `io.github.lithe-idea.python-support/scripts/build_provider.py`<br>`io.github.lithe-idea.python-support/scripts/run_entrypoint.py`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_current_file_run_e2e_test.go` |
2121
| 手动 Python 运行配置 (`runConfigurationTypes: 0`) | 「运行/调试配置 → + → Python」支持脚本路径和模块名、解释器选项、参数、环境变量和工作目录。使用项目选定的解释器或插件解释器覆盖设置。 | ✅ 已验证(macOS ARM64) | ⏳ 待实机验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | 2026-11-21(固定宿主,脚本与模块) | `io.github.lithe-idea.python-support/plugin.json`<br>`e2e/host/desktop/internal/workbench/python_project_workflow_e2e_test.go` |
22-
| 构建面板依赖树 (`litheDependencies`) | 展开「依赖」时读取所选 Python 环境中已安装的包、版本和依赖关系,解析 PEP 508 条件和可选依赖。仅有 Conda 环境清单的项目也可加载。支持无 pip 的环境。 | ✅ 已验证(macOS ARM64) | ⏳ 待实机验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | 2026-11-21(固定宿主与打包命令) | `io.github.lithe-idea.python-support/scripts/dependencies.py`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_project_workflow_e2e_test.go` |
22+
| 构建面板依赖树 (`litheDependencies`) | 展开「依赖」时用 `python -I -B` 读取所选 Python 环境中已安装的包、版本和依赖关系,跳过用户级 site-packages 及其启动钩子,解析 PEP 508 条件和可选依赖。仅有 Conda 环境清单的项目也可加载。支持无 pip 的环境。 | ✅ 已验证(macOS ARM64) | ⏳ 待实机验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | 2026-11-21(固定宿主与打包命令) | `io.github.lithe-idea.python-support/scripts/dependencies.py`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_project_workflow_e2e_test.go` |
2323
| 动态 `pytest` 测试树与测试执行 (`litheTestDiscovery` + `junitXml`) | `test.discoveryCommand` 通过 `python -I -B` 启动包内 `discover_tests.py`。`pytest --collect-only` 收集函数、类方法、参数化用例与 doctest,并将 `litheTestDiscovery` v0 写入宿主提供的 `${outputFile}`。无测试时返回空列表,收集失败或选择器超出契约时整次失败。报告与缓存由宿主清理。`lithe/build/test` 以精确 node ID 或 JUnit 类名重跑,继续提供流式进度和 JUnit 报告。 | ✅ 已验证(桌面主线) | ⏳ 待实机验证 | ⏳ 待 CI 与宿主验收 | ⏳ 待 CI 与宿主验收 | 2026-11-20(macOS ARM64,桌面主线) | `io.github.lithe-idea.python-support/scripts/discover_tests.py`<br>`io.github.lithe-idea.python-support/scripts/build_provider.py`<br>`io.github.lithe-idea.python-support/plugin.json`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go` |
2424
| 独立工具构建系统 (`uv` / `conda` / `ruff`) | `uv`(`sync` / `lock`)、`conda`(`env update --file environment.yml --prune`)、`ruff`(`check` 含 `lineMatcher` / `format`)按各自标记文件独立检测与规划 | ✅ 已验证 | ⏳ pending(归档已校验) | ⏳ pending(归档已校验) | ⏳ pending(归档已校验) | `2026-11-18`(`darwin-arm64`) | `io.github.lithe-idea.python-support/plugin.json`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go` |
2525
| 调试器 (`debugAdapters: debugpy`) | 内置打包 `debugpy` `1.8.22`(`stdio` DAP,`["-I", "-B"]` + `scripts/debugpy_adapter.py`、`--log-dir ${logDir}`、适配器与 `launch` 双重设置 `PYTHONDONTWRITEBYTECODE=1` 保持插件目录零写入、支持远程 `attach` + `pathMappings`) | ✅ 已验证 | ⏳ pending(归档已校验) | ⏳ pending(归档已校验) | ⏳ pending(归档已校验) | 2026-11-20(macOS ARM64,桌面主线) | `io.github.lithe-idea.python-support/plugin.json`<br>`io.github.lithe-idea.python-support/scripts/debugpy_adapter.py`<br>`vendor-lock.json`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go`<br>`e2e/host/desktop/internal/workbench/python_editor_debug_e2e_test.go` |

‎io.github.lithe-idea.python-support/plugin.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@
594594
"dependencies": {
595595
"source": "command",
596596
"scopes": ["project"],
597-
"arguments": ["-E", "-B", "${pluginDir}/scripts/dependencies.py", "${outputFile}"],
597+
"arguments": ["-I", "-B", "${pluginDir}/scripts/dependencies.py", "${outputFile}"],
598598
"output": "outputFile",
599599
"decoder": "litheDependencies",
600600
"executesProjectCode": true,

‎io.github.lithe-idea.python-support/scripts/dependencies.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from importlib import metadata
88
from pathlib import Path
99

10-
# Run with -E -B to ignore inherited PYTHONPATH and still inspect the selected
11-
# interpreter's normal site directories. Resolve PEP 508 with the bundled parser,
12-
# even in a pip-free environment. Remove its path before discovering packages.
10+
# The manifest uses -I -B to skip user-site startup hooks and bytecode writes.
11+
# Use the bundled PEP 508 parser even in pip-free environments, and remove its
12+
# path before discovering distributions so bundled packages stay out of the tree.
1313
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "vendor"))
1414
try:
1515
from packaging.requirements import InvalidRequirement, Requirement

‎scripts/test.mjs‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
writeFileSync,
2727
} from "node:fs";
2828
import { tmpdir } from "node:os";
29-
import { basename, dirname, join, normalize, resolve } from "node:path";
29+
import { basename, dirname, join, normalize, resolve, sep } from "node:path";
3030
import { fileURLToPath } from "node:url";
3131
import { readZipEntries } from "./lib/archive.mjs";
3232
import { backendPath } from "./lib/backend.mjs";
@@ -308,6 +308,34 @@ try {
308308
}
309309

310310
const pluginManifest = JSON.parse(readFileSync(join(unpackedPluginDir, "plugin.json"), "utf8"));
311+
const dependencyCommand = pluginManifest.buildSystems.find(({ id }) => id === "python").dependencies;
312+
const dependencyArguments = (outputFile) => dependencyCommand.arguments.map((arg) =>
313+
arg.replace("${pluginDir}", unpackedPluginDir).replace("${outputFile}", outputFile));
314+
315+
await check("dependency command skips user-site startup hooks", () => {
316+
const environment = join(scratch, "dependency environment with shared site");
317+
run(pythonCmd, ["-I", "-B", "-m", "venv", "--without-pip", "--system-site-packages", environment]);
318+
const python = process.platform === "win32"
319+
? join(environment, "Scripts", "python.exe")
320+
: join(environment, "bin", "python");
321+
const userBase = join(scratch, "dependency user base");
322+
const env = { ...process.env, PYTHONUSERBASE: userBase };
323+
const userSite = run(python, ["-E", "-B", "-c", "import site; assert site.ENABLE_USER_SITE; print(site.getusersitepackages())"], {
324+
cwd: scratch, env,
325+
}).trim();
326+
assert.ok(userSite.startsWith(userBase + sep), "expected a user site under the test's PYTHONUSERBASE");
327+
mkdirSync(userSite, { recursive: true });
328+
writeFileSync(join(userSite, "usercustomize.py"), [
329+
"from pathlib import Path",
330+
'Path(__file__).with_name("user-site-hook-ran").touch()',
331+
"",
332+
].join("\n"));
333+
334+
const treeFile = join(scratch, "isolated-dependencies.json");
335+
run(python, dependencyArguments(treeFile), { cwd: scratch, env });
336+
assert.equal(existsSync(join(userSite, "user-site-hook-ran")), false, "usercustomize.py was executed");
337+
assert.equal(JSON.parse(readFileSync(treeFile, "utf8")).format, "litheDependencies");
338+
});
311339

312340
await check("dependency command reads installed packages from a pip-free Python environment", () => {
313341
const environment = join(scratch, "dependency environment");
@@ -335,12 +363,9 @@ try {
335363
metadata("CycleB", "5.0", ["CycleA"]);
336364

337365
const treeFile = join(scratch, "dependencies.json");
338-
const declaration = pluginManifest.buildSystems.find(({ id }) => id === "python").dependencies;
339-
assert.equal(declaration.output, "outputFile");
340-
assert.equal(declaration.decoder, "litheDependencies");
341-
const args = declaration.arguments.map((arg) =>
342-
arg.replace("${pluginDir}", unpackedPluginDir).replace("${outputFile}", treeFile));
343-
run(python, args, { cwd: scratch });
366+
assert.equal(dependencyCommand.output, "outputFile");
367+
assert.equal(dependencyCommand.decoder, "litheDependencies");
368+
run(python, dependencyArguments(treeFile), { cwd: scratch });
344369
const leaf = (name, version, children = []) => ({ name, version, children });
345370
assert.deepEqual(JSON.parse(readFileSync(treeFile, "utf8")), {
346371
format: "litheDependencies", version: 0, dependencies: [

0 commit comments

Comments
 (0)