Skip to content

Improve PEP 723 inline-script env setup (failure messages + uv shim fix) - #1764

Merged
Stella Huang (StellaHuang95) merged 2 commits into
microsoft:mainfrom
StellaHuang95:fixInlineScriptIssues
Sep 4, 2026
Merged

Improve PEP 723 inline-script env setup (failure messages + uv shim fix)#1764
Stella Huang (StellaHuang95) merged 2 commits into
microsoft:mainfrom
StellaHuang95:fixInlineScriptIssues

Conversation

@StellaHuang95

Copy link
Copy Markdown
Contributor

Summary

Two related fixes for the (internally-flagged) PEP 723 inline-script environment feature, found while manually testing the "Set up environment for this script" CodeLens.

1. Surface inline-script setup failures instead of failing silently

Previously, clicking the CodeLens and having setup produce no environment was silent — the reason lived only in the Python Environments output log. The interactive setup command now surfaces an actionable message:

  • a warning for the no-compatible-python case, with a note when requires-python is an exact two-segment pin like ==3.11 (PEP 440 treats it as exactly 3.11.0, which is often not installable);
  • a generic "see the output" error for other failures;
  • silence when the user declined the Python install, or when an environment was built but intentionally not associated (the script's metadata changed mid-setup).

The env manager records the setup outcome (failed reason or benign skip) on the shared InlineScriptRoutingRegistry as a diagnostic side-channel; the setup command reads it once to choose the message. Only the interactive CodeLens flow shows messages — bulk and programmatic environment creation are unaffected.

2. Build inline-script venvs from the real interpreter, not a uv shim

When the extension auto-installs a Python via uv, discovery can select uv's version-named launcher shim on PATH (e.g. ~/.local/bin/python3.8.exe) as the base interpreter. Passing that shim to uv venv --python <shim> fails for Python ≤ 3.10 — uv can't inspect it; the launched interpreter can't locate its standard library and aborts with No module named 'encodings'. (Python 3.11+ tolerates it.) The real interpreter in the uv-managed install folder works for every version.

createWithProgress now resolves the base via a new getBaseInterpreterForVenv: when the environment's executable lives outside its own sysPrefix (a launcher/shim), it uses the interpreter inside the prefix (<sysPrefix>/python.exe or bin/python[3]), falling back to the original executable if none exist. Applied to both the uv venv and python -m venv paths. It is a no-op for normal interpreters, and it also prevents a shim-built venv from recording a pyvenv.cfg home whose python.exe does not exist (which would otherwise make the base-existence check report the base as missing on every reuse).

Testing

  • npm run compile-tests + unit suite: 2001 passing / 0 failing (3 new tests for getBaseInterpreterForVenv).
  • Manually verified on Windows: uv venv --python <shim> fails for 3.8 while uv venv --python <real interpreter> succeeds; the failure boundary is exactly Python ≤ 3.10.

Clicking the PEP 723 "Set up environment for this script" CodeLens and
having setup produce no environment used to be silent -- the reason was
only in the Python Environments output log. The interactive setup
command now surfaces an actionable message:

- a warning for the no-compatible-python case, with a note when
  requires-python is an exact two-segment pin like "==3.11" (PEP 440
  treats it as exactly 3.11.0, which is often not installable);
- a generic "see output" error for other failures;
- silence when the user declined the Python install, or when an
  environment was built but intentionally not associated (the script's
  metadata changed mid-setup).

The env manager records the setup outcome (failed reason or benign
skip) on the shared InlineScriptRoutingRegistry as a diagnostic
side-channel; the setup command reads it once to choose the message.
Only the interactive CodeLens flow shows messages -- bulk and
programmatic environment creation are unaffected.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When the extension auto-installs a Python via uv, discovery can select
uv's version-named launcher shim on PATH (e.g. ~/.local/bin/python3.8.exe)
as the base interpreter. Passing that shim to `uv venv --python <shim>`
fails for Python <= 3.10 because uv cannot inspect it -- the launched
interpreter cannot locate its standard library and aborts with
"No module named 'encodings'". The real interpreter (in the uv-managed
install folder) works for every version.

createWithProgress now resolves the base interpreter via
getBaseInterpreterForVenv: when the environment's executable lives
outside its own sysPrefix (a launcher/shim), it uses the interpreter
inside the prefix (<sysPrefix>/python.exe or bin/python[3]) instead,
falling back to the original executable if none exist. Applied to both
the `uv venv` and `python -m venv` paths, so it is a no-op for normal
interpreters and also prevents a shim-built venv from recording a
pyvenv.cfg `home` whose python.exe does not exist (which would make the
base-existence check fail on every reuse).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* "No compatible Python" message. Calls out an exact two-segment pin like `==3.11` (PEP 440 =
* exactly 3.11.0, often not installable) so the user understands why nothing matched.
*/
function buildNoCompatiblePythonMessage(requiresPython?: string): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this maybe use the new PythonVersion class for pargins and extracting major/minor?

@StellaHuang95
Stella Huang (StellaHuang95) merged commit 731b31c into microsoft:main Sep 4, 2026
45 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants