terminal icon indicating copy to clipboard operation
terminal copied to clipboard

Feature Request: Path translation in WSL and Git Bash

Open Forinil opened this issue 5 years ago • 29 comments

Summary of the new feature/enhancement

It would be useful if the path pasted from clipboard to a shell was translated to a format that said shell understands if it differs form native windows format.

For example, if user pastes C:\Windows\System\ to PowerShell or cmd it remains unchanged, but in WSL it changes to /mnt/c/Windows/System and in Git Bash to /c/Windows/System.

Forinil avatar Jul 02 '19 09:07 Forinil

I wonder if this would be implementable in a generic way such as by having two settings like this for Git bash:

"pathConversionForwardSlashes": true,
"pathConversionRemovePrefix": "C:\",
"pathConversionReplacePrefix": "/mnt/c/"

eberkund avatar Jul 02 '19 15:07 eberkund

The prefixes would have to be regular expressions to acount for the fact that C is not the only possible drive letter.

Maybe something like:

"pathConversionExpression": "|([A-Za-z])\(.*)|/mnt/\L$1/$2|"

where $1, $2, ... are capture groups and \L is a lowercase operator (like in Notepad++).

Forinil avatar Jul 02 '19 16:07 Forinil

Yeah you're right, I guess full blown regex would be a better solution at that point.

eberkund avatar Jul 02 '19 16:07 eberkund

A better solution IMO would be to use the path conversion commands command built into WSL and Cygwin/MSYS2/Git Bash/etc. to convert it for us. So for WSL you would have:

"pathConversionCmd": "wsl wslpath -a $1"

And for Git Bash you could have :

"pathConversionCmd": "\"C:\\Program Files\\Git\\usr\\bin\\cygpath.exe\" -a $1"

ToadKing avatar Dec 07 '19 01:12 ToadKing

Any traction on this issue? It seems small, but this is actually a major roadblock to me switching from cmder to Windows Terminal.

eleniums avatar May 22 '20 01:05 eleniums

A better solution IMO would be to use the path conversion commands command built into WSL and Cygwin/MSYS2/Git Bash/etc. to convert it for us. So for WSL you would have:

"pathConversionCmd": "wsl wslpath -a $1"

And for Git Bash you could have :

"pathConversionCmd": "\"C:\\Program Files\\Git\\usr\\bin\\cygpath.exe\" -a $1"

