Skip to content

Resolve symbolic links for environment metadata - #2686

Merged
gaborbernat merged 2 commits into
pypa:mainfrom
ofek:resolve
Feb 21, 2024
Merged

gaborbernat merged 2 commits into
pypa:mainfrom
ofek:resolve

Conversation

@ofek

@ofek ofek commented Jan 22, 2024 •

Copy link
Copy Markdown
Contributor

Resolves #2682

Caution

DO NOT CHECKOUT THE FIRST COMMIT!
I tried running tox -e py311 and it completely wiped my Python installation. 😢

@gaborbernat
gaborbernat marked this pull request as draft January 22, 2024 19:11
@ofek
ofek marked this pull request as ready for review January 22, 2024 22:34
@ofek

ofek commented Jan 22, 2024

Copy link
Copy Markdown
Contributor Author

This is ready for review.

@gaborbernat gaborbernat left a comment

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.

Tests please 😁

@gaborbernat

Copy link
Copy Markdown
Contributor

@ofek plan to pick it up again, or shall I close it?

@ofek

ofek commented Jan 31, 2024

Copy link
Copy Markdown
Contributor Author

How do you recommend testing?

@gaborbernat

Copy link
Copy Markdown
Contributor

A unit test sounds good 👍

@ofek

ofek commented Feb 21, 2024

Copy link
Copy Markdown
Contributor Author

Oh snap sorry, I forgot to follow-up with the test

@ofek
ofek deleted the resolve branch February 21, 2024 22:01
stefanor added a commit to stefanor/virtualenv that referenced this pull request Apr 29, 2025
gaborbernat added a commit that referenced this pull request Oct 1, 2026
`pyvenv.cfg` can end up with a path where a plain value should be,
depending on what happens to sit in the directory virtualenv is run
from.

`PyEnvCfg.write()` makes a value absolute when it names something that
exists. The check runs for every key and is relative to the working
directory, so a value that isn't a path gets replaced whenever the
current folder has an entry with the same name. On current `main`:

- `virtualenv 3.14` with Python 3.14 writes `python-version =
/cwd/3.14`, because the environment folder itself matches the value
- with a file or folder called `true` in the working directory,
`--system-site-packages` writes `include-system-site-packages =
/cwd/true`. `site.py` only accepts `true`, so the environment comes up
without the system site-packages and nothing says why
- a `cpython` folder in the working directory does the same to
`implementation` on Windows and macOS, where the lookup ignores case

I reproduced all three on Windows and the first two on Linux.

The check dates back to #2686 (it resolved symlinks then, #3022 changed
it to `abspath`). It only makes sense for keys that hold a path, so this
limits it to those: `home`, `executable`, `base-prefix`,
`base-exec-prefix`, `base-executable` and GraalPy's
`venvlauncher_command`. Every other value is written as given. One thing
to be aware of: a creator plugin that adds a path key of its own no
longer gets it made absolute.

`test_pyenv_cfg_preserves_symlinks` used a made-up key (`test_path`), so
it now uses `home`. There are two new tests. One checks that plain
values survive when the working directory has an entry of the same name,
the other that a relative `home` is still made absolute. Putting the old
line back fails the first on all four keys. Never calling `abspath`, or
dropping `home` from the list, fails the second. Using `realpath` in
place of `abspath` fails the symlink test (run on Linux, my Windows
machine can't create symlinks).

On the outside input: it is the names of files and folders in the
working directory, plus the destination name. Before, those could change
what a non-path key says, and after this they can't. No new input is
read, values still go through the line boundary collapsing from #3247,
and nothing on this path is executed.

- [x] ran the linter to address style issues (`tox -e fix`)
- [x] wrote descriptive pull request text
- [x] ensured there are test(s) validating the fix
- [x] added news fragment in `docs/changelog` folder
- [ ] updated/extended the documentation
- [x] for changes to activation scripts, `pyvenv.cfg`, wheel downloads,
app-data or release workflows: explained in the
PR text which outside input the change handles, such as a path, a prompt
or a downloaded file, and how it stops that
  input from running as code. See the
[security
scope](https://github.com/pypa/virtualenv/blob/main/.github/SECURITY.md#scope).

---------

Co-authored-by: Bernat Gabor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

venv base-executable path does not resolve symlinks using realpath()

2 participants