Skip to content

Commit af7ca87

Browse files
committed
feat(toolchain): 设置页原处选解释器、可用项目外的虚拟环境,发布 1.2.0 预发布版
- 两个设置页顶部的解释器行写 choose: true,变成在原处选择工具链安装的下拉菜单(宿主 #2312) - python 工具定义写 executableLinksOutsideHome: true(Core #177、宿主 #2321), 项目外 venv / uv venv / virtualenv、Poetry 与 Pipenv 的项目环境、Homebrew 安装可以选中, 按环境里的路径启动 - 新发现 Hatch 虚拟环境和 virtualenvwrapper 环境 - .python-version 写版本号或 requires-python 时只选基础解释器;写环境名时仍按名选环境 - 去掉没有用到的 run.start 权限 - 宿主 e2e 固定到 #2321 的提交,新增固定项目外 venv 的当前文件运行用例; 旧宿主不能安装 1.2.0,CI 去掉 0.8.9 兼容子集 - README、功能矩阵、v1.2.0 发布说明;v1.2.0 列入预发布标签
1 parent 0c91477 commit af7ca87

16 files changed

Lines changed: 388 additions & 91 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ jobs:
115115
path: dist
116116
- name: Install pytest for the host suite
117117
run: python -m pip install --disable-pip-version-check pytest
118-
- name: Verify Python highlighting, run configurations, and dependencies on Lithe 0.8.9
119-
run: node scripts/e2e-host.mjs --ref 4d1b88f03f243356c855299464baa2647cc57226 --run '^TestPythonSupportPlugin(GrammarHighlighting|RunCurrentFile(InterpreterOverride|PinnedInterpreter)?|ManualRunAndDependencies)$'
120118
- name: Run the full Python host suite on the pinned desktop
121119
env:
122120
GOFLAGS: -tags=vivahead

‎.github/workflows/release.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
- name: Publish GitHub Release
8383
env:
8484
GH_TOKEN: ${{ github.token }}
85-
PRERELEASE_TAGS: v0.1.0 v0.1.1
85+
PRERELEASE_TAGS: v0.1.0 v0.1.1 v1.2.0
8686
run: |
8787
release_flags=()
8888
installation='Lithe Store 会在收录后提供此版本。打开 设置 → 插件 → 插件市场,搜索 Python Support。'

‎README.md‎

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@ Pyrefly 和 debugpy 随插件包分发,Python 解释器、pytest、uv、conda
88
> English: Python support for Lithe — Pyrefly code intelligence and type checking, run entries, a pytest test tree and
99
> the installed-package tree in the build window, uv/conda/Ruff/mypy tasks, debugpy debugging, a Python project entry in
1010
> New Project, and previews on the settings pages. Pyrefly and debugpy ship in the plugin; the interpreter, pytest, uv,
11-
> conda and Ruff stay your own. Pick the interpreter on Settings › Build, Execution, Deployment › Toolchains.
11+
> conda and Ruff stay your own. Pick the interpreter from the drop-down at the top of Settings › Languages & Frameworks › Python.
1212
13-
> **兼容性**:需要 Lithe 桌面端 0.8.9 或更新版本;0.8.10 起还能按 shebang 识别没有后缀的 Python 脚本。
13+
> **兼容性**:1.2.0 需要 0.8.14 之后发布的 Lithe 桌面端(包含 [#2312](https://github.com/1lck/Lithe-IDEA/pull/2312) 和
14+
> [#2316](https://github.com/1lck/Lithe-IDEA/issues/2316) 的版本),旧版本不能安装,请继续用 1.1.0(支持 0.8.9 或更新版本)。
1415
> 各版本的变化见 [docs/releases](docs/releases)。
1516
1617
## 功能
1718

1819
| 功能 | 说明 |
1920
| --- | --- |
2021
| 代码智能 | 补全、自动导入、跳转定义、悬停、类型诊断,由随包的 [Pyrefly](https://github.com/facebook/pyrefly) 1.4.1 提供 |
21-
| 识别文件 | `.py`、`.pyi`、`.pyw`;0.8.10 起首行是 Python shebang 的无后缀脚本也按 Python 打开 |
22+
| 识别文件 | `.py`、`.pyi`、`.pyw`;首行是 Python shebang 的无后缀脚本也按 Python 打开 |
2223
| 运行 | 工具栏「当前文件」运行带 `if __name__ == "__main__"` 的脚本和包里的 `__main__.py`;构建窗口列出 `[project.scripts]`、Django `manage.py`、FastAPI、Flask 等入口;「运行/调试配置 → + → Python」可以手动配置脚本或模块 |
2324
| 测试 | `pytest --collect-only` 收集函数、类方法、参数化用例和 doctest,组成测试树;可以运行整个套件或单个用例 |
2425
| 依赖 | 构建窗口的「依赖」列出所选解释器里已安装的包、版本和依赖关系,不需要 pip |
2526
| 任务 | `pip install -r requirements.txt`、`mypy`;有 `uv.lock` 时 `uv sync`、`uv lock`;有 `environment.yml` 时 `conda env update`;有 Ruff 配置时 `ruff check`、`ruff format` |
2627
| 调试 | 随包的 debugpy 1.8.22,支持启动和按地址附加(`host`、`port`、`pathMappings`) |
2728
| 输出链接 | 终端和运行输出里 Traceback 的 `File "…", line N` 可以点击跳到源码 |
2829
| 新建项目 | 选解释器、选是否用 venv 或 uv 建 `.venv`、是否加示例代码;创建后项目固定使用所选解释器 |
29-
| 工具链 | 找到项目的 `.venv` 和电脑上 uv、pyenv、conda、python.org 装的 Python,按 `.python-version` 和 `requires-python` 选版本,见下文“Python 解释器” |
30+
| 工具链 | 找到项目的 `.venv`,Poetry、Pipenv、Hatch、virtualenvwrapper 建在项目外的环境,以及电脑上 uv、pyenv、conda、Homebrew、python.org 装的 Python;按 `.python-version` 和 `requires-python` 选版本,见下文“Python 解释器” |
3031
| 设置预览 | 语言页和构建工具页画出设置的效果和解释器的查找顺序,见下文“设置预览” |
3132

3233
## 设置
@@ -48,30 +49,22 @@ Pyrefly 和 debugpy 随插件包分发,Python 解释器、pytest、uv、conda
4849
Pyrefly 分析代码、运行、测试、调试、读取依赖、安装依赖都用同一个解释器,除非设置了“Python 解释器覆盖路径”:那时构建任务、运行和
4950
pytest 用覆盖路径,Pyrefly 和调试仍用下面找到的解释器。Lithe 按这个顺序找:
5051

51-
1. **项目环境**:为项目固定的解释器;项目里的 `.venv`、`venv`、`.conda`(项目要先被信任);项目的 `.python-version` 和
52-
`pyproject.toml` 的 `requires-python` 决定选哪个版本。
52+
1. **项目环境**:为项目固定的解释器;项目里的 `.venv`、`venv`、`.conda`(项目要先被信任);有 `poetry.lock` 或 `Pipfile` 时,
53+
Poetry、Pipenv 为这个项目建的环境;项目的 `.python-version` 和 `pyproject.toml` 的 `requires-python` 决定选哪个版本。
5354
2. **全局默认**:全局固定的解释器,其次默认版本。
54-
3. **自动发现**:`VIRTUAL_ENV`,`PATH` 里的 `python3`、`python`,然后是 uv、pyenv、conda、python.org 装的 Python。
55+
3. **自动发现**:`VIRTUAL_ENV`,`PATH` 里的 `python3`、`python`,然后是 uv、pyenv、conda、Homebrew、python.org 装的 Python。
5556

56-
换解释器在 **设置 → 构建、执行、部署 → 工具链**:为项目或全局固定一个找到的安装,或者添加其他目录。
57-
「语言与框架 › Python」顶部那一行显示当前用的解释器,点它也会到工具链页。
57+
`.python-version` 写版本号(如 `3.12`)和 `requires-python` 只选基础解释器,不会选中别的项目的虚拟环境;
58+
`.python-version` 写的是环境名(pyenv-virtualenv 的做法)时,选同名的环境。
5859

59-
**目前用不了的解释器**([#2316](https://github.com/1lck/Lithe-IDEA/issues/2316)):在 macOS 和 Linux 上,Lithe 拒绝程序链接到安装目录外的
60-
Python,固定也不行。受影响的有:
61-
62-
- 建在项目外的虚拟环境:`python -m venv`、`uv venv`、virtualenv 建的,以及 Poetry、Pipenv、virtualenvwrapper 默认建的环境;
63-
- Homebrew 的 Python,它不会出现在安装列表里。
64-
65-
在此之前请把环境建在项目里:`python3 -m venv .venv` 或 `uv venv`;Poetry 先运行 `poetry config virtualenvs.in-project true`,
66-
Pipenv 设置 `PIPENV_VENV_IN_PROJECT=1`。也可以固定 uv、pyenv、python.org 装的基础解释器,但那里没有环境里装的包。
67-
68-
项目外的环境暂时也可以填进“Python 解释器覆盖路径”,让运行和 pytest 先用上它。
60+
换解释器:点「语言与框架 › Python」顶部那一行,在下拉菜单里为项目或全局选一个找到的解释器,或者添加其他目录,
61+
例如建在别处的 `python -m venv`、`uv venv` 环境。菜单底部也能打开 **设置 → 构建、执行、部署 → 工具链**。
6962

7063
## 设置预览
7164

7265
两块预览由插件后端按界面语言、外观和设置窗口宽度画出。它们用示例代码,不读取打开的项目;截图里的路径和版本是测试数据。
7366

74-
**语言与框架 › Python**:顶部是当前用的 Python 解释器;设置了覆盖路径时,下面多一行说明哪些流程改用它。下面四张卡片各对应一项设置:
67+
**语言与框架 › Python**:顶部是当前用的 Python 解释器,点它打开下拉菜单换解释器;设置了覆盖路径时,下面多一行说明哪些流程改用它。下面四张卡片各对应一项设置:
7568

7669
| 卡片 | 画出什么 |
7770
| --- | --- |
@@ -86,7 +79,7 @@ Pipenv 设置 `PIPENV_VENV_IN_PROJECT=1`。也可以固定 uv、pyenv、python.o
8679

8780
<img src="docs/assets/settings-previews/language-en-draft-light.png" alt="还没应用的修改先显示在预览里,英文浅色外观" width="720">
8881

89-
**构建工具 › Python**:先画覆盖路径(有没有设置、设置后谁用它),再画上面三步查找顺序,以及查找到的解释器用在哪些流程。这一页没有设置项,换解释器要到工具链页。
82+
**构建工具 › Python**:顶部同样是解释器下拉菜单;下面先画覆盖路径(有没有设置、设置后谁用它),再画上面三步查找顺序,以及查找到的解释器用在哪些流程。这一页没有其他设置项。
9083

9184
<img src="docs/assets/settings-previews/build-tools-zh-Hans-dark.png" alt="构建工具 › Python 画出 Lithe 查找 Python 解释器的顺序,深色外观" width="720">
9285

@@ -113,7 +106,9 @@ Pipenv 设置 `PIPENV_VENV_IN_PROJECT=1`。也可以固定 uv、pyenv、python.o
113106

114107
- 后端用 [Rust 插件 SDK](https://github.com/Lithe-IDEA/Lithe-core-rust/tree/main/shared/plugins/sdk/rust) 编写,回答
115108
`pyrefly.resolveLaunch`(启动 Pyrefly 并传入所选解释器)、`settings.view`(两块设置预览)和 `toolchains/query`(读取 conda 登记、uv、pyenv、
116-
Poetry、Pipenv 环境和项目的版本要求)。后端不保存状态,也不写文件。
109+
Poetry、Pipenv、Hatch、virtualenvwrapper 环境和项目的版本要求)。后端不保存状态,也不写文件。
110+
- 虚拟环境的 `bin/python` 通常链接到基础安装。`plugin.json` 的 python 工具定义写了 `executableLinksOutsideHome`,
111+
Lithe 按环境里的路径启动它,Python 因此能找到自己所在的环境和装在里面的包。
117112
- 构建提供者 `scripts/build_provider.py` 由所选解释器以 `python -I -B` 启动,回答 `lithe/build/model`、`test`、`runEntrypoints` 和 `prepareRun`。
118113
它只用 `ast` 和 `tomllib` 读项目,不执行项目代码。
119114
- 测试树由 `test.discoveryCommand` 运行 `scripts/discover_tests.py` 得到;报告和 pytest 缓存在 Lithe 分配的目录里,用完由 Lithe 清理。
@@ -158,16 +153,16 @@ PYTEST_PYTHON=/path/to/python node scripts/test.mjs # 用真实的 Pyre
158153
需要 Node.js 22+、Go、Rust 工具链、`git` 和两个 Python:
159154

160155
- `PATH` 里的 `python3` 要是 3.10 或更新(调试用的 debugpy 需要),并且能用 `--copies` 建虚拟环境。Homebrew(`/opt/homebrew/opt/python@3.x/libexec/bin`)或 python.org 装的都可以;uv 装的独立版本复制出来不能运行,Xcode 自带的是 3.9;
161-
- `PYTEST_PYTHON` 指向装了 pytest 的真实安装,不能是虚拟环境(固定解释器的测试会固定它,见 #2316)。例如
162-
`uv python install 3.12 --install-dir .artifacts/test-python`,再用 `uv pip install --python <它的 python3.12> --break-system-packages pytest`。
156+
- `PYTEST_PYTHON` 指向装了 pytest 的真实安装,不能是虚拟环境:`RunCurrentFilePinnedInterpreter` 固定它,
157+
`RunCurrentFilePinnedEnvironment` 另用 `python3` 在项目外建虚拟环境来固定。例如 `uv python install 3.12 --install-dir .artifacts/test-python`,
158+
再用 `uv pip install --python <它的 python3.12> --break-system-packages pytest`。
163159

164160
```bash
165161
node scripts/package.mjs --platform darwin-arm64
166-
export LITHE_TEST_PYTHON_PLUGIN_ARCHIVE="$PWD/dist/python-support-1.1.0-darwin-arm64.zip"
162+
export LITHE_TEST_PYTHON_PLUGIN_ARCHIVE="$PWD/dist/python-support-1.2.0-darwin-arm64.zip"
167163
PYTEST_PYTHON=/path/to/python3.12 node scripts/e2e-host.mjs
168-
# 用同一个包检查 0.8.9:高亮、当前文件运行、固定解释器、手动运行
169-
PYTEST_PYTHON=/path/to/python3.12 node scripts/e2e-host.mjs --ref desktop-v0.8.9 \
170-
--run '^TestPythonSupportPlugin(GrammarHighlighting|RunCurrentFile(InterpreterOverride|PinnedInterpreter)?|ManualRunAndDependencies)$'
164+
# 只跑固定解释器的两个用例
165+
PYTEST_PYTHON=/path/to/python3.12 node scripts/e2e-host.mjs --run '^TestPythonSupportPluginRunCurrentFilePinned'
171166
```
172167

173168
插件包路径要写绝对路径。脚本把宿主检出到 `.artifacts/lithe-host`,复制 `e2e/host/desktop/` 下的测试后运行;`--source`、`--ref`、`--run`
@@ -181,9 +176,9 @@ PYTEST_PYTHON=/path/to/python3.12 node scripts/e2e-host.mjs --ref desktop-v0.8.9
181176

182177
1. 修改 `io.github.lithe-idea.python-support/plugin.json` 的 `version`:修复升修订号,加功能升次版本号,特别大的革新才升主版本号。
183178
依赖还没发布的宿主能力时照样升次版本号,作为预发布版本发布。写好 `docs/releases/v<版本>.md`,合入 `main`。
184-
2. 推送同名标签,例如 `git tag v1.1.0 && git push origin v1.1.0`。
179+
2. 推送同名标签,例如 `git tag v1.2.0 && git push origin v1.2.0`。
185180
3. `Release` 工作流核对标签与版本,打六个平台的包并发布 Release,说明里附 SHA-256 和 `docs/releases/v<版本>.md`。
186-
`release.yml` 的 `PRERELEASE_TAGS` 里的版本标为预发布,目前是 `v0.1.0` 和 `v0.1.1`。
181+
`release.yml` 的 `PRERELEASE_TAGS` 里的版本标为预发布,目前是 `v0.1.0`、`v0.1.1` 和 `v1.2.0`。
187182
4. Lithe Store 的自动追踪会为稳定版本开合并请求,合并后用户即可更新。
188183

189184
## 许可

‎backend/src/discovery.rs‎

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ struct Installation {
2828
home: String,
2929
names: BTreeSet<OsString>,
3030
version: Version,
31+
/// A virtual environment belongs to one project. A version request picks
32+
/// it only by its name, never by its version alone.
33+
virtual_environment: bool,
3134
}
3235

3336
struct Snapshot<'a> {
@@ -93,6 +96,17 @@ pub(crate) fn query(
9396
if let Some(pipenv) = pipenv_root(user_home.as_deref()) {
9497
snapshot.scan(&pipenv, Source::Pipenv)?;
9598
}
99+
// virtualenvwrapper keeps environments in ~/.virtualenvs unless WORKON_HOME
100+
// moves them, and so does Hatch when configured to use it. On Windows this
101+
// is already the Pipenv default above.
102+
if !cfg!(windows) && env::var_os("WORKON_HOME").is_none() {
103+
if let Some(home) = &user_home {
104+
snapshot.scan(&home.join(".virtualenvs"), Source::Virtualenvwrapper)?;
105+
}
106+
}
107+
if let Some(hatch) = hatch_root(user_home.as_deref()) {
108+
snapshot.scan_hatch(&hatch)?;
109+
}
96110
if let Some((root, directory)) = workspace {
97111
project::read(&mut snapshot, &root, &directory)?;
98112
}
@@ -150,24 +164,49 @@ impl Snapshot<'_> {
150164

151165
fn scan(&mut self, root: &Path, source: Source) -> Result<()> {
152166
self.watch(root)?;
153-
let Some(root) = canonical_directory(root)? else {
154-
return Ok(());
167+
for entry in self.entries(root)? {
168+
self.observe(&entry, source)?;
169+
}
170+
Ok(())
171+
}
172+
173+
/// Hatch keeps each environment under `<project name>/<project ID>/`. The
174+
/// root and each project ID directory are watched, as an environment is
175+
/// added to the latter; a new project name also changes the root.
176+
fn scan_hatch(&mut self, root: &Path) -> Result<()> {
177+
self.watch(root)?;
178+
for project in self.entries(root)? {
179+
for id in self.entries(&project)? {
180+
if canonical_directory(&id)?.is_some() {
181+
self.scan(&id, Source::Hatch)?;
182+
}
183+
}
184+
}
185+
Ok(())
186+
}
187+
188+
/// Lists the entries of a directory outside the workspace, skipping hidden
189+
/// ones such as Hatch's `.pythons`. A missing directory has no entries.
190+
fn entries(&mut self, directory: &Path) -> Result<Vec<PathBuf>> {
191+
let Some(directory) = canonical_directory(directory)? else {
192+
return Ok(Vec::new());
155193
};
156194
if self
157195
.workspace
158196
.as_ref()
159-
.is_some_and(|workspace| root.starts_with(workspace))
197+
.is_some_and(|workspace| directory.starts_with(workspace))
160198
{
161-
return Ok(());
199+
return Ok(Vec::new());
162200
}
163-
for entry in std::fs::read_dir(root).map_err(metadata::io_failure)? {
201+
let mut entries = Vec::new();
202+
for entry in std::fs::read_dir(directory).map_err(metadata::io_failure)? {
164203
self.step()?;
165204
let entry = entry.map_err(metadata::io_failure)?;
166205
if !entry.file_name().as_encoded_bytes().starts_with(b".") {
167-
self.observe(&entry.path(), source)?;
206+
entries.push(entry.path());
168207
}
169208
}
170-
Ok(())
209+
Ok(entries)
171210
}
172211

173212
fn observe(&mut self, candidate: &Path, source: Source) -> Result<()> {
@@ -190,11 +229,14 @@ impl Snapshot<'_> {
190229
existing.names.extend(names);
191230
return Ok(());
192231
}
193-
if !metadata::has_safe_executable(&home)? {
232+
if !metadata::has_safe_executable(&home, self.workspace.as_deref())? {
194233
return Ok(());
195234
}
196-
let Some(Metadata { version, input }) =
197-
metadata::version(&home, candidate, source, || self.step())?
235+
let Some(Metadata {
236+
version,
237+
input,
238+
virtual_environment,
239+
}) = metadata::version(&home, candidate, source, || self.step())?
198240
else {
199241
return Ok(());
200242
};
@@ -210,6 +252,7 @@ impl Snapshot<'_> {
210252
home: wire_home,
211253
names,
212254
version,
255+
virtual_environment,
213256
},
214257
);
215258
if self.installations.len() > MAX_INSTALLATIONS {
@@ -277,6 +320,21 @@ fn poetry_root(home: Option<&Path>) -> Option<PathBuf> {
277320
})
278321
}
279322

323+
fn hatch_root(home: Option<&Path>) -> Option<PathBuf> {
324+
let data = absolute_env("HATCH_DATA_DIR").or_else(|| {
325+
if cfg!(target_os = "macos") {
326+
home.map(|path| path.join("Library/Application Support/hatch"))
327+
} else if cfg!(windows) {
328+
absolute_env("LOCALAPPDATA").map(|path| path.join("hatch"))
329+
} else {
330+
absolute_env("XDG_DATA_HOME")
331+
.or_else(|| home.map(|path| path.join(".local/share")))
332+
.map(|path| path.join("hatch"))
333+
}
334+
});
335+
data.map(|path| path.join("env/virtual"))
336+
}
337+
280338
fn pipenv_root(home: Option<&Path>) -> Option<PathBuf> {
281339
absolute_env("WORKON_HOME").or_else(|| {
282340
home.map(|path| {

0 commit comments

Comments
 (0)