Windows shortcuts can silently omit CONDA_PREFIX / env config vars from the activation script

Open
#545 6 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

@marcoesters is already working on this.

Since Sep 15, 2026.

  • #553 by @marcoesters — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, shell

Research direction

Start in menuinst/platforms/win.py at WindowsMenuItem._command() and inspect the .env-file branch and its logged_run() activation call. Reproduce shortcut generation from a clean shell and from an already activated target environment, then verify the generated .env consistently includes CONDA_PREFIX, CONDA_DEFAULT_ENV, and configured environment variables.

Written by the indexing model from the issue text.

Description

type::bug
Checklist
  • I added a descriptive title
  • I searched open reports and couldn't find a duplicate
What happened?

Component: menuinst/platforms/win.py, WindowsMenuItem._command() (the .env-file branch used for conda >= 25.3.0)

Summary

When a package's post-install hook calls menuinst.api.install() from inside the environment it's creating a shortcut for (i.e. CONDA_SHLVL/CONDA_PREFIX/CONDA_DEFAULT_ENV already point at that same target prefix), the generated .bat shortcut is missing CONDA_PREFIX, CONDA_DEFAULT_ENV, and any custom variables set via conda env config vars set (from conda-meta/state). PATH and the activate.d/deactivate.d hook calls are present and correct, so the shortcut looks activated but isn't fully.

Root cause

_command() shells out to conda shell.cmd.exe activate <prefix> via logged_run(activator_cmd, check=True, log=False) with no explicit env=, so the subprocess inherits the calling process's environment as-is. conda's activator only emits SET lines for variables whose value differs from what's already in that inherited environment. If the builder process is already activated in the same target env, CONDA_PREFIX/CONDA_DEFAULT_ENV/config vars already match the target exactly, so conda considers them "unchanged" and omits them — even though the resulting script is meant to run standalone, later, in a fresh cmd.exe with none of that ambient state.

Reproduction
  1. Env myenv has a custom var set: conda env config vars set FOO=bar -n myenv.
  2. From a clean shell: conda shell.cmd.exe activate <prefix of myenv> → resulting .env file includes CONDA_PREFIX, CONDA_DEFAULT_ENV, and FOO=bar.
  3. From a shell where myenv is already active (conda activate myenv first): re-run the same shell.cmd.exe activate <prefix> call → the .env file now omits CONDA_PREFIX, CONDA_DEFAULT_ENV, and FOO entirely, while still including PATH, CONDA_SHLVL, and the activate.d/deactivate.d _CONDA_SCRIPT lines.
  4. Since menuinst's post-install-triggered shortcut generation typically requires being run activated (to know CONDA_PREFIX in the first place), any package following that pattern gets a shortcut baked from case 3, not case 2.
Expected

The generated shortcut should be self-contained and always include the full activation state (CONDA_PREFIX, CONDA_DEFAULT_ENV, and all conda-meta/state env_vars), regardless of the activation state of the process that built it.

Suggested fix

In _command(), pass a sanitized env= to the logged_run(activator_cmd, ...) call that strips CONDA_SHLVL, CONDA_PREFIX, CONDA_DEFAULT_ENV, CONDA_PROMPT_MODIFIER, _CE_M, _CE_CONDA before invoking conda shell.cmd.exe activate, so the subprocess always computes activation "from scratch" instead of diffing against whatever state the builder process happens to be in.

Environment
  • conda: >= 25.3.0 (uses the .env-file activation path in win.py, not the older .bat path)
  • OS: Windows
Conda Info

Conda Config

Conda list

Additional Context

No response

Dominant language
Python
Stars
50
Forks
49
Avg merge
1d 22h
Merged PRs (30d)
13

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from conda/menuinst

All issues in conda/menuinst

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.