Skip to content

pwsh 7 can't parse path in WSL when using Windows integration #12020

Closed

Description

@gdoenlen

I just upgraded to pwsh 7 in my WSL instance and it causes the process to exit when using windows integration of $PATH with an error of: bad variable name: (x86)\Nvidia

It persists even after removing this folder from my windows path (the next error is about a bad variable for Files/Microsoft. This only occurs when starting a fresh WSL instance and doesn't occur when running pwsh from bash.

Changing HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{YOUR_GUID}\Flags to 5 will disable the integration and fix this behavior.

Changing the above registry key to 7 (enable integration) and using bash as your shell will also fix this behavior.

Steps to reproduce

  1. set your WSL shell to pwsh 7 with: chsh -s /path/to/pwsh
  2. Make sure windows integration of path is enabled in the registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{YOUR_GUID} and the Flags key is set to 7. Setting this key to 5 turns the integration off and pwsh will start fine. Additionally setting this back to 7 and setting bash as your shell will have WSL start fine as well.
  3. Restart WSL and the path should fail (depending on your windows path of course).

Expected behavior

Path should be properly parsed.

Actual behavior

Failures related to bad variable names.

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Linux 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Question
    on Mar 5, 2020
  2. iSazonov commented on Mar 5, 2020

    @iSazonov
    Collaborator

    I think it is a problem with Windows paths sharing.
    See microsoft/WSL#1640.

    Steve Lee (@SteveL-MSFT) Rob Holt (@rjmholt) Sean Wheeler (@sdwheeler) I believe this is "by-design" and the workaround could be documented.

  3. gdoenlen commented on Mar 5, 2020

    @gdoenlen
    Author

    Yes I know it's from path sharing, hence the registry edits. The work around isn't ideal as you lose access to everything in windows without typing the full path to something (i.e. vscode). Pwsh 6 definitely didn't have the same behavior.

  4. rjmholt commented on Mar 5, 2020

    @rjmholt
    Collaborator

    It’s a known issue arising from the fact that 7, unlike 6, supports being run as a login shell.

    This has shed new light on it though

  5. iSazonov commented on Mar 5, 2020

    @iSazonov
    Collaborator

    Rob Holt (@rjmholt) Is it WSL or PowerShell issue? I did not find we parse PATH in the code.

  6. rjmholt commented on Mar 5, 2020

    @rjmholt
    Collaborator

    Quite possibly a PowerShell issue. exec cares about the path. We use it here:

    if (isMacOS)
    {
    return Exec("/bin/zsh", execArgs);
    }
    return Exec("/bin/sh", execArgs);

  7. iSazonov commented on Mar 5, 2020

    @iSazonov
    Collaborator

    Rob Holt (@rjmholt) You referenced MacOs code :-).

    In any case we don't get explicitly PATH value and don't parse it.

    Can you catch the exception in debug build?

  8. rjmholt commented on Mar 5, 2020

    @rjmholt
    Collaborator

    You referenced MacOs code

    The second part is Linux, but my point is about exec.

    If exec is failing there will be no exception; it's a native API.

    I could be wrong about the provenance of the issue though. We need to see the whole error message (if you could add that please George (@gdoenlen))

  9. gdoenlen commented on Mar 5, 2020

    @gdoenlen
    Author

    Rob Holt (@rjmholt) The part after export will change depending on what it encounters in the path first.

    : 6: export: Files/Microsoft: bad variable name
    
    [process exited with code 2] <-- this is probably from windows terminal
    
  10. gdoenlen commented on Mar 5, 2020

    @gdoenlen
    Author

    Does powershell leave a log anywhere? I could check that as well.

  11. rjmholt commented on Mar 5, 2020

    @rjmholt
    Collaborator

    Does powershell leave a log anywhere

    I think it can be configured to, but I don't think at the level we're looking for; it's designed to log commands rather than its internals. But all of this happens so early anyway that we avoid all possible work to reduce startup time (since we have to start up twice).

  12. iSazonov commented on Mar 5, 2020

    @iSazonov
    Collaborator

    So we see bash error for export. I guess it is possible to repo without PowerShell.

  13. gdoenlen commented on Mar 5, 2020

    @gdoenlen
    Author

    Just for reference I believe /bin/sh points to dash and not bash. My posix shell isn't so great but maybe it's running something from /etc/profile or /etc/profile.d when you use it as a login shell? Though it is odd that bash and zsh as my login shell don't do the same.

  14. rjmholt commented on Mar 5, 2020

    @rjmholt
    Collaborator

    Just for reference I believe /bin/sh points to dash and not bash. My posix shell isn't so great but maybe it's running something from /etc/profile or /etc/profile.d when you use it as a login shell?

    Interesting. We specifically intend to work with dash, but running as login deliberately runs either /etc/profile or ~/.profile. Those are supposed to be /bin/sh compatible according to POSIX

  15. 13 remaining items

  16. gdoenlen commented on Mar 16, 2020

    @gdoenlen
    Author

    Thank you Michael Klement (@mklement0) ! It seems like shellcheck fails to catch this as well.

    /etc/profile.d $ shellcheck -s sh apps-bin-path.sh
    
    In apps-bin-path.sh line 5:
    if [ -n "${PATH##*${snap_bin_path}}" -a -n "${PATH##*${snap_bin_path}:*}" ]; then
                                         ^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
    
    
    In apps-bin-path.sh line 19:
    if [ -n "${XDG_DATA_DIRS##*${snap_xdg_path}}" -a -n "${XDG_DATA_DIRS##*${snap_xdg_path}:*}" ]; then
                                                  ^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
  17. mklement0 commented on Mar 16, 2020

    @mklement0
    Contributor

    Good point, George (@gdoenlen) - a request to add such a check has already been made (by the author of shellcheck himself): koalaman/shellcheck#951

  18. mklement0 commented on Mar 16, 2020

    @mklement0
    Contributor

    I've updated the previous comment to paint what I hope is the full picture, but in a nutshell:

    • The root cause is the poorly written /etc/profile.d/apps-bin-path.sh script that ships with Ubuntu.
    • The problem surfaces predictably in WSL (with Windows path integration turned on, which is true by default) with pwsh as the user's default shell, given that WSL of necessity always invokes user shells as a login shell to ensure /etc/profile processing - unlike in actual Linux (virtual) machines, where that isn't necessary.
      • The problem can hypothetically also occur in actual Linux machines, albeit with a different symptom: if you add a directory with spaces to PATH (e.g. via /etc/enviroment), a blocking error message will pop up during system startup.
  19. ExE-Boss commented on Feb 5, 2021

    @ExE-Boss

    This has now been fixed in the snapd GitHub repository: canonical/snapd#9888 (canonical/snapd@ae84b65).

  20. gdoenlen commented on Feb 5, 2021

    @gdoenlen
    Author

    This has now been fixed in the snapd GitHub repository: snapcore/snapd#9888 (snapcore/snapd@ae84b65).

    Any idea when this will make it into WSL? As of today pwsh 7.1.1 is still broken as a login shell.

  21. added
    WG-Engine
    Needs-Triage
    on Feb 6, 2021
  22. added
    Resolution-External
    and removed
    Needs-Triage
    on Feb 4, 2022
  23. ghost closed this as completedon Feb 5, 2022
  24. rkeithhill commented on Feb 5, 2022

    @rkeithhill
    Collaborator

    The Engine WG reviewed this issue yesterday. We believe this is an external issue. While snapd has applied a fix, that fix was not available on the WSL Ubuntu 20.04 distro we tested. Applying the fix "..." around the RHS manually seems to allow PowerShell to run as a login shell:

    hillr@Keith-Dell8500:/$ sh -l
    $ exit
    hillr@Keith-Dell8500:/$ pwsh -l
    PowerShell 7.2.0
    Copyright (c) Microsoft Corporation.
    
    https://aka.ms/powershell
    Type 'help' to get help.
    
       A new PowerShell stable release is available: v7.2.1
       Upgrade now, or check out the release page at:
         https://aka.ms/PowerShell-Release?tag=v7.2.1
    
    PS />
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-QuestionOS-WSLResolution-ExternalWG-Engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions