saltstack / saltstack/salt

[BUG] Windows environ.setenv is not passed to cmd.run spawned processes

Open
#58,774 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug severity-medium Windows
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

Description
Windows doesn't pass the current minion environment to any spawned processes using cmd._run
It is instead using a copy of the environment set in nt.env, which is not updated if the minion updates: https://github.com/saltstack/salt/blob/998c382f5f2c3b4cbf7d96aa6913ada6993909b3/salt/modules/cmdmod.py#L601-L604

Either environ.setenv/setval need to update the nt.environ hash (if possible?), or cmd.run needs to build a correct copy of the env before spawning processes, somehow.

Using permanent or update_minion will not work around this issue, they only fix subsequent runs of the state.

Setup
testenv.sls:

set env:
  environ.setenv:
    - name: TEST_ENV_VAR
    - value: hello there
{%if grains['os'] == 'Windows'%}
spawn a windows test:
  cmd.run:
    - name: $env:TEST_ENV_VAR -eq "hello there"
    - shell: powershell
{%else%}
spawn a linux test:
  cmd.run:
    - name: >-
        [[ $TEST_ENV_VAR == "hello there" ]]
{%endif%}

Steps to Reproduce the behavior
salt '*' state.apply testenv

It will work as expected on linux, windows will not have the env var in the shell and returns False

Expected behavior
Both windows and linux shells have the environment variable set.

Contributor guide

Open the contributing guide

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.

Research direction

Start with salt/modules/cmdmod.py around lines 601-604 and trace how cmd.run/cmd._run prepares the Windows environment for spawned processes. Compare that path with environ.setenv and the provided testenv.sls reproduction; done means the Windows PowerShell command receives TEST_ENV_VAR=hello there in the same state run as the Linux command.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.