Skip to content

[BUG] cmd.run: "- prepend_path:" broken (fails with KeyError exception) on Windows #57672

Open

Description

@dhs-rec

Description
Due to #56199 I changed some states using cmd.run from

- env:
  - PATH: ...

to

- prepend_path: ...

but that also led to failures on (Windows) minions running Salt Minion 3000.3.

I even get two different error outputs for the same kind of thing (running in a loop):

----------
          ID: something_one
    Function: cmd.run
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "c:\salt\bin\lib\site-packages\salt-3000.3-py3.5.egg\salt\state.py", line 1981, in call
                  **cdata['kwargs'])
                File "c:\salt\bin\lib\site-packages\salt-3000.3-py3.5.egg\salt\loader.py", line 1977, in wrapper
                  return f(*args, **kwargs)
                File "c:\salt\bin\lib\site-packages\salt-3000.3-py3.5.egg\salt\states\cmd.py", line 892, in run
                  cret = mod_run_check(cmd_kwargs, onlyif, unless, creates)
                File "c:\salt\bin\lib\site-packages\salt-3000.3-py3.5.egg\salt\states\cmd.py", line 366, in mod_run_check
                  cmd = __salt__['cmd.retcode'](unless, ignore_retcode=True, python_shell=True, **cmd_kwargs)
                File "c:\salt\bin\lib\site-packages\salt-3000.3-py3.5.egg\salt\modules\cmdmod.py", line 2277, in retcode
                  **kwargs)
                File "c:\salt\bin\lib\site-packages\salt-3000.3-py3.5.egg\salt\modules\cmdmod.py", line 598, in _run
                  run_env['PATH'] = ':'.join((prepend_path, run_env['PATH']))
              KeyError: 'PATH'
     Started: 14:23:09.012359
    Duration: 62.504 ms
     Changes:

and

----------
          ID: something_two
    Function: cmd.run
      Result: False
     Comment: 'PATH'
     Started: 14:24:12.652165
    Duration: 15.624 ms
     Changes:

The reason for this seems to be the code for prepend_path in cmdmod.py, which looks like being Linux/Unix only (https://github.com/saltstack/salt/blob/master/salt/modules/cmdmod.py#L619). It

  1. uses a variable name of "PATH", but all Windows systems I looked at use "Path" and the environment variable code seems to be case-sensitive (now?)
  2. always uses ":" as path separator, but Windows requires ";"

Setup
Salt Minion 3000.3

Steps to Reproduce the behavior
Execute any cmd.run: state containing - prepend_path: on a Windows Minion running 3000.3

Expected behavior
It should not fail.

Activity

  1. added
    bug
    on Jun 15, 2020
  2. added this to the Approved milestone on Jun 16, 2020
  3. added
    Magnesium
    severity-high
    on Jun 25, 2020
  4. removed
    Magnesium
    on Sep 29, 2020
  5. xeacott commented on Feb 2, 2021

    @xeacott
    Contributor

    Ah yes... debugging this now. Question about prepend_path for Windows here. Is that something that should change the system path permanently or the current shell's env:$Path?

    Effectively implementing either here:

    [Environment]::SetEnvironmentVariable
         ("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
    
    [Environment]::SetEnvironmentVariable
         ("INCLUDE", $env:INCLUDE, [System.EnvironmentVariableTarget]::User)
    
  6. xeacott commented on Feb 2, 2021

    @xeacott
    Contributor

    I'm guessing though that we wouldn't want to change the system path, unless the user specifically requested it. In either case...im thinking this is enough to add the prepend_path path to.. path. lol.

        if prepend_path:
            if salt.utils.platform.is_windows():
                run_env["PATH"] = ";".join((prepend_path, run_env["Path"]))
            run_env["PATH"] = ":".join((prepend_path, run_env["PATH"]))
    
  7. added
    Phosphorus v3005.0
    on Jun 18, 2021
  8. removed
    Phosphorus v3005.0
    on Apr 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Execution-ModuleWindowsbugseverity-high

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions