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

Support linux paths for startingDirectory of WSL distros #592

Closed
tackyunicorn opened this issue May 8, 2019 · 50 comments · Fixed by #9223
Closed

Support linux paths for startingDirectory of WSL distros #592

tackyunicorn opened this issue May 8, 2019 · 50 comments · Fixed by #9223
Assignees
Labels
Area-Settings Issues related to settings and customizability, for console or terminal Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Help Wanted We encourage anyone to jump in on these. Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.

Comments

@tackyunicorn
Copy link

  • 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.

  1. 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
@caksoylar
Copy link

caksoylar commented May 8, 2019

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

@zadjii-msft
Copy link
Member

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.

@zadjii-msft zadjii-msft added the Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. label May 8, 2019
@zadjii-msft zadjii-msft added this to the Windows Terminal v1.0 milestone May 8, 2019
@zadjii-msft zadjii-msft added the Help Wanted We encourage anyone to jump in on these. label May 8, 2019
@ghost ghost added the Needs-Tag-Fix Doesn't match tag requirements label May 17, 2019
@miniksa miniksa added Product-Terminal The new Windows Terminal. Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. Area-Settings Issues related to settings and customizability, for console or terminal and removed Mass-Chaos labels May 17, 2019
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label May 17, 2019
@miniksa miniksa added Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) and removed Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. labels May 17, 2019
@sandeepkv93
Copy link

sandeepkv93 commented May 19, 2019

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

Problem solved.

@DHowett-MSFT
Copy link
Contributor

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. 😄

@JBanks
Copy link
Contributor

JBanks commented Jun 9, 2019

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.

@benc-uk
Copy link

benc-uk commented Jun 22, 2019

@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

@jkugiya
Copy link

jkugiya commented Jun 24, 2019

My workaround

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

@siennathesane
Copy link

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.

@nanjj
Copy link

nanjj commented Sep 2, 2019

My workaround

"commandline" : "wsl.exe ~"

@leolozes
Copy link

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.

@Herohtar
Copy link

Herohtar commented Nov 3, 2019

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

@anikishov
Copy link

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

@zadjii-msft

This comment has been minimized.

@troy-mac
Copy link

troy-mac commented Oct 2, 2020

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"

@antoineco
Copy link
Contributor

@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>" (#592 (comment)) 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.

@mslot
Copy link

mslot commented Oct 13, 2020

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.

@Ratismal
Copy link

I feel like I must be missing something obvious here. Everything I've read in this issue indicates that if you set your startingDirectory to //wsl$/ubuntu/whatever/path/you/want, it will start in that directory. But that's not the case for me? No matter what I set startingDirectory, it always always takes me to my home directory. This is not what I want.

I've tried making new profiles, and I've tried updating the existing ubuntu profile. I've tried forward slashes, back slashes, ~, everything has the same result. See:
image

Is it intentional that you're essentially locked into starting at the home directory, or am I missing something?

@zadjii-msft
Copy link
Member

You might need to use backslashes (instead of forward slashes), because it definitely works:

image

Is there anything else in your .bashrc that might be changing directories?

@mariusstrom
Copy link
Contributor

I feel like I must be missing something obvious here. Everything I've read in this issue indicates that if you set your startingDirectory to //wsl$/ubuntu/whatever/path/you/want, it will start in that directory. But that's not the case for me? No matter what I set startingDirectory, it always always takes me to my home directory. This is not what I want.

