Terminal: Support linux paths for `startingDirectory` of WSL distros

Created on 8 May 2019  路  38Comments  路  Source: microsoft/terminal

  • Your Windows build number: Microsoft Windows [Version 10.0.18362.86]

  • What you're doing and what's happening:
    Setting the starting directory for wsl in profiles.json doesn't make it point to the right directory.

  • Take the config below :-
{
        "startingDirectory": "C:/Users/joshj/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu16.04onWindows_79rhkp1fndgsc/LocalState/rootfs/home/tackyunicorn",
        "guid": "{a2785f8e-72c9-4550-b406-697388644902}",
        "name": "Ubuntu",
        "colorscheme": "MonokaiVivid",
        "historySize": 9001,
        "snapOnInput": true,
        "cursorColor": "#FFFFFF",
        "cursorHeight": 25,
        "cursorShape": "vintage",
        "commandline": "wsl.exe",
        "fontFace": "Inconsolata for Powerline",
        "fontSize": 10,
        "acrylicOpacity": 1,
        "useAcrylic": true,
        "closeOnExit": true,
        "padding": "10, 10, 10, 10",
        "icon": "ms-appdata:///roaming/ubuntu.png"
}

This results in wsl starting up in the root folder
issue1

  1. In this config :-
{
        "startingDirectory": "/home/tackyunicorn",
        "guid": "{a2785f8e-72c9-4550-b406-697388644902}",
        "name": "Ubuntu",
        "colorscheme": "MonokaiVivid",
        "historySize": 9001,
        "snapOnInput": true,
        "cursorColor": "#FFFFFF",
        "cursorHeight": 25,
        "cursorShape": "vintage",
        "commandline": "wsl.exe",
        "fontFace": "Inconsolata for Powerline",
        "fontSize": 10,
        "acrylicOpacity": 1,
        "useAcrylic": true,
        "closeOnExit": true,
        "padding": "10, 10, 10, 10",
        "icon": "ms-appdata:///roaming/ubuntu.png"
}

This results in wsl starting up in the Windows user folder
issue2

  • What's wrong / what should be happening instead:
    The startingDirectory profile setting is not being applied correctly on a wsl profile
Area-Settings Area-TerminalControl Help Wanted Issue-Feature Product-Terminal

Most helpful comment

it looks like it expects Windows paths but passing the WSL fs paths under AppData directly doesn't work. Using the new UNC path works for me:
"startingDirectory":"//wsl$/Ubuntu/home/cem/src" -> ~/src

So do regular Windows locations:
"startingDirectory":"C:/wd" -> /mnt/c/wd

All 38 comments

it looks like it expects Windows paths but passing the WSL fs paths under AppData directly doesn't work. Using the new UNC path works for me:
"startingDirectory":"//wsl$/Ubuntu/home/cem/src" -> ~/src

So do regular Windows locations:
"startingDirectory":"C:/wd" -> /mnt/c/wd

Hmm. We should maybe add a setting to suggest that the startingDirectory is a wsl path. Right now we're just manually setting the working directory of the client app we're starting, so we'd have to specifically know that the profile was trying to be wsl, and adjust our logic for passing in a working directory.

one easy hack would be inside ~/.bashrc file of wsl, add the line
cd ~

Problem solved.

one easy hack would be inside ~/.bashrc file of wsl, add the line
cd ~

Problem solved.

This is also a great way to get weird and unexpected behaviour in everything else that uses bash on your machine. :smile:

This method is a little bit of work. We could parse out the distribution name from the command in the profile so that wsl.exe -d Ubuntu gives us "Ubuntu", and then pass that to Profile::EvaluateStartingDirectory. Then, if it fails, before setting it to %userprofile% we can evaluate \\WSL$\[distributionName][directory] and see if that is a valid directory path.

Or we could make the method a non-static method and draw the value from _commandline.

EDIT: I realize now that this won't work. The P9 server will likely not have been started, and if it had been, won't have all of the distros loaded as available shares under the UNC path.

@sandeepkv93 - That hack isn't the best. When working in say VS Code and you want your WSL terminal to open inside your project directory, this will prevent that. Definitely going to cause other problems too

My workaround

"commandline" : "wsl.exe ~  -d WLinux"

I just edited my /etc/passwd after creating a new user to be my Windows' user's home directory /c/Users/MikeLloyd and set startingDirectory to ~ in my profile.json. It works for me as expected on 18965.1005.

My workaround

"commandline" : "wsl.exe ~"

What if I want to set the default directory to a directory in C:?
I tried "commandline" : "wsl.exe /mnt/c/Programming -d Ubuntu-18.04" and other directories, with or without /mnt/c/, with or without singlequotes and no luck.

@LeoLozes The second post in this thread shows how to do exactly what you want.

in my case, it works this way
"startingDirectory": "//wsl$/Ubuntu-18.04/home/username"

Both the wsl.exe ~ -d <distro> and //wsl$/home/<username> workarounds worked for me, but it would be nice if the startingDirectory were interpreted by WSL rather than Windows. E.g. "startingDirectory": "$HOME/projects" should just work.

It will be nice if Windows Terminal could convert the path. I followed #1060 expecting to start my wsl quickly, but it doesn't work, because Windows Explorer always pass the original path to Terminal.

I fix this issue with

"startingDirectory": "//wsl$/Ubuntu-18.04/home/myname"

in Microsoft Windows [Version 10.0.18363.592], ver 0.8.100910.0

My way to always start WSL on $HOME:
Create the link of wt.exe,
C:\Users\YOURNAME\AppData\Local\Microsoft\WindowsApps\wt.exe wsl.exe ~ -d Ubuntu
and just pin it on my start menu, and keep "startingDirectory" in profile as null

If you don't pass the "starting directory" to "wsl.exe", it will always start from your current directory.

My solution is to create a new profile with starting command line wsl.exe ~. Latest version on MS store.

{
            "acrylicOpacity" : 1,
            "closeOnExit" : true,
            "colorScheme" : "Campbell",
            "commandline" : "wsl.exe ~",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "bar",
            "fontFace" : "Consolas",
            "fontSize" : 12,
            "guid" : "{*********************************}",
            "historySize" : 9001,
            "icon" : "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png",
            "name" : "Ubuntu home",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : "%USERPROFILE%",
            "useAcrylic" : true
},

Easy fix
"startingDirectory": "//wsl$/Ubuntu/home/",

@troy-mac that will literally start in the "home" directory, not _your_ home directory. Please see above answers for countless working solutions (example)

I use Ubuntu and my workaround is:
"commandline" : "ubuntu.exe"

Easy fix
"startingDirectory": "//wsl$/Ubuntu/home/",

I don't even know why they give 馃憥馃徎

Thanks I figured it out... next thing that needs resolving is the /etc/resolv.conf file getting overwritten. Tried a few work arounds non work

How do I start WSL at a specific directory?

For instance, If I want to start at ~/dev what do I write in the commandline property?

I tried "commandline": "wsl.exe ~/dev", but got an error like below:

/bin/bash: /home/myname/dev: Is a directory

[process exited with code 126]

Edit:
nvm. Figure it out by writing

"startingDirectory":"//wsl$/Ubuntu/home/myname/dev"

With the latest release a couple of days ago (profile.json renamed to settings.json), startingDirectory now doesn't work at all. Not one single variation in this thread works at all for me, is anyone else seeing the same?

Setting startingDirectory worked for me, but it seems that you have to place it in the profile you're using. So for example, Ubuntu:

            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": false,
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl",
        "startingDirectory":"//wsl$/Ubuntu/home/me/dev"
            },

I have 4 wsl distros installed, Ubuntu, Debian, Kali-Linux and CentOS. When I use the setting "startingDirectory": "\\\\wsl$\\<distroname>\\home\\jakob" For the most part Debian respect the starting directory but not always, the others do not. However, when I open a second tab with one the other 3 distos they always open in ~ It seems that using "startingDirectory": "//wsl$//<distroname>/home/jakob" makes no difference but using \\ is more reliable

Edit: I am running Insider preview build 19619 and wsl2. It seems that when the VMs is up and running they respect the startingDirectory setting. If the VMs are stopped and has to start when opening a tab they do not.

Easy fix
"startingDirectory": "//wsl$/Ubuntu/home/",

I don't even know why they give 馃憥馃徎

It should be /home/username not /home

Just to repeat something that was already said, this is how you set your starting directory for a given WSL instance:

    "profiles": [
        {
            "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
            "hidden": false,
            "name": "Ubuntu",
            "source": "Windows.Terminal.Wsl",
            "fontFace": "Cascadia Code",
            "fontSize": 12,

            // THIS                 vvvvvvvvvvvv
            "commandline": "wsl.exe ~ -d Ubuntu"
        }
    ],

