Repository navigation
feat(python): 接入 Pyrefly 解释器选择和 pytest 动态测试树 - #1
Merged
Merged
Conversation
…ytest discovery, and toolchains
Mucheen
marked this pull request as ready for review
November 18, 2026 15:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue / Linked issue
PR 类型 / Type
改动说明 / What changes
背景 / Why
在 Python 插件 M1/M2 纯声明式基线(
v0.1.1)之上,用户在两类核心工作流中仍存在体验缺口:conda环境、uv python安装、pyenv版本或系统环境)时,静态声明启动的pyrefly lsp无法自动感知宿主选定的tools.python.executable,导致外部环境site-packages中的第三方包出现missing-import误报;pytest --collect-only的动态测试树发现(无法在运行前展开查看并单独点击执行测试类方法或@pytest.mark.parametrize参数化子用例),且toolDefinitions的环境根目录尚未覆盖UV_PYTHON_INSTALL_DIR、PYENV_ROOT、CONDA_ENVS_PATH、CONDA_PREFIX、WORKON_HOME、XDG_DATA_HOME等自定义安装位置與项目内.conda环境。M3 新增动态 pytest 发现与解释器注入,按主仓库
release-lithe的插件版本规则从上一预发布版v0.1.1升到v0.2.0。六个归档继续共享纯 Python 后端(python -I -B)。本次验证只构建本地归档。改动 / Changes
io.github.lithe-idea.python-support/scripts/lithe_rpc.py):Content-Length帧编解码,强制协商experimental.litheLoggingversion: 0(缺失时以-32600拒绝initialize),按需协商litheFunctions0、litheBuild1/0与litheToolchains0。sys.dont_write_bytecode = True、PYTHONDONTWRITEBYTECODE=1,并将 Python 层sys.stdout重定向至sys.stderr,确保stdout仅输出合法 JSON-RPC 帧且零自建日志文件。CancelToken与跨平台子进程组终止(POSIXstart_new_session=True+os.killpg,WindowsCREATE_NEW_PROCESS_GROUP+taskkill /T /F),收到$/cancelRequest或shutdown时立即清理子进程树并返回-32800(取消不发终态日志)。lithe/log通知严格在对应 JSON-RPC 响应帧发出之前写入stdout以携带operationId关联,内置suppress_success_diagnostics高频成功抑制与受控错误码脱敏。Pyrefly启动与解释器注入(scripts/backend.py+plugin.json):backend(kind: "runtime",tool: "python",minimumVersion: "3.9",runtimeArguments: ["-I", "-B"],entry: "scripts/backend.py",arguments: ["--state", "${stateDir}"]),languageServers[0]切换为"launch": {"kind": "backend", "function": "pyrefly.resolveLaunch", "timeoutMilliseconds": 30000}(按契约移除静态arguments与validationArguments,保留全部即时设置引用)。pyrefly.resolveLaunch根据platform.os动态返回包内bin/pyrefly(Unix)或bin/pyrefly.exe(Windows)及arguments: ["lsp"];当宿主传入合法tools.python.executable时将其注入workspaceConfiguration.python.pythonPath(实测验证pyrefly 1.4.1通过workspace/configurationsection: "python"读取pythonPath解析项目外环境site-packages);当tools.python缺失(未选定或未信任项目环境)时优雅省略pythonPath回退原生.venv发现;对格式异常的tools.python.executable发送degraded(invalid_tool_hint)并在后续恢复有效提示时发送recovered(tool_hint_restored)。python.inspectEnvironment并启用suppress_success_diagnostics。pytest测试树发现与构建提供者(scripts/build_provider.py+scripts/build_model.py+plugin.json):buildSystems[0](python)接入provider(scripts/build_provider.py --cache ${workspaceStateDir},executesProjectCode: true,mayUseNetwork: false),将model与test切换为"source": "provider"并开启"discovery": true,完整保留pip-install-requirements、mypy任务、7 类派生run配置与debugpy调试器。lithe/build/model复用build_model_with_diagnostics(新增.conda排除与pyproject.toml解析失败检测),损坏时上报degraded(pyproject_parse_failed),修复后上报recovered(pyproject_restored)。lithe/build/discoverTests通过有界可取消pytest --collect-only -q -o addopts= -o cache_dir=<workspaceStateDir>/pytest-collect-cache收集函数、测试类方法与@pytest.mark.parametrize参数化用例(不向用户工作区写.pytest_cache),处理ExitCode.NO_TESTS_COLLECTED (5)返回空列表,缺失pytest返回受控错误python.pytestUnavailable,正常返回litheTestDiscoveryv0(超过 10,000 条截断并记录test_discovery_truncated)。lithe/build/test先校验所有测试选择器严格位于项目根目录内且真实存在(拒绝..、逃逸软链、前导-与外部已删除的失效套件python.invalidTestSelector/python.staleTestSelector,绝不退化为全项目-k扩大运行范围),清理陈旧报告后通过--分隔标志与位置参数执行pytest --junitxml=.pytest_cache/lithe-junit.xml,支持动态测试树精确 nodeid(tests/test_x.py::TestCls::test_fn[a])与 JUnit XML 点分classname双格式定位,实时推送lithe/build/progress。lithe/build/runEntrypoints在协商litheBuild1时提供单文件 AST 入口探测并抑制成功INFO日志。litheToolchains实现(plugin.json+scripts/toolchain_provider.py):plugin.jsontoolDefinitions[0]中补齐UV_PYTHON_INSTALL_DIR、XDG_DATA_HOME、PYENV_ROOT、CONDA_ENVS_PATH、CONDA_PREFIX、WORKON_HOME、LOCALAPPDATA、APPDATA等全部 16 条跨平台自动发现规则,并新增.conda(requires: [".conda/conda-meta/history"],projectKind: "environment")项目内环境候选。scripts/toolchain_provider.py并在backend.py注册litheToolchains(providerID: "python"),独立测试覆盖toolchains/query(解析~/.conda/environments.txt、CONDA_ENVS_PATH、CONDA_PREFIX、UV_PYTHON_INSTALL_DIR、PYENV_ROOT、Poetry 与 Pipenv 外部虚拟环境、.python-version与pyproject.tomlrequires-python,严格排除工作区内目录并通过真实 Coretoolchains.decodeReply校验)。desktop/internal/pluginhost/pluginhost.go的Declared()尚未声明toolchainProviders(Corevalidate_packaged_provider_backend同时规定自举必须使用包内可执行文件或打包运行时)。当前兼容归档走toolDefinitions,并已通过固定桌面宿主的真实安装校验;(2)shared/plugins/contract/v1/plugin-manifest.schema.json中requires.buildProviders枚举目前仍为[0](Core 已支持[0, 1]),buildProviders: 0已完整涵盖构建提供者与litheTestDiscovery;(3) Coreplugin_logging操作白名单未包含toolchains/query,后端按白名单不对其额外发lithe/log以避免plugins.diagnostics.rejected。l10n/zh-Hans.json、scripts/、e2e/、.github/workflows/ci.yml、docs/feature-matrix.md、README.md):pythonExecutable中英说明、docs/feature-matrix.md与README.md结构化日志接线表。scripts/package/archives.test.mjs、scripts/test.mjs、.github/workflows/ci.yml(安装pytest)及e2e/(固定桌面宿主9f4d5fb0597fc54223d85b8f06c9a1032a161bf8,支持本地已有 commit 离线直接检出,新增真实宿主Host.Call(pyrefly.resolveLaunch)、DecodePluginLanguageServerLaunch、buildprovider.Start、PlanBuildTestDiscovery/DecodeBuildTestDiscovery、PlanBuildTest/DecodeBuildTestResults、toolchains.decodeReply、Corelithe/log事实捕获及包目录 SHA-256 零变更断言)。审查修订与兼容性
host通道。backend: 0与buildProviders: 0。Pyrefly 的启动解析新增 Python 3.9 或更新版本前置要求。此前使用已打包 Pyrefly 且未配置可用 Python 的用户需要先安装或选择解释器。宿主按自动启动契约向启动解析器传递可用的外部解释器,项目内环境仍由 Pyrefly 自动发现。当前安装和验证基线为桌面宿主9f4d5fb05与 Core8b91b324。结构化日志 v2 接线总表
operation/ 组件WARN)ERROR)code/ 隐私与限流说明plugins/io.github.lithe-idea.python-support/hostpyrefly.resolveLaunchplugins.operation.finished(INFO)invalid_tool_hint/tool_hint_restoredplugins.operation.failedpython.unknownServer、internal_error;响应前发送lithe/log关联opplugins/io.github.lithe-idea.python-support/hostpython.inspectEnvironmentsuppressSuccessDiagnostics)plugins.operation.failedpython.invalidExecutable、internal_errorplugins/io.github.lithe-idea.python-support/hostlithe/build/modelplugins.operation.finished(INFO)pyproject_parse_failed/pyproject_restoredplugins.operation.failedpython.invalidRoot、python.projectUnavailable、internal_errorplugins/io.github.lithe-idea.python-support/hostlithe/build/discoverTestsplugins.operation.finished(INFO)test_discovery_truncatedplugins.operation.failedpython.pytestUnavailable、python.pytestTimedOut、python.pytestCollectionFailed、python.interpreterUnavailable、python.cacheUnavailable;取消不记失败plugins/io.github.lithe-idea.python-support/hostlithe/build/testplugins.operation.finished(INFO)plugins.operation.failedpython.invalidTestSelector、python.staleTestSelector、python.pytestTimedOut、python.interpreterUnavailable、python.projectUnavailableplugins/io.github.lithe-idea.python-support/hostlithe/build/runEntrypointssuppressSuccessDiagnostics)plugins.operation.failedpython.invalidRoot、python.projectUnavailableplugins/io.github.lithe-idea.python-support/hostlanguageServer/start、debugAdapter/startplugins.operation.finished(INFO,宿主/Core 代记)plugins.operation.failedhost.planFailed、host.executableUnavailable、host.launchFailed、host.exitedEarly、host.initializeFailed、host.timedOutplugins/io.github.lithe-idea.python-support/host、debug/debugpy、sessions/<session>/plugins/.../files/backend.py、build_provider.py、pyrefly lsp、debugpyplugins.host.process.started/plugins.host.protocol.initialized/plugins.host.session.updated(INFO);未分类stderr记plugins.host.stderr.received(DEBUG)ERRORdebugpy日志仅写${logDir};绝不记录源码、路径原文、命令输出或环境变量值不在本 PR 范围内 / Out of scope
plugin.json中启用requires.toolchainProviders: 0:待桌面宿主pluginhost.Declared()开放toolchainProviders并完成后端自举方案对齐后开启(后端toolchains/query实现与 Core 解码测试已在本 PR 就绪)。pyproject.toml原生 GUI 配置编辑器(sourceSettings)与依赖树视图(lithe/build/dependencies):留作后续迭代。类型专属:feat
多平台对齐
Lithe 的目标是 macOS 与 Windows 能力对齐。必须选下面一项:
-I -B后端与构建提供者,pyrefly.resolveLaunch与项目向导均按平台区分 Unix 与 Windows,且双端清单均通过真实 Corepluginhost.Validate校验)platform-specific),理由:范围检查 / Scope checks
功能矩阵 / Feature matrix
docs/feature-matrix.md(插件功能矩阵仅在插件仓库维护,不进入宿主仓库shared/platform-feature-matrix/),如实区分darwin-arm64实机已验证与其他 5 个目标平台归档/契约已校验(pending)及toolchainProviders宿主前置状态。插件归属 / Plugin ownership
l10n/zh-Hans.json)、单测、宿主端到端测试(e2e/)、功能矩阵(docs/feature-matrix.md)与日志/缓存说明(README.md)均只在本插件仓库lithe-plugin-python维护,对宿主与 Core 仓库零写入、零具体插件依赖。UI 验证 / UI evidence
N/A:本 PR 为插件仓库的后端协议、语言服务器启动解析、构建测试提供者与工具链发现实现,不包含宿主原生 UI 控件或布局改动。
统一日志系统 / Structured logging
litheLoggingversion: 0(lithe/log)接入统一日志系统 v2,无自建插件日志文件,无stdout自由文本输出。python.inspectEnvironment、lithe/build/runEntrypoints)已启用suppressSuccessDiagnostics。stderr仅输出受控分类码与异常类名,严禁拼接工作区路径、用户源码、命令输出或环境变量值;debugpy原生日志仅写宿主传入的${logDir}。界面文案 / i18n
io.github.lithe-idea.python-support/plugin.json与l10n/zh-Hans.json中 1:1 维护,并通过 CoreReadPluginSettingsState(zh-Hans)零拒绝校验,未写入宿主shared/i18n/。逻辑归属 / Logic placement
backend、languageServersbackend launch、buildProviders、litheTestDiscovery、junitXml、toolDefinitions、litheToolchains),Python 语言/测试引擎专属行为封装在本插件内,未使用任何私有契约字段。性能与内存 / Performance and memory
backend.py与build_provider.py均由宿主按需惰性拉起,空闲零轮询;模型扫描限制深度<= 4、文件数<= 500、单文件<= 1 MiB;测试发现上限10,000条、子进程输出上限8 MiB、带超时与取消整组终止清理;所有 Python 进程强制-I -B与PYTHONDONTWRITEBYTECODE=1,包目录保持零写入。设置 / Settings
pythonExecutable设置说明以准确反映.venv/venv/.conda/VIRTUAL_ENV/PATH/conda/uv/pyenv自动回退链,未维护影子配置。插件契约 / Plugin contract
N/A:本 PR 严格消费 Rust Core 已合入并固定的现有插件契约 v1,未修改任何 Core 契约 schema、能力版本或宿主方法。
发行包与资源 / Bundle and resources
${pluginDir}写入任何文件或__pycache__;pytest 发现缓存写入${workspaceStateDir},运行报告写入项目的.pytest_cache/lithe-junit.xml,debugpy日志仅写入${logDir};scripts/test.mjs与python_support_plugin_e2e_test.go均在可写插件目录下对全量文件清单与 SHA-256 哈希执行前后 100% 不变性断言。README.md已完整说明.cache/、.artifacts/stage/、.artifacts/lithe-host/与dist/的归属、生命周期及跨工作树复用规则。无用代码与重构 / Dead code and refactoring
scripts/build_model.py的核心静态模型提取被scripts/build_provider.py直接复用并保留独立 CLI 入口测试兼容性。refactorIssue 的既有债务。验证 / Verification
node scripts/package.mjs --expect-tag v0.2.0darwin-arm64(产出全部 6 个目标平台归档)dist/python-support-0.2.0-{darwin-arm64,darwin-x64,windows-x64,windows-arm64,linux-x64,linux-arm64}.zip)node --test --test-timeout=60000 scripts/package/*.test.mjsdarwin-arm64(校验全部 6 个平台归档)PYTHON=<pytest 环境的解释器> node scripts/test.mjsdarwin-arm64(Python 3.14.6、pytest 9.2.0)build_model.py、backend.py握手与pyrefly.resolveLaunch外部解释器注入 + 真实pyrefly lsp验证、toolchain_provider.py、build_provider.py降级/恢复 +pytest --collect-only发现 + JUnit XML 单测执行 +runEntrypoints+ 取消杀进程树、debugpyDAP 及已安装插件目录 SHA-256 100% 不变)node scripts/e2e-host.mjsdarwin-arm64(固定宿主9f4d5fb0597fc54223d85b8f06c9a1032a161bf8+ 真实 Rust Core8b91b324)./internal/projectgenTestPythonPluginGeneratorCreatesProjectAndVirtualenv与./internal/workbenchTestPythonSupportPluginContractPlansInstallAndImmutability通过,最终版用例分别用时 3.21 秒与 7.35 秒,使用真实 pytest 9.2.0)git diff --check origin/main...HEADdarwin-arm64f5cc2b7darwin-arm64a2ae524)通过 5/5,包括六平台归档校验及 Linux x64、macOS arm64、Windows x64、Windows ARM64 的真实插件流程。Windows 测试保留主目录隔离、原生路径身份、Job Object 终止和正常退出回归,适配连接重置及 CRLF 握手。最终 CI未能执行的检查和原因:
提交前确认 / Final checks
main(8728b7ac29f82f6124074eee38678009f3d9b7e3)创建(feat/2122-m3-backend-pytest);目标分支:main.build/、target/、dist/、.artifacts/、.cache/等产物,没有凭据、个人路径和未脱敏的日志或截图