@Ratismal
What's the output of wsl.exe -l? Make sure the startingDirectory path matches the name of the WSL distro from wsl.exe -l exactly. If case sensitivity is required there, I'm guessing yours should be Ubuntu, and not ubuntu. Some distros include the LTS version in the distro name (like you can see in @zadjii-msft's post above with Ubuntu-18.04).

"startingDirectory": "//wsl$/Ubuntu/home/cat/proj"

I think it's case-sensitive to the distro name.

@mariusstrom
Copy link
Contributor

@zadjii-msft forward slashes unescaped work for me:

image

@Ratismal
Copy link

Ratismal commented Jan 22, 2021

@mariusstrom

What's the output of wsl.exe -l? Make sure the startingDirectory path matches the name of the WSL distro from wsl.exe -l exactly. If case sensitivity is required there, I'm guessing yours should be Ubuntu, and not ubuntu.

You were exactly right, thank you! I think there might be a bug somewhere in there, though? Did some experimenting.

  • //wsl$/ubuntu/path/that/does/exist - brings me to my home directory
  • //wsl$/ubuntu/path/that/doesnt/exist - brings me to my home directory in the C: drive
  • //wsl$/Ubuntu/path/that/does/exist - brings me to that directory
  • //wsl$/Ubuntu/path/that/doesnt/exist - brings me to my home directory in the C: drive

I would expect that case sensitivity would cause /ubuntu/ to always bring me back to the home directory on the C: drive, since its an invalid path. But that doesn't seem to be the case. Is that... intended behaviour?

@zadjii-msft
Copy link
Member

Frankly, I have no idea how the WSL filesystem driver works. The best place to ask would be over at https://github.com/microsoft/WSL

@stephenmartindale
Copy link

stephenmartindale commented Feb 5, 2021

My workaround

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

This seems to be the commonly accepted solution but it has one fatal flaw: if you use wsl.exe ~ as the commandline, then the --startingDirectory (or -d) parameter to wt.exe ceases to function, properly.

I use the Windows Subsystem for Linux for spinning up sand-boxes for developer experiments, using wsl.exe --export and --import to load up and pack away these sand-boxes as required. At any given time, my default WSL distribution is the one that contains the tool-chain that I'm currently using for my work. For this reason, I've created a WT profile that always spawns whatever the default WSL distribution happens to be:

            {
                "guid": "{75155ba1-aebb-4196-af68-117807257cff}",
                "commandline": "%systemroot%\\System32\\wsl.exe",
                "name": "Windows Subsystem for Linux",
                "icon": "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png",
            },

This works great because I can use commands like wt.exe --profile "{75155ba1-aebb-4196-af68-117807257cff}" --startingDirectory "D:\Project1". However, as soon as I append ~, then ~ overrides any and all paths passed to wt via the --startingDirectory command-line parameter.

Obviously, not appending ~ means that starting this profile without --startingDirectory, from within the Terminal's U.I. for example, opens up the new shell in System32 which is sub-optimal.

@dark-swordsman
Copy link

dark-swordsman commented Apr 19, 2021

Using the new UNC path as suggested by caksoylar worked great for me, but just a note to others that, for example, if you have "Ubuntu", it may not just be "Ubuntu". Mine was actually Ubuntu-20.04.

I was able to confirm this by opening up VSCode and using "reveal in explorer" on a file, to which it then opened \\wsl$\Ubuntu-20.04\....

You can also just do \\wsl$ in the address bar and see it there, I had just forgotten that Windows paths use \, not /, lol.

image

@LeaveTheCapital
Copy link

for me since version 1.9.1445.0 (preview) this no longer worked

{
"commandline": "wsl.exe ~",
"name": "Ubuntu-20.04",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "/home/henry/source"
}

however this works
{
"name": "Ubuntu-20.04",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "//wsl$/Ubuntu-20.04/home/henry/source"
}

@saadanerdetbare
Copy link

This works for me: cd into the directory where I want to start and type wslpath -w .
Example output: \\wsl.localhost\Ubuntu-20.04\home\bob
Put that in the "startingDirectory": and escape any \ with another \

@ednl
Copy link

ednl commented Jul 19, 2021

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

Is this still true?? I seems to be, it is for me. So is there no way to always (even the first time) start up a WSL instance in the Linux home directory? That seems really strange, especially in WSL2 where the division between file systems is greater because of performance differences.

@ghost ghost added the In-PR This issue has a related PR label Aug 2, 2021
@DHowett DHowett self-assigned this Aug 2, 2021
@ghost ghost closed this as completed in #9223 Aug 2, 2021
@ghost ghost added Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. and removed In-PR This issue has a related PR labels Aug 2, 2021
ghost pushed a commit that referenced this issue Aug 2, 2021
This commit introduces a hack to ConptyConnection for launching WSL.
When we detect that WSL is being launched (either "wsl" or "wsl.exe",
unqialified or _specifically_ from the current OS's System32 directory),
we will promote the startingDirectory specified at launch time into a
commandline argument.

Why do we want to switch to `--cd`?
With the current design of ConptyConnection and WSL, there are some
significant limitations:
* `startingDirectory` cannot be a WSL path, which forces users to
  use weird tricks such as setting the starting directory to
  `\\wsl$\Distro\home\user`.
* WSL occasionally fails to launch in time to handle a `\\wsl$` path,
  which makes us spawn in a strange location (or no location at all).

(This fix will only address the second one until a WSL update is
released that adds support for `--cd $LINUX_PATH`.)

We will not do the promotion if any of the following are true:
* the commandline contains `--cd` already
* the commandline contains a bare `~`
   * This was a commonly-used workaround that forced wsl to start in the
     user's home directory. It conflicts with --cd.
* wsl is not spelled properly (`WSL` and `WSL.EXE` are unacceptable)
* an absolute path to wsl outside the system32 directory is provided

We chose the do this trick in the connection layer, the latest possible
point, because it captures the most use cases.

We could have done it earlier, but the options were quite limiting.
They are:

* Generate WSL profiles with startingDirectory set to the home folder
   * We can't do this because we do not know the user's home folder
     path.
* Generate WSL profiles with `--cd` in them.
   * This only works for unmodified profiles.
   * This only works for generated profiles.
   * Users cannot override the commandline without breaking it.
   * Users cannot specify a startingDirectory (!) since the one on the
     commandline wins.
* Set a flag on generated WSL profiles to request this trick
   * This only works for generated profiles. Users who create their own
     WSL profiles couldn't set startingDirectory and have it work the
     same.

Patching the commandline, hacky though it may be, seemed to be the most
compatible option. Eventually, we can even support `wt -d ~ wsl`!

## Validation Steps Performed

Manual validation for the following cases:

```c++
// MUST MANGLE
auto a01 = _tryMangleStartingDirectoryForWSL(LR"(wsl)", L"SENTINEL");
auto a02 = _tryMangleStartingDirectoryForWSL(LR"(wsl -d X)", L"SENTINEL");
auto a03 = _tryMangleStartingDirectoryForWSL(LR"(wsl -d X ~/bin/sh)", L"SENTINEL");
auto a04 = _tryMangleStartingDirectoryForWSL(LR"(wsl.exe)", L"SENTINEL");
auto a05 = _tryMangleStartingDirectoryForWSL(LR"(wsl.exe -d X)", L"SENTINEL");
auto a06 = _tryMangleStartingDirectoryForWSL(LR"(wsl.exe -d X ~/bin/sh)", L"SENTINEL");
auto a07 = _tryMangleStartingDirectoryForWSL(LR"("wsl")", L"SENTINEL");
auto a08 = _tryMangleStartingDirectoryForWSL(LR"("wsl.exe")", L"SENTINEL");
auto a09 = _tryMangleStartingDirectoryForWSL(LR"("wsl" -d X)", L"SENTINEL");
auto a10 = _tryMangleStartingDirectoryForWSL(LR"("wsl.exe" -d X)", L"SENTINEL");
auto a11 = _tryMangleStartingDirectoryForWSL(LR"("C:\Windows\system32\wsl.exe" -d X)", L"SENTINEL");
auto a12 = _tryMangleStartingDirectoryForWSL(LR"("C:\windows\system32\wsl" -d X)", L"SENTINEL");
auto a13 = _tryMangleStartingDirectoryForWSL(LR"(wsl ~/bin)", L"SENTINEL");

// MUST NOT MANGLE
auto a14 = _tryMangleStartingDirectoryForWSL(LR"("C:\wsl.exe" -d X)", L"SENTINEL");
auto a15 = _tryMangleStartingDirectoryForWSL(LR"(C:\wsl.exe)", L"SENTINEL");
auto a16 = _tryMangleStartingDirectoryForWSL(LR"(wsl --cd C:\)", L"SENTINEL");
auto a17 = _tryMangleStartingDirectoryForWSL(LR"(wsl ~)", L"SENTINEL");
auto a18 = _tryMangleStartingDirectoryForWSL(LR"(wsl ~ -d Ubuntu)", L"SENTINEL");
```

We don't have anywhere to put TerminalConnection unit tests :|

Closes #592.
@ghost
Copy link

ghost commented Aug 31, 2021

🎉This issue was addressed in #9223, which has now been successfully released as Windows Terminal Preview v1.11.2421.0.:tada:

Handy links:

@Seefer
Copy link

Seefer commented Sep 17, 2021

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

Problem solved.

I've recently discovered that having cd ~ in the .bashrc or .zshrc file of a Linux distro breaks VS Code's integrated terminal behaviour. With cd ~ in one of these shell script files, if you are in a project folder in VS Code and open the integrated terminal, the terminal directory does not point to the project directory. It always opens at ~ .

Is there some other way via WSL to specify the working directory for the distro instead of it always pointing at /mnt/c/Users/<username>?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Settings Issues related to settings and customizability, for console or terminal Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Help Wanted We encourage anyone to jump in on these. Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.
Projects
None yet
Development

Successfully merging a pull request may close this issue.