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

Automatically convert paths dropped on WSL instances #11625

Merged
6 commits merged into from
Nov 10, 2021
Merged

Automatically convert paths dropped on WSL instances #11625

6 commits merged into from
Nov 10, 2021

Conversation

petrsnm
Copy link
Contributor

@petrsnm petrsnm commented Oct 27, 2021

Drag and drop does not work for WSL because paths are pasted as windows
paths having incorrect path separator and path root. This PR adds code
to correct the path in TerminalControl before pasting to WSL terminals.

One problem with this approach is that it assumes the default WSL
automount root of "/mnt". It would be possible to add a setting like
"WslDragAndDropMountRoot"... but I decided it if someone wants to change
automount location it would be simple enough just to create the "/mnt"
symlink in WSL.

Validation

Couldn't find an obvious place to add a test. Manually tested
cut-n-paste from following paths:

  • "c:"
  • "c:\subdir"
  • "c:\subdir\subdir"
  • "\wsl.localhost<distro>"
  • \wsl.localhost<distro>\subdir"

Closes #331

@ghost ghost added Area-Interaction Interacting with the vintage console window (as opposed to driving via API or hooks) Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Conhost For issues in the Console codebase labels Oct 27, 2021
@github-actions
Copy link

github-actions bot commented Oct 27, 2021

@check-spelling-bot Report

Unrecognized words, please review:

  • mnt
Previously acknowledged words that are now absent carlos dpg sid SPACEBAR Unregister Urxvt vcvarsall xIcon yIcon zamora
To accept these unrecognized words as correct (and remove the previously acknowledged and now absent words), run the following commands

... in a clone of the git@github.com:petrsnm/terminal.git repository
on the FixDragDropWslPath branch:

update_files() {
perl -e '
my @expect_files=qw('".github/actions/spelling/expect/alphabet.txt
.github/actions/spelling/expect/expect.txt
.github/actions/spelling/expect/web.txt"');
@ARGV=@expect_files;
my @stale=qw('"$patch_remove"');
my $re=join "|", @stale;
my $suffix=".".time();
my $previous="";
sub maybe_unlink { unlink($_[0]) if $_[0]; }
while (<>) {
if ($ARGV ne $old_argv) { maybe_unlink($previous); $previous="$ARGV$suffix"; rename($ARGV, $previous); open(ARGV_OUT, ">$ARGV"); select(ARGV_OUT); $old_argv = $ARGV; }
next if /^(?:$re)(?:(?:\r|\n)*$| .*)/; print;
}; maybe_unlink($previous);'
perl -e '
my $new_expect_file=".github/actions/spelling/expect/fe26a6efc0d7fdadfa8aebea7861ee43ab0043a3.txt";
use File::Path qw(make_path);
use File::Basename qw(dirname);
make_path (dirname($new_expect_file));
open FILE, q{<}, $new_expect_file; chomp(my @words = <FILE>); close FILE;
my @add=qw('"$patch_add"');
my %items; @items{@words} = @words x (1); @items{@add} = @add x (1);
@words = sort {lc($a)."-".$a cmp lc($b)."-".$b} keys %items;
open FILE, q{>}, $new_expect_file; for my $word (@words) { print FILE "$word\n" if $word =~ /\w/; };
close FILE;
system("git", "add", $new_expect_file);
'
}

comment_json=$(mktemp)
curl -L -s -S \
  --header "Content-Type: application/json" \
  "https://api.github.com/repos/microsoft/terminal/issues/comments/952543614" > "$comment_json"
comment_body=$(mktemp)
jq -r .body < "$comment_json" > $comment_body
rm $comment_json

patch_remove=$(perl -ne 'next unless s{^</summary>(.*)</details>$}{$1}; print' < "$comment_body")
  

patch_add=$(perl -e '$/=undef;
$_=<>;
s{<details>.*}{}s;
s{^#.*}{};
s{\n##.*}{};
s{(?:^|\n)\s*\*}{}g;
s{\s+}{ }g;
print' < "$comment_body")
  
update_files
rm $comment_body
git add -u
✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. You can copy the contents of each perl command excluding the outer ' marks and dropping any '"/"' quotation mark pairs into a file and then run perl file.pl from the root of the repository to run the code. Alternatively, you can manually insert the items...

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spelling/allow/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spelling/allow/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spelling/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spelling/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

🗜️ If you see a bunch of garbage

If it relates to a ...

well-formed pattern

See if there's a pattern that would match it.

If not, try writing one and adding it to a patterns/{file}.txt.

Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

Note that patterns can't match multiline strings.

binary-ish string

Please add a file path to the excludes.txt file instead of just accepting the garbage.

File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

@zadjii-msft
Copy link
Member

Wow, this is elegant. It informs the control as to the source of the profile, and then the control will convert the path if it already knows it's a WSL profile. That's pretty neat.

It obviously won't work for the "run cmd, then manually spawn wsl", but I'm totally okay with that. There's no good way to detect that scenario, so it's not worth using heuristics (read: guessing) to try and reverse engineer that scenario.

We had a similar idea in mind for the final resolution to #3158, so I'm cool with this.

zadjii-msft
zadjii-msft previously approved these changes Oct 27, 2021
Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with paths with spaces in them too? e.x. C:\Program Files (x86)

// Remove \\wsl.localhost from the string (leaving the raw Linux filesystem path)
else if (fullPath.find(L"//wsl.localhost") != std::string::npos)
{
fullPath.erase(0, fullPath.find('/', fullPath.find('/', fullPath.find('/', fullPath.find('/') + 1) + 1) + 1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uh, wat

If we're looking for the literal //wsl.localhost string, wouldn't it be easier to just hardcode erase(0, 15) (+/-1)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh the path is \\wsl.localhost\Ubuntu-18.04\foo\bar, got it, I see now.

Co-authored-by: Leonard Hecker <leonard@hecker.io>
@github-actions
Copy link

github-actions bot commented Oct 27, 2021

@check-spelling-bot Report

Unrecognized words, please review:

  • mnt
Previously acknowledged words that are now absent carlos dpg sid SPACEBAR Unregister Urxvt vcvarsall xIcon yIcon zamora
To accept these unrecognized words as correct (and remove the previously acknowledged and now absent words), run the following commands

... in a clone of the git@github.com:petrsnm/terminal.git repository
on the FixDragDropWslPath branch:

update_files() {
perl -e '
my @expect_files=qw('".github/actions/spelling/expect/alphabet.txt
.github/actions/spelling/expect/expect.txt
.github/actions/spelling/expect/web.txt"');
@ARGV=@expect_files;
my @stale=qw('"$patch_remove"');
my $re=join "|", @stale;
my $suffix=".".time();
my $previous="";
sub maybe_unlink { unlink($_[0]) if $_[0]; }
while (<>) {
if ($ARGV ne $old_argv) { maybe_unlink($previous); $previous="$ARGV$suffix"; rename($ARGV, $previous); open(ARGV_OUT, ">$ARGV"); select(ARGV_OUT); $old_argv = $ARGV; }
next if /^(?:$re)(?:(?:\r|\n)*$| .*)/; print;
}; maybe_unlink($previous);'
perl -e '
my $new_expect_file=".github/actions/spelling/expect/fe26a6efc0d7fdadfa8aebea7861ee43ab0043a3.txt";
use File::Path qw(make_path);
use File::Basename qw(dirname);
make_path (dirname($new_expect_file));
open FILE, q{<}, $new_expect_file; chomp(my @words = <FILE>); close FILE;
my @add=qw('"$patch_add"');
my %items; @items{@words} = @words x (1); @items{@add} = @add x (1);
@words = sort {lc($a)."-".$a cmp lc($b)."-".$b} keys %items;
open FILE, q{>}, $new_expect_file; for my $word (@words) { print FILE "$word\n" if $word =~ /\w/; };
close FILE;
system("git", "add", $new_expect_file);
'
}

comment_json=$(mktemp)
curl -L -s -S \
  --header "Content-Type: application/json" \
  "https://api.github.com/repos/microsoft/terminal/issues/comments/953084799" > "$comment_json"
comment_body=$(mktemp)
jq -r .body < "$comment_json" > $comment_body
rm $comment_json

patch_remove=$(perl -ne 'next unless s{^</summary>(.*)</details>$}{$1}; print' < "$comment_body")
  

patch_add=$(perl -e '$/=undef;
$_=<>;
s{<details>.*}{}s;
s{^#.*}{};
s{\n##.*}{};
s{(?:^|\n)\s*\*}{}g;
s{\s+}{ }g;
print' < "$comment_body")
  
update_files
rm $comment_body
git add -u
✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. You can copy the contents of each perl command excluding the outer ' marks and dropping any '"/"' quotation mark pairs into a file and then run perl file.pl from the root of the repository to run the code. Alternatively, you can manually insert the items...

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spelling/allow/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spelling/allow/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spelling/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spelling/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

🗜️ If you see a bunch of garbage

If it relates to a ...

well-formed pattern

See if there's a pattern that would match it.

If not, try writing one and adding it to a patterns/{file}.txt.

Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

Note that patterns can't match multiline strings.

binary-ish string

Please add a file path to the excludes.txt file instead of just accepting the garbage.

File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

// Fix path for WSL
if (_settings.ProfileSource() == L"Windows.Terminal.Wsl")
{
static constexpr std::wstring_view wslPathPrefix{L"//wsl.localhost/"};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't work on windows 10 because the WSL path prefix is //wsl$/ there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case we can just run the prefix check twice and pick the std::wstring_view that matched. 🙂

@metathinker
Copy link
Contributor

(Sorry for the surprise comment)

What might happen if you have multiple WSL distributions and you copy and paste in a \\wsl.localhost path from a different distribution from the one in the Terminal tab?

  • To me, it looks like we don't check for this case and so we generate a path that isn't valid in the Terminal tab's distro.
  • Even if that's right, fixing the problem isn't critical - more of a nice-to-have extra.
  • Could we pull the distro name from the profile, reformat it the same way that the \\wsl.localhost path does (if any), then compare against that?
    • Unfortunately, I don't think we can do this because I don't see a place for the WSL distro name in the Profile record, but I might have missed something.

@petrsnm
Copy link
Contributor Author

petrsnm commented Oct 28, 2021

(Sorry for the surprise comment)

What might happen if you have multiple WSL distributions and you copy and paste in a \wsl.localhost path from a different distribution from the one in the Terminal tab?

  • To me, it looks like we don't check for this case and so we generate a path that isn't valid in the Terminal tab's distro.

  • Even if that's right, fixing the problem isn't critical - more of a nice-to-have extra.

  • Could we pull the distro name from the profile, reformat it the same way that the \wsl.localhost path does (if any), then compare against that?

    • Unfortunately, I don't think we can do this because I don't see a place for the WSL distro name in the Profile record, but I might have missed something.

This isn't a copy/paste fix. Only works for drag and drop. But yes, the drag and drop from \wsl.localhost<distro>\ only works if you're dragging into a terminal of the same distro. If you drag it to a terminal with a different distro, then the path may not exist. Currently (without this PR), none of the paths that you drag and drop will exist in WSL (due to slashes and rooting).

@petrsnm petrsnm closed this Oct 28, 2021
@metathinker
Copy link
Contributor

Sorry, I meant to say drag and drop not copy and paste. Did you intend to close the PR?

@petrsnm petrsnm reopened this Oct 28, 2021
@zadjii-msft
Copy link
Member

@petrsnm you need to run a tools\runformat.cmd to get the CI to pass (but if you get the signoffs I might just push a commit to this branch that does it for you 😛)

@zadjii-msft zadjii-msft added the Needs-Second It's a PR that needs another sign-off label Oct 28, 2021
Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. This PR seems to make some unexpected file mode changes.

image

It may be necessary to re-add the files that changed from 644 to 755 using git add --chmod -x

I'm also blocking to do a more in-depth review; sorry!

@ghost ghost added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Oct 28, 2021
Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also this weird guy:

image

@petrsnm
Copy link
Contributor Author

petrsnm commented Oct 28, 2021

ugh... looks like I made a commit from a WSL2 console with automount options that fix up file permissions (umask=22, fmask=11). I'll fix the commits

@ghost ghost removed the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Oct 28, 2021
@petrsnm
Copy link
Contributor Author

petrsnm commented Oct 29, 2021

@DHowett, sorry about the fileMode mess. I was goofing around with WSL automount umask and fmask for another project and didn't notice the filemode change in the commit. Lesson learned. Rather than re-add the files I just committed
ca11715fea09bd0c71d6e9db1f132b2f45be1c0 to revert the file mode back to 644.

@DHowett
Copy link
Member

DHowett commented Nov 9, 2021

Thanks for working on this, @petrsnm! Sorry for the delay in my reviewing it.

I love the direction it's headed, and this is definitely a feature that people will be happy about.

I'm a little iffy on the design, however, for one reason: TerminalControl was originally architected to be a reusable component that another app developer could take a dependency on. Through that lens, giving it knowledge of "profiles" and "sources" and how to translate paths--when the app developer using it may not have even connected it to a local process and likely doesn't have profiles or sources--falls short of that need. TC already has a few layers of indirection to support this which are visible in how we handle pastes, among other places.

I accept that this design is not obvious if you're jumping into the code out of nowhere, and that we should for sure make sure our documentation mentions our guiding principles. It's also a bit of a pain for a contributor to have to wrap their head around. Because of that, I'm not going to block your pull request over it. If you're not comfortable making those changes, or if me asking is overstaying my "maintainer welcome", or if you feel like you've done enough: what is here is perfectly acceptable and something that we can build on later.

Let me know 😄

@petrsnm
Copy link
Contributor Author

petrsnm commented Nov 10, 2021

@DHowett, I saw that there was already pre-paste fixup code for quoting paths with spaces in TerminalControl._DragDropHandler, so I assumed this was the right place to do pre-paste fixups. It wasn't obvious to me that TerminalControl' was meant to be reusable. Incidentally, would quoting paths with spaces belong in ControlInteractivity` also?

It looks like it would not be that difficult to move the fix to ControlInteractivity but I'm currently buried in projects that I have to complete before Thanksgiving, and I don't think I'll have time to make a new pull request until December.

I'm currently running a dev copy of Terminal because this PR makes a huge productivity difference for me. Would it be possible to merge this PR , and then maybe I can come back and move the fix into ControlInteractivity when someone creates an issue that they can't reuse TerminalControl because they don't have "profiles" or "sources"?

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW we're cool merging this as-is. From an "architectural purity" standpoint, we should maybe move some logic around, but that's not something we'd totally expect an external contributor to do. The code is good, and the feature works, I can come back around later and move it 😋 Thanks again for doing this!

@DHowett DHowett dismissed their stale review November 10, 2021 21:14

addressed

@DHowett
Copy link
Member

DHowett commented Nov 10, 2021

Notes moved from PR body:

windows.terminal.drag.and.drop.for.wsl.mp4

@DHowett DHowett changed the title When drag-n-drop path to WSL, fix the path so it's usable. Closes #331 Automatically convert paths dropped on WSL instances Nov 10, 2021
@DHowett DHowett added the AutoMerge Marked for automatic merge by the bot when requirements are met label Nov 10, 2021
@ghost
Copy link

ghost commented Nov 10, 2021

Hello @DHowett!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@DHowett
Copy link
Member

DHowett commented Nov 10, 2021

Thanks so much for doing this! I'm writing up the overall architecture plan for another comment, but I wanted to make sure I said thanks foremost 😄

@ghost ghost merged commit d5974f4 into microsoft:main Nov 10, 2021
zadjii-msft added a commit that referenced this pull request Nov 11, 2021
@ghost
Copy link

ghost commented Feb 3, 2022

🎉Windows Terminal Preview v1.13.10336.0 has been released which incorporates this pull request.:tada:

Handy links:

@loic
Copy link

loic commented Apr 15, 2024

My Terminal has 2 Ubuntu profiles. (I installed the Ubuntu app from Windows Store).

This fix works well with the Ubuntu profile that is "hidden" by default (profile with a Tux icon and a command line of wsl.exe -d Ubuntu) but not with the default Ubuntu profile (profile with the Ubuntu logo icon and a command line of ubuntu.exe).

I haven't researched the concrete difference between wsl.exe -d Ubuntu and ubuntu.exe, but it would be nice if it worked with both, especially with the default profile. Anyone got that to work or knows what could be at play?

image

@zadjii-msft
Copy link
Member

I haven't researched the concrete difference between wsl.exe -d Ubuntu and ubuntu.exe, but it would be nice if it worked with both, especially with the default profile. Anyone got that to work or knows what could be at play?

Basically, the ubuntu.exe profile is one that Canonical ships as a "fragment extension", while the wsl.exe -d Ubuntu one is one that comes with the Terminal itself. Terminal only knows how to translate paths for our own WSL profiles, and can't be sure for the Canonical ones.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Interaction Interacting with the vintage console window (as opposed to driving via API or hooks) AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Needs-Second It's a PR that needs another sign-off Product-Conhost For issues in the Console codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Drag & Drop from File Explorer produces incorrect file paths with WSL
7 participants