Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spaces in windows path breaks in Bash #1766

Closed
themaninthesuitcase opened this issue Mar 9, 2017 · 31 comments
Closed

Spaces in windows path breaks in Bash #1766

themaninthesuitcase opened this issue Mar 9, 2017 · 31 comments

Comments

@themaninthesuitcase
Copy link

Brief description

Using windows tools from Bash fails when using a path that has a space.

The windows path is added to the $PATH and some translation happens ie C: => /mnt/c. Spaces are not managed in this translation. This means any windows path that includes a space (Program Files or user paths with a space as below) fail to find files.

Expected results

Files found and are accessible.

Actual results

username@DESKTOP-M7UD40T:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Program Files/nodejs:/mnt/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/mnt/c/Program Files/dotnet:/mnt/c/Program Files/Git/cmd:/mnt/c/Users/User Name/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/User Name/AppData/Local/atom/bin:/mnt/c/Users/User Name/AppData/Roaming/npm
username@DESKTOP-M7UD40T:~$ apm
/mnt/c/Users/User Name/AppData/Local/atom/bin/../app-1.14.4/resources/cli/../app/apm/bin/apm: line 37: /mnt/c/Users/User Name/AppData/Local/atom/app-1.14.4/resources/app/apm/bin/node: No such file or directory
username@DESKTOP-M7UD40T:~$ ls /mnt/c/Users/User Name/AppData/Local/atom/app-1.14.4/resources/app/apm/bin
ls: cannot access 'Name/AppData/Local/atom/app-1.14.4/resources/app/apm/bin': No such file or directory
/mnt/c/Users/User
username@DESKTOP-M7UD40T:~$ ls "/mnt/c/Users/User Name/AppData/Local/atom/app-1.14.4/resources/app/apm/bin"
apm  apm.cmd  etc  node.exe  npm  npm.cmd  python-interceptor.sh
username@DESKTOP-M7UD40T:~$

Windows build number

Microsoft Windows [Version 10.0.15042]

Repro Steps
Install Atom Editor & node.js in windows land
open bash shell
run apm command

@aseering
Copy link
Contributor

aseering commented Mar 9, 2017

Hi @themaninthesuitcase -- just for completeness, could you try running apm.exe (including the .exe)? WSL requires the file extension.

@emrsmsrli
Copy link

you can use escape character \ before a space character as in "C:\Users\Emre\ Simsirli".

@aseering
Copy link
Contributor

aseering commented Mar 10, 2017

@themaninthesuitcase -- I have verified that WSL is behaving correctly. The problem here is that you forgot to type the ".exe" on the end of the command name. Spaces do not need to be escaped in Linux $PATH.

@aseering
Copy link
Contributor

aseering commented Mar 10, 2017

@emrsmsrli -- (a) that's a Windows path, not a Linux path; and (b) \ is not a Windows path escape character. Your example path represents a folder named Simsirli (note the leading space) inside a folder named Emre.

@themaninthesuitcase
Copy link
Author

themaninthesuitcase commented Mar 10, 2017

There's actually apm executable also There isn't an exe, just apm which is a bash script to detect if it needs to use exe. I did test with .exe but also didn't work.

cpearson @ DESKTOP-M7UD40T ~
$ apm.exe
apm.exe: command not found
cpearson @ DESKTOP-M7UD40T ~
$ apm
/mnt/c/Users/Chris Pearson/AppData/Local/atom/bin/../app-1.14.4/resources/cli/../app/apm/bin/apm: line 37: /mnt/c/Users/Chris Pearson/AppData/Local/atom/app-1.14.4/resources/app/apm/bin/node: No such file or directory
cpearson @ DESKTOP-M7UD40T ~
$ ls "/mnt/c/Users/Chris Pearson/AppData/Local/atom/app-1.14.4/resources/app/apm/bin"
apm  apm.cmd  etc  node.exe  npm  npm.cmd  python-interceptor.sh
cpearson @ DESKTOP-M7UD40T ~
$ file "/mnt/c/Users/Chris Pearson/AppData/Local/atom/app-1.14.4/resources/app/apm/bin/apm"
/mnt/c/Users/Chris Pearson/AppData/Local/atom/app-1.14.4/resources/app/apm/bin/apm: Bourne-Again shell script, ASCII text executable
cpearson @ DESKTOP-M7UD40T ~
$ cat "/mnt/c/Users/Chris Pearson/AppData/Local/atom/app-1.14.4/resources/app/apm/bin/apm"
#!/bin/bash