I was looking into implementing this, I really like the idea but also I started thinking; what about security issues? that config field will allow anyone to run any executable by hijacking the settings file (even with admin permissions if the terminal was opened that way). Should I do the regex instead? or detect it's wsl and support only that? another idea (which I don't like, but still throwing it out there): ability to add a wrapper: path --> wslpath "*path*" (note the addition of ` character) @zadjii-msft @DHowett-MSFT what do you guys think?

Daniel599 avatar May 17 '21 10:05 Daniel599

You know what, we've got a team sync later today, I'll bring this up. We had a plan for WSL distros that would just work like magic, but that would not work at all for mingw profiles. Let's see if we can come up with something.

I don't love the idea of putting an arbitrary exe into the settings file itself. It's definitely a possible security hole. We might be able to hack around this temporarily with

  • A. making this an experimental. setting, in case we come up with a better plan
  • B. letting this only work in unelevated windows. I know that's annoying, but prevents an escalation-of-privilege whereby an unelevated malicious.exe from changing your settings.json so that pathConversionCmd is set to malicious.exe instead

zadjii-msft avatar May 17 '21 11:05 zadjii-msft

@zadjii-msft any update in that matter? In the meanwhile I think I`ll try to implement #10073, as it seems to use working-dir to convert path so maybe those two issues are ortogonale

Daniel599 avatar May 22 '21 10:05 Daniel599

My apologies. A bunch of the team was out sick last week, so we ended up just not having the meeting 😬 Hopefully everyone's feeling better this week (and we're not to preoccupied with last-minute //Build fires)

zadjii-msft avatar May 24 '21 13:05 zadjii-msft

notes from team sync: someone recalled vaguely that ConEmu has a solution for this that doesn't involve launching another exe to resolve that path. Maybe we should take a look at what they do, and see if that makes sense to emulate.

zadjii-msft avatar May 24 '21 21:05 zadjii-msft

Here's the documentation of Shell working dir in ConEmu for reference: https://conemu.github.io/en/ShellWorkDir.html

I see ConEmuC.exe which is a native win32 application that seems to handle path resolution.

Also tips for @Daniel599: Dustin no longer uses the account DHowett-MSFT for this project. He's now @DHowett .

skyline75489 avatar Jul 20 '21 10:07 skyline75489

This is the only issue that blocks me to switch to Windows Terminal. Solution wise, I would suggest adding an option in the profile -> Advanced, the option can be called: "Convert Windows path to Linux path during paste"

lingyanmeng avatar Oct 27 '21 08:10 lingyanmeng

@lingyanmeng I would love to do that. How would we know to translate the path depending on WSL vs Cygwin vs msys?

I was under the impression that windows-side paths were in different places for those various different linux-like installs.

zadjii-msft avatar Oct 27 '21 11:10 zadjii-msft

Any update on that issue?

shinayser avatar Dec 14 '21 16:12 shinayser

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 ☺️

  • #11625 added support for translating drag/dropped WSL paths.
  • We'd still need to do something similar if the clipboard contained a path (this issue)
  • #3158 is for tracking "the shell told us it was in {WSL path}, which we can actually reverse engineer to {Windows path}"
  • There's still no good solution for cygwin/msys paths. That's not something we control, so we're not really planning on adding tighter integration with that. Throwing arbitrary exe's in the settings to have the Terminal execute seems generally like a bad idea.

zadjii-msft avatar Dec 14 '21 16:12 zadjii-msft

Do you guys mind increasing the priority of this issue? It would be nice to be able to smoothly paste a file path. 😄

MattBussing avatar Jul 07 '22 20:07 MattBussing

We certainly don't have the capacity to get to this ourselves any time soon, sorry 😕. If someone wants to help sort out the bits of this to enable this for WSL distros, I'd be more than capable to give pointers of where I'd start. The "use an exe to translate the path" seems a bit more... risqué, but the "I know the thing running in this terminal is this WSL so we can translate the path {like so}" seems pretty doable.

zadjii-msft avatar Jul 07 '22 21:07 zadjii-msft

Thank for for the quick reply! I don't plan on working on it at the moment.

MattBussing avatar Jul 09 '22 18:07 MattBussing

i'm pretty sure to have seen this at some point some time ago, i dragged a folder to terminal with ubuntu open on it, and it replaced the path to the corresponding /mnt/c path, but i can't see it working now 🤔

voltuer avatar Nov 30 '22 19:11 voltuer

@sebolio This only works for panes created for profiles with a Microsoft.Terminal.WSL source set. There's a set of "source": "CanonicalGroupLimited.Ubuntu* profiles floating around, where that won't work. That's vaguely tracked in #14233, #14003 and #12961

zadjii-msft avatar Nov 30 '22 19:11 zadjii-msft

We're in 2024 now, but Windows remains unable to automatically transform its own paths into Linux-style paths when pasting.

garretzou avatar Jan 01 '24 03:01 garretzou

#11625 detects whether the profile is WSL buy checking for "source": "Windows.Terminal.Wsl" in the configs (which isn’t exposed to the Settings UI because why should we?). This means manually-created WSL profiles, such as my Interactive Ruby (wsl irb), don’t get the same VIP treatment.

Please, either

  • Detect WSL from the start-up command (/\Awsl(.exe)?/) rather than that arcane setting
  • Make path translations manually customizable – users get to decide whether to translate or not.
    • The automatic profile from WSL installation will instead be created with this config set to translating Windows paths to Linux.

ParadoxV5 avatar Jan 30 '24 20:01 ParadoxV5

which isn’t exposed to the Settings UI because why should we?

Because that setting literally tracks where the profile came from. There is no meaningful change you can make to it (and if you do, the profile will get deleted. Because the thing you said it "came from" can't attest that it came from there. That's actually how we automatically hide profiles for WSL distributions that you uninstall!)

In general, detecting what a profile is running versus where a profile came from is difficult and annoying.

Even these proposed solutions don't work for the case where you run wsl from within PowerShell, or run ubuntu2204.exe which is a wrapper around the WSL API.

Yes, path translations should be manually customizable. That's why this issue is still open, after all! I'd rather have some progress than no progress.

DHowett avatar Jan 30 '24 21:01 DHowett

Even these proposed solutions don't work for the case where you run wsl from within PowerShell, or run ubuntu2204.exe which is a wrapper around the WSL API.

Is it possible for the terminal to know what (sub)process it is currently hosting?

ParadoxV5 avatar Jan 30 '24 21:01 ParadoxV5

Is it possible for the terminal to know what (sub)process it is currently hosting?

To an extent, yes :)

We have it on the books to track what we're hosting so that we can display better error messages and do profile matching. However, it'll break down for "opaque" remoting protocols like SSH and all of WSL (since processes on the Linux side don't present as processes on the Windows side) ☹️

Still, that's better than nothing!

DHowett avatar Jan 30 '24 22:01 DHowett

Wouldn't it be enough to just introduce a separate "unixPaths": "true" setting for it for most usecases?

MichalPetryka avatar Jan 30 '24 22:01 MichalPetryka

However, it'll break down for "opaque" remoting protocols like SSH and all of WSL (since processes on the Linux side don't present as processes on the Windows side) ☹️

Still, that's better than nothing!

At least enough for the terminal to know that it’s running something WSL! (Probably not enough to know that it’s hosting a Linux through SSH, though.)

ParadoxV5 avatar Jan 30 '24 23:01 ParadoxV5

We already use VT to let shells announce their CWD, why not use VT to let shells announce what they actually are?

driver1998 avatar Jan 31 '24 01:01 driver1998

Wouldn't it be enough to just introduce a separate "unixPaths": "true" setting for it for most usecases?

This does not work in my case. I use WSL and Cygwin, which have different prefixes:

WSL: /mnt Cygwin: /cygpath

martin-eder-zeiss avatar Jan 31 '24 06:01 martin-eder-zeiss