Replace "Ubuntu" with the name of your distro.

I have 4 wsl distros installed, Ubuntu, Debian, Kali-Linux and CentOS. When I use the setting "startingDirectory": "\\\\wsl$\\<distroname>\\home\\jakob" For the most part Debian respect the starting directory but not always, the others do not. However, when I open a second tab with one the other 3 distos they always open in ~ It seems that using "startingDirectory": "//wsl$//<distroname>/home/jakob" makes no difference but using \\ is more reliable

Edit: I am running Insider preview build 19619 and wsl2. It seems that when the VMs is up and running they respect the startingDirectory setting. If the VMs are stopped and has to start when opening a tab they do not.

This is because //wsl$/ folder is not initialized until a wsl instance is started. So the first instance wont start in ~, but the others will start in ~ because the first instance initialized the //wsl$/ folder

@troy-mac that will literally start in the "home" directory, not _your_ home directory. Please see above answers for countless working solutions (example)

@antoineco I am using Ubuntu (not sure if that matters) but It does put me in _my_ home directory /home/troymac so for me I get the results I was looking for...

@troy-mac I just tried on Ubuntu, and it doesn't. You probably have another directive, either in your settings file, or your bashrc file, but this alone does _not_ put you in your home directory.

Anyway, my main point was that we should keep this thread free of erroneous information. A working solution was proposed, let's encourage users to use that instead of sharing inaccurate alternative tricks.

For me (perhaps because of zsh), workaround "commandline": "wsl.exe /mnt/d/workspace -d Debian" doesn't work:

zsh:1: permission denied: /mnt/d/workspace

[process exited with code 126]

but "startingDirectory": "D:/workspace" without any "commandline" works just fine, as was suggested in the very beginning.

There no world in which specifying the directory by doing wsl /directory will work. That will _always_ (not just when using zsh!) result in the shell attempting to _execute the directory_ and then exit. That's never what you want.

any update on this?

@maximgeerinck Nope. We'll make sure to update this thread when there is. In the meantime, might I recommend the Subscribe button?
image
That way you'll be notified of any updates to this thread, without needlessly pinging everyone on this thread 鈽猴笍

Hi all I know I got a few thumbs down for this suggestion that I posted earlier, but I just installed WSL and Ubuntu-20.04 on a new system and all I did was add the below lines to my settings.json file and it 100% worked for dropping me into my home directory. I am not saying it will work for everyone as I am not sure the thumbs down users actually tested this or not, but it has worked for me on 2 different systems both with Ubuntu-20.04 and 18.04. I think it is the easiest thing to try first and if it does not work for you then try a couple other suggestions on this thread.

"startingDirectory": "//wsl$/Ubuntu-20.04/home/troy-mac"
"startingDirectory": "//wsl$/Ubuntu-18.04/home/troy-mac"

or use @antoineco suggestion which may work all the time...
"commandline": "wsl.exe ~ -d Ubuntu"

@troy-mac I'm speaking for myself here, but the thumb down was a way to say _"Warning, reader. The solution suggested in this message doesn't work for everyone, please refer to the accepted solution instead"_. It wasn't an attempt to dismiss you.

Like you said, "commandline": "wsl.exe ~ -d <distro_name>" (https://github.com/microsoft/terminal/issues/592#issuecomment-504947429) works in pretty much any case, so let's make sure this solution is visible to people who go through this issue, instead of trying to come up with new, approximate solutions.

When installing Ubuntu, can use

"startingDirectory":"\\\\wsl$\\[DISTRONAME]\\home\\[USERNAME]"

If not certain what [DISTRONAME] is, do:

  1. Win+R
  2. \\wsl$
  3. Hit enter

Now you will get the fileshare in an explorer. Fx

image

Simply follow the path to where you want to start from when opening a new tab in Windows Terminal. Remember:

  1. four back slashes at the start and
  2. two back slashes in between the folder names

Like:
image

when construcing the startingDirectory. Restart Windows Terminal, and you are good to go.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dev-logan picture dev-logan  路  3Comments

Wid-Mimosa picture Wid-Mimosa  路  3Comments

TayYuanGeng picture TayYuanGeng  路  3Comments

mrmlnc picture mrmlnc  路  3Comments

zadjii-msft picture zadjii-msft  路  3Comments