set -e

initialCwd=`pwd -P`

apmPath=$0
builtin cd "`dirname "$apmPath"`"
binDir=`basename "$apmPath"`

# Detect node binary name
osName=`uname -s`
if [ "${osName:0:10}" == 'MINGW32_NT' ]; then
  nodeBin="node.exe"
else
  nodeBin="node"
fi

while [ -L "$binDir" ]
do
  binDir=`readlink "$binDir"`
  builtin cd "`dirname "$binDir"`"
  binDir=`basename "$binDir"`
done

binDir=`pwd -P`

maybe_node_gyp_path="$binDir"/../node_modules/.bin/node-gyp
if [ -e "$maybe_node_gyp_path" ]
then
  export npm_config_node_gyp="$maybe_node_gyp_path"
fi

export PYTHON="${binDir}/python-interceptor.sh"

builtin cd "$initialCwd"
"$binDir/$nodeBin" "$binDir/../lib/cli.js" "$@"
cpearson @ DESKTOP-M7UD40T ~
$

My current theory seems to be paths in the Windows path section with spaces do not work and would need to be escaped when combined with the Ubuntu path.

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Program Files/nodejs:/mnt/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/mnt/c/Program Files/dotnet:/mnt/c/Program Files/Git/cmd:/mnt/c/Users/Chris Pearson/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Chris Pearson/AppData/Local/atom/bin:/mnt/c/Users/Chris Pearson/AppData/Roaming/npm

@themaninthesuitcase
Copy link
Author

Just noticed a bump in window version happened last night: Microsoft Windows [Version 10.0.15048]

@emrsmsrli
Copy link

@aseering it at least is a workaround for changing directories which contain spaces. please see the image below, there first command gives the error directory does not exist but the second one
succeeded.

capture

@themaninthesuitcase
Copy link
Author

@emrsmsrli this isn't all that useful when it's the automatically generated path (Ubuntu + Windows) that has the spaces. I don't plan on typing /mnt/c/some\ long\ path\app.exe every time I want to run something that's already in the path.

My envisioned fix is that the auto combine process is updated to escape spaces exactly like that though. It's not just "User Name" paths that will see this issue but also anything in Program Files.

@aseering
Copy link
Contributor

aseering commented Mar 13, 2017

@emrsmsrli -- if you manually update $PATH to contain backslashes, does it find your apm binary?

@themaninthesuitcase
Copy link
Author

Looks like it "breaks" the path and the issue is more complex than I expected.

Summary of the below:

  • Dump the PATH to console
  • test the apm command
  • update to add \[space] escapes
  • test apm command
  • which apm to show the path is no longer evaluated fully where it was pre escape or else the issue wouldn't have been happening.

I am wondering if this issue is not a WSL issue but the app/shell script isn't managing spaces in the paths internally.

cpearson @ DESKTOP-M7UD40T ~
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Program Files/nodejs:/mnt/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/mnt/c/Program Files/dotnet:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files (x86)/Microsoft SQL Server/Client SDK/ODBC/130/Tools/Binn:/mnt/c/Program Files (x86)/Microsoft SQL Server/130/Tools/Binn:/mnt/c/Program Files (x86)/Microsoft SQL Server/130/DTS/Binn:/mnt/c/Program Files (x86)/Microsoft SQL Server/130/Tools/Binn/ManagementStudio:/mnt/c/Users/Chris Pearson/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Chris Pearson/AppData/Local/atom/bin:/mnt/c/Users/Chris Pearson/AppData/Roaming/npm
cpearson @ DESKTOP-M7UD40T ~
$ apm
/mnt/c/Users/Chris Pearson/AppData/Local/atom/bin/../app-1.15.0/resources/cli/../app/apm/bin/apm: line 37: /mnt/c/Users/Chris Pearson/AppData/Local/atom/app-1.15.0/resources/app/apm/bin/node: No such file or directory
cpearson @ DESKTOP-M7UD40T ~
$ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Program\ Files/nodejs:/mnt/c/Program\ Files/Microsoft\ SQL\ Server/130/Tools/Binn:/mnt/c/Program\ Files/dotnet:/mnt/c/Program\ Files/Git/cmd:/mnt/c/Program\ Files\ (x86)/Microsoft\ SQL\ Server/Client\ SDK/ODBC/130/Tools/Binn:/mnt/c/Program\ Files\ (x86)/Microsoft\ SQL\ Server/130/Tools/Binn:/mnt/c/Program\ Files\ (x86)/Microsoft\ SQL\ Server/130/DTS/Binn:/mnt/c/Program\ Files\ (x86)/Microsoft\ SQL\ Server/130/Tools/Binn/ManagementStudio:/mnt/c/Users/Chris\ Pearson/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Chris\ Pearson/AppData/Local/atom/bin:/mnt/c/Users/Chris\ Pearson/AppData/Roaming/npm"
cpearson @ DESKTOP-M7UD40T ~
$ apm
The program 'apm' is currently not installed. You can install it by typing:
sudo apt install apmd
cpearson @ DESKTOP-M7UD40T ~
$ which apm
cpearson @ DESKTOP-M7UD40T ~
$

@aseering
Copy link
Contributor

Again, @themaninthesuitcase , my machine has no problems finding binaries in paths containing spaces. $PATH does not need spaces to be escaped on my machine. In fact, escaping spaces in $PATH breaks it on my machine. The default bash shell has no problem handling paths with spaces; it correctly quotes them internally.

Is it possible that the problem isn't with the $PATH variable but with some other piece of your environment?

@themaninthesuitcase
Copy link
Author

Looking at the errors it seems it's a propblem internal to this app, I'll close.

@a-nadig
Copy link

a-nadig commented Oct 27, 2017

I have a similar issue with automatic handling of spaces. I tried creating a python virtual environment in bash on windows but I can't pip install anything because the parent folder has a space in it

aaa@ideapad:/mnt/e/foo bar/project1$ virtualenv venv
New python executable in /mnt/e/foo bar/project1/venv/bin/python
Installing setuptools, pip, wheel...done.
aaa@ideapad:/mnt/e/foo bar/project1$ . venv/bin/activate

(venv) aaa@ideapad:/mnt/e/foo bar/project1$ pip install dateparser
bash: /mnt/e/foo bar/project1/venv/bin/pip: "/mnt/e/foo: bad interpreter: No such file or directory
(venv) aaa@ideapad:/mnt/e/foo bar/project1$ pip freeze
bash: /mnt/e/foo bar/project1/venv/bin/pip: "/mnt/e/foo: bad interpreter: No such file or directory

PS. I wasn't sure if i should comment on closed issues. Do let me know if im doing the wrong thing here

@solesensei
Copy link

Hey, there.
I stucked with same problem.

SoleSensei@SoleSurface:~$ $BROWSER
-bash: /mnt/c/Program: No such file or directory
SoleSensei@SoleSurface:~$ export BROWSER='"/mnt/c/Program\ Files/Firefox/firefox.exe"'
SoleSensei@SoleSurface:~$ $BROWSER
-bash: "/mnt/c/Program\: No such file or directory
SoleSensei@SoleSurface:~$ export BROWSER="/mnt/c/Program\ Files/Firefox/firefox.exe"
SoleSensei@SoleSurface:~$ $BROWSER
-bash: /mnt/c/Program\: No such file or directory
SoleSensei@SoleSurface:~$ export BROWSER=/mnt/c/Program\ Files/Firefox/firefox.exe
SoleSensei@SoleSurface:~$ $BROWSER
-bash: /mnt/c/Program: No such file or directory
SoleSensei@SoleSurface:~$ export BROWSER='/mnt/c/Program Files/Firefox/firefox.exe'
SoleSensei@SoleSurface:~$ $BROWSER
-bash: /mnt/c/Program: No such file or directory
SoleSensei@SoleSurface:~$ xdg-open https://github.com
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: /mnt/c/Program: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: /mnt/c/Program: not found
xdg-open: no method available for opening 'https://github.com'

Path just breaks on first space. Any ideas?

@therealkenc
Copy link
Collaborator

$ export BROWSER="/mnt/c/Program Files/Firefox/firefox.exe"
$ "$BROWSER"

@KoryNunn
Copy link

Hi, The solution to this is to double-quote the string eg:

$ export BROWSER="/mnt/c/Program Files/Firefox/firefox.exe"
$ "$BROWSER"

Found here: https://stackoverflow.com/questions/40135502/opening-sublime-text-from-bash-on-ubuntu-on-windows

@solesensei
Copy link

@therealkenc, @KoryNunn thanks, guys

Xdg-open and other applications using unquoted environment variables stay with this problem.

$ xdg_open xdg-open https://github.com
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: /mnt/c/Program: not found

@cbdoc
Copy link

cbdoc commented Dec 7, 2018

This is definitely a bug and should be fixed. To fix manually:

In file /usr/bin/sensible-browser, update line that reads ${BROWSER} "$1" to "${BROWSER}" "$1"

@JamieRRobillardSr
Copy link

JamieRRobillardSr commented Dec 7, 2018 via email

@cbdoc
Copy link

cbdoc commented Dec 7, 2018

I'm quite familiar with quoting and escape characters in nix. It simply does not work when passing the $BROWSER variable from bash to sensible-browser. In .bashrc, I tried single quote, double quotes, escape characters and even double quote embedded in single quote:
export BROWSER="/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"
export BROWSER='/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
export BROWSER='"/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"'
export BROWSER="/mnt/c/Program\ Files\ (x86)/Google/Chrome/Application/chrome.exe"

All produce the error:
$ source ~/.bashrc
$ /usr/bin/sensible-browser
/usr/bin/sensible-browser: 6: /usr/bin/sensible-browser: /mnt/c/Program: not found

The only fix so far: In file /usr/bin/sensible-browser, update line that reads ${BROWSER} "$1" to "${BROWSER}" "$1"

This is definitely a bug and should be fixed.

This is not a bug.

I assume he means "bug in /usr/bin/sensible-browser". There is, needless to say, no bug in WSL or Linux. Looks like you could report the bug here. I'd tell you where to to upstream a patch but the upstream Debian git link 404s.

I'm quite familiar with quoting and escape characters in nix

Then you know you can't fix this with a different $BROWSER shell variable (f.e. in .bashrc). Because, the $BROWSER variable already contains the correct path if you quote it.

Good point re bug in sensible-browser. Will report there.

@therealkenc
Copy link
Collaborator

This is definitely a bug and should be fixed.

This is not a bug.

I assume he means "bug in /usr/bin/sensible-browser". There is, needless to say, no bug in WSL or Linux. Looks like you could report the bug here. I'd tell you where to to upstream a patch but the upstream Debian git link 404s.

I'm quite familiar with quoting and escape characters in nix

Then you know you can't fix this with a different $BROWSER shell variable (f.e. in .bashrc). Because, the $BROWSER variable already contains the correct path if you quote it.

@virgilwashere
Copy link

virgilwashere commented May 19, 2019

Or use a wrapper script, and put it in your PATH. (ie $HOME/bin)

#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# Filename:     chrome
# Description:  chrome.exe wrapper for $BROWSER

# "unofficial" bash strict mode. See: http://redsymbol.net/articles/unofficial-bash-strict-mode
set -o errexit  # Exit when simple command fails               'set -e'
#set -o errtrace # Exit on error inside any functions or subshells.
set -o nounset  # Trigger error when expanding unset variables 'set -u'
set -o pipefail # Do not hide errors within pipes              'set -o pipefail'
IFS=$'\n\t'

declare -gx CHROME
CHROME="/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"

find_browser_env() {
  if [[ -z "${BROWSER-}" ]]; then
    printf 'Cannot find BROWSER environment variable. Add this to your ~/.bashrc:\n\n' >&2
    printf '\tdeclare -g BROWSER\n' >&2
    printf '\tBROWSER=%s\n' "${BASH_SOURCE[0]}" >&2
    # shellcheck disable=SC2016
    printf '\tif [[ -x "${BROWSER}" ]]; then\n' >&2
    printf '\t  export BROWSER\n' >&2
    printf '\telse\n' >&2
    printf '\t  unset -v BROWSER\n' >&2
    printf '\tfi\n' >&2
  fi
}

"${CHROME}" "$@"

find_browser_env

The bare essentials version:

#!/usr/bin/env bash
CHROME="/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"
"${CHROME}" "$@"

@TuanWorkMail
Copy link

I have the error of $PATH got cut off after the first space when I installed yvm

When installing a fresh Ubuntu, echo $PATH show up correctly, after install yvm the problem appeared

@constantineuk
Copy link

In case you need to run incognito mode from your WSL
export BROWSER="/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"
"${BROWSER}" -incognito "https://www.google.com" &

@br4nnigan
Copy link

br4nnigan commented Apr 20, 2021

I don't know why anyone here faults this to the user. In Windows paths don't need to be escaped, that's why no program out there escapes their path they add.

xxx@PC:~$ $PATH -bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Python38/Scripts/:/mnt/c/Python38/:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program: No such file or directory

Notice how everything after "Program Files" is cut off?

How is it not WSL's bug when it automatically adds Windows paths which work on Windows like they are, to the bash in a way that breaks them?

What is the user supposed to do, modify all their Windows paths? And do it again any time some program adds their path?

@therealkenc
Copy link
Collaborator

therealkenc commented Apr 20, 2021

$ $PATH -bash:

That is linux-behavior. You've attempted to execute a long string with colons and forward slashes in it. The long string before the space is not a directory in your Linux filesystem, hence the error.

In Windows paths don't need to be escaped

They do, because in the example below, C:\Program.exe could be an executable file and Files\Git\bin\git.exe might be the first parameter to that program. [Setting aside that IRL "C:\Program Files" is spoken for.]

image

What is the user supposed to do

Opening a discussion with your use case and and detailed repro steps is one approach. Superuser (and similar) is another, because there are more eyeballs and the behavior is not specific to WSL.

@andreas-becker
Copy link

I fixed the error, by not adding the Windows Path entries to the Linux Path.
Open a wsl terminal, create or edit the wsl.conf file under /etc with the following command

sudo nano /etc/wsl.conf

Add the following content:

[interop]
appendWindowsPath = false

Save the file and restart the wsl.
I used the following command to shutdown the wsl (closing the terminal window and open it again didn't work)

wsl.exe --shutdown

Start a new wsl termial and check if the Windows Path entries are gone

echo $PATH

Output should look like

/home/[username]/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

@br4nnigan
Copy link

my $PATH is now showing correctly, maybe an update fixed it

name@machine:~> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files (x86)/Common Files/Intel/Shared Libraries/redist/intel64_win/compiler:/mnt/c/Program Files (x86)/Common Files/Intel/Shared Libraries/redist/intel64/compiler:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/PuTTY/:/mnt/c/Program Files/Microsoft SQL Server/Client SDK/ODBC/170/Tools/Binn/:/mnt/c/Program Files (x86)/Microsoft SQL Server/150/Tools/Binn/:/mnt/c/Program Files/Microsoft SQL Server/150/Tools/Binn/:/mnt/c/Program Files/Microsoft SQL Server/150/DTS/Binn/:/mnt/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn/:/mnt/c/Program Files/Azure Data Studio/bin:/mnt/c/Program Files (x86)/nodejs/:/mnt/c/Program Files (x86)/Common Files/Adobe/AGL:/mnt/c/Program Files/php-8.0.6-nts-Win32-vs16-x64:/mnt/c/ProgramData/ComposerSetup/bin:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Users/WinName/AppData/Local/Programs/Python/Python39/Scripts/:/mnt/c/Users/WinName/AppData/Local/Programs/Python/Python39/:/mnt/c/Users/WinName/AppData/Local/Microsoft/WindowsApps:/mnt/c/Program Files/Azure Data Studio/bin:/mnt/c/Users/WinName/AppData/Roaming/npm:/mnt/c/Users/WinName/AppData/Local/atom/bin:/mnt/c/Users/WinName/AppData/Roaming/Composer/vendor/bin:/home/name/bin:/usr/local/bin:/usr/bin:/bin

@Tharius-Dawnbreaker
Copy link

Tharius-Dawnbreaker commented Oct 12, 2021

I had great difficulty getting the escaped strings correct when appending a path so I just used the 8.3 file names (dir /x), e.g. /mnt/c/progra~1/MiFA7F~1/bin and life was good.

Ventzy added a commit to Financial-Times/n-gage that referenced this issue Apr 11, 2022
As an WSL user, I was getting `env: 'Files': Permission denied` error for each `make` command I ran in few different FT repos. I looked up into n-gage source and I was able to locate the reason. It is not caused by n-gage or make, but by having Windows paths with spaces added to Linux path. Got this solution microsoft/WSL#1766 (comment) after googling and described the problem and the workaround into this PR. I have applied on my system and it solved the problem. Suggesting to add it to the README.
@humandevs
Copy link

humandevs commented Aug 15, 2022

I'm having the same issue. The PATH that WSL2 imports from Windows is broken, as it is not escaping characters properly. Restarting WSL2 does not fix.

I am not adding anything to the path, and .bashrc is vanilla. This is the path that WSL2 creates on boot.

$PATH outputs:
bash: /home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program: No such file or directory

This broken path causes issues when installing nvm, as nvm, as per the official documentation from Microsoft for installing nvm/node tells you to run the shell script to install, which appends to the path, which does not work since there is a broken path in the middle.

This appears to be a bug in /etc/wsl.conf line 6, export PATH=$PATH:"${snap_bin_path}" which imports the PATH variable but does not properly escape characters.

I can't seem to edit this file, but I wrote a workaround to escape all paths that have spaces. This can be added to ~/.profile to fix this issue, but shouldn't this really be fixed by Windows?

`# fix for Windows Subsystem for Linux not escaping imported paths with quotes (escapes ALL paths with quotes)

since can't edit /etc/wsl.conf directly to fix line 6 which imports the snap_bin_path from windows:

Can't use sed since it doesn't have lazy match, so just use a string split function:

IN=$PATH
ESCAPEDPATH=``
while [ "$IN" != "$iter" ] ;do
# extract the substring from start of string up to delimiter.
iter=${IN%%:}
# delete this first "element" AND next separator, from $IN.
IN="${IN#$iter:}"
# Print (or doing anything with) the first "element".
if [[ $iter = " " && $iter != "
" && $iter != '*' ]]; then
# string contains a space, and is not already escaped with single or double quotes
# so escaping string and appending to new path variable
# echo "space: '$iter'"
ESCAPEDPATH=$ESCAPEDPATH:"$iter"
else
# string does not contain a space
echo "no space: $iter:"
ESCAPEDPATH=$ESCAPEDPATH:$iter
fi
done
PATH=$ESCAPEDPATH`

There's a thread on Stack overflow about this here:
https://stackoverflow.com/questions/62429828/wsl-windows-subsystem-linux-breaks-path-when-the-windows-path-has-folder-name

@nrhint
Copy link

nrhint commented Sep 12, 2022

As much discussion of "This is a user problem" as there is this does break scripts that are not perfectly written. Many scripts that would/should work on a real Linux box fail here because there are no spaces by default in the Linux path. In the Windows path however there are spaces and they are not escaped before passed in to wsl. This can be fixed by better scripts from the maintainer of the softwares however if they are not specifically supporting wsl they usually see no need. It does seem like something that is not hard to fix when appending the windows path to wsl. There are a lot of factors but the easiest solution seems to me to be at the root of the problem, a path with spaces being appended to the default Linux path.

I bet that script from @humandevs could be integrated into wsl to make life easier for people instead of people having to find random ideas and solutions. :)

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

No branches or pull requests