Coder Social home page Coder Social logo

mikebattista / powershell-wsl-interop Goto Github PK

View Code? Open in Web Editor NEW
428.0 428.0 15.0 86 KB

Integrate Linux commands into Windows with PowerShell and the Windows Subsystem for Linux (WSL).

Home Page: https://devblogs.microsoft.com/commandline/integrate-linux-commands-into-windows-with-powershell-and-the-windows-subsystem-for-linux/

License: MIT License

PowerShell 100.00%

powershell-wsl-interop's People

Contributors

kitingchris avatar mikebattista avatar paaland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

powershell-wsl-interop's Issues

\\wsl$\<distro_name>\<path> UNC path syntax is not converted

Describe the bug
\wsl$<distro_name><path> UNC path syntax is not converted to linux path
e.g. \wsl$\Ubuntu\etc\wsl.conf to /etc/wsl.conf
To Reproduce
Steps to reproduce the behavior:

  1. Start pwsh
  2. Enter imported command like vim '\\wsl$\Ubuntu\etc\wsl.conf'
  3. Command is not converted to /etc/wsl.conf

Expected behavior
Path conversion working.
e.g. convert \wsl$\Ubuntu\etc\wsl.conf to /etc/wsl.conf

Desktop (please complete the following information):

  • Windows 2004 (OS Build (19041.208)
  • Ubuntu 20.04 on Windows 10 x86_64 on 4.19.84-microsoft-standard
  • GNU bash, version 5.0.16(1)-release (x86_64-pc-linux-gnu)

Additional context
Good thing to note is that this still won't work across multiple wsl distros.
vim '\\wsl$\Ubuntu\etc\wsl.conf'
vim '\\wsl$\Debian\etc\wsl.conf'

-exec Parameter for "find" does not work properly

Not sure if this issue can be solved, anyway - thanks a ton for your great work on this 😄

Describe the bug

There are various bugs with the "-exec" parameter of the find command. Arguments to -exec do not work and result in errors.

To Reproduce

find . -exec echo blub \;
find . -exec echo {} \;

Expected behavior

The first command should output "blub" for every item found.
The second command should output the pathname of every item found.

Screenshots

Output of first command:

find: missing argument to `-exec'

Output of second command:

Test-Path:
Line |
  22 |              } elseif (Test-Path $args[$i] -ErrorAction Ignore) {
     |                                  ~~~~~~~~~
     | Cannot evaluate parameter 'Path' because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input.
find: missing argument to `-exec'

Desktop (please complete the following information):

  • Windows 11, WSL2

Error loading profile

When installing the module using

Install-Module WslInterop

adding

Import-WslCommand "awk", "emacs", "grep", "head", "less", "ls", "man", "sed", "seq", "ssh", "tail", "vim", "bash"
$WslDefaultParameterValues["grep"] = "-E"
$WslDefaultParameterValues["less"] = "-i"
$WslDefaultParameterValues["ls"] = "-AFh --group-directories-first"
$WslDefaultParameterValues["bash"] = "--login --init-file ~/.nix-profile/etc/profile.d/nix.sh"

and the reloading profile I get a bunch of:

Cannot index into a null array.
At ...\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:8 char:1

  • $WslDefaultParameterValues["grep"] = "-E"
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : NullArray

Improve performance of importing lots of commands by creating one Invoke-WslCommand function with command wrappers just being aliases that point to this function

Invoke-WslCommand would use $MyInvocation.InvocationName to know which alias was used to call it and then pass that down to wsl.

This works great except in cases where the alias is invoked using the call operator (&). The canonical sample is setting $Env:PAGER = "less" and then calling help which uses the & operator to invoke the pager.

In this scenario, I don't see a way yet to retrieve the alias that was used to call Invoke-WslCommand since $MyInvocation.InvocationName is just '&' in this case.

Command execution broken in Powershell 7.3.0

Describe the bug
Executing a command that was imported using Import-WslCommand no longer executes with the following error in Powershell 7.3.0:
/bin/bash: : command not found

To Reproduce
Steps to reproduce the behavior:

  • Import-WslCommand ls
  • ls

Expected behavior
Execution of the command should work and error should not be displayed.

Screenshots
image

Desktop (please complete the following information):

  • OS: Windows 11
  • Version 0.4.0

Additional context
This works fine in Powershell 7.2.3 but confirmed broken after upgrade to 7.3.0

Awk not working correctly

Describe the bug
Awk is not working correctly when running in Powershell. It works in my WSL2 instance but not while using Interoped commands via the WslInterop module.

To Reproduce
So I created a test.txt file that I can use to get familiar with awk and then tried a simple command.

PS C:\Users\interopUser> touch test.txt
PS C:\Users\interopUser> echo "Name, Age, City" > test.txt
PS C:\Users\interopUser> echo "Alice, 25, New York" >> test.txt
PS C:\Users\interopUser> echo "Bob, 30, Los Angeles" >> test.txt
PS C:\Users\interopUser> echo "Charlie, 22, Chicago" >> test.txt
PS C:\Users\interopUser> echo "David, 35, Houston" >> test.txt
PS C:\Users\interopUser> echo "Eva, 28, Miami" >> test.txt
PS C:\Users\interopUser> cat .\test.txt
Name, Age, City
Alice, 25, New York
Bob, 30, Los Angeles
Charlie, 22, Chicago
David, 35, Houston
Eva, 28, Miami
PS C:\Users\interopUser> awk -F', ' '{print $2}' test.txt
Name, Age, City
Alice, 25, New York
Bob, 30, Los Angeles
Charlie, 22, Chicago
David, 35, Houston
Eva, 28, Miami

As you can see the awk command did not work correctly.

So then I tried to do the same in wsl2 see here:

interopUser@UAS2324401:~$ touch test.txt
interopUser@UAS2324401:~$ echo "Name, Age, City" > test.txt
echo "Alice, 25, New York" >> test.txt
echo "Bob, 30, Los Angeles" >> test.txt
echo "Charlie, 22, Chicago" >> test.txt
echo "David, 35, Houston" >> test.txt
echo "Eva, 28, Miami" >> test.txt
interopUser@UAS2324401:~$ cat test.txt
Name, Age, City
Alice, 25, New York
Bob, 30, Los Angeles
Charlie, 22, Chicago
David, 35, Houston
Eva, 28, Miami
interopUser@UAS2324401:~$ awk -F', ' '{print $2}' test.txt
Age
25
30
22
35
28

And as expected the awk command worked in WSL2

Expected behavior
The expected behavior is to have the awk command work in the same manner in Powershell as it does for WSL2

Desktop (please complete the following information):

  • OS: Windows 11
  • Version 22H2
  • Build 22621.2283

Additional context
Here is what is being placed in my profile to help make this work:
Import-WslCommand "grep", "tail", "touch", "man", "awk", "cut", "vim"

PowerShell passes trimmed command to bash completion.

Describe the bug
PowerShell passes down command to be completed with trimmed spaces and some completions don't work because of that.

To Reproduce
Steps to reproduce the behavior:

  1. Run pwsh
  2. Enter imported command with some additional arguments and space at the end and press tab
code --disable-extension <tab>
--disable-extension   --disable-extensions
  1. PowerShell outputs completions as if there is no space at the end of the command. The number of spaces at the end doesn't matter, it just passes down trimmed command to bash.

Expected behavior
PowerShell outputting completions for command that has spaces at the end.

code --disable-extension <tab>
adamsome.vscode-theme-gruvbox-minor       ms-azuretools.vscode-docker
akamud.vscode-theme-onedark               ms-dotnettools.csharp
apollographql.vscode-apollo               ms-edgedevtools.vscode-edge-devtools

Desktop (please complete the following information):

  • Windows 2004 (OS Build (19041.208)
  • Ubuntu 20.04 on Windows 10 x86_64 on 4.19.84-microsoft-standard
  • GNU bash, version 5.0.16(1)-release (x86_64-pc-linux-gnu)

Additional context
The solution I found that works is to pad the $COMP_LINE until your cursor position $cursorPosition.
So instead of this:

$COMP_LINE = "`"$commandAst`""

do this:

$compLine = "$commandAst".PadRight($cursorPosition)
$COMP_LINE = "`"$compLine`""

Commands are not saved

I followed the instructions on the README, installed PS Core, the package with the package manager, I can add new aliases (with Import-WslCommand) of my WSL, and it works. But when I close the PowerShell window, and open a new one, the aliases are gone.

Cannot run npm/nvm

Describe the bug
Cannot run npm.

To Reproduce
In Ubuntu-20.04 run:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts
npm --version - should return version

Run PowerShell Script:

    Import-Module "WslInterop"
    Import-WslCommand "nvm", "npm", "ls"
    nvm --version
    npm --version
    ls -la

Expected behavior
I've thought this PowerShell module solves https://stackoverflow.com/questions/61823716/wsl-on-windows-10-not-loading-config-files-when-ran-interoperabiltiy

error loading profile.ps

Hi,

after following readme and storing 'PowerShell WSL Interop.ps1' as 'Profile.ps1', I get this error on powershell startup:

PowerShell 6.2.3
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

At C:\Program Files\PowerShell\6\profile.ps1:169 char:190
+ ... ata-ga-click="(Logged out) Header, go to Features">Features <span cla ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:185 char:255
+ ... ogged out) Header, go to Customer stories">Customer stories <span cla ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:186 char:231
+ ... ata-ga-click="(Logged out) Header, go to Security">Security <span cla ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:206 char:222
+ ... a-click="(Logged out) Header, go to Explore">Explore GitHub <span cla ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:242 char:185
+ ... r" data-ga-click="(Logged out) Header, go to Pricing">Plans <span cla ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:250 char:235
+ ... -ga-click="(Logged out) Header, go to Nonprofits">Nonprofit <span cla ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:251 char:258
+ ... a-ga-click="(Logged out) Header, go to Education">Education <span cla ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:822 char:93
+ ... b-code blob-code-inner js-file-line"><span class="pl-c">    </span></ ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:830 char:93
+ ... b-code blob-code-inner js-file-line"><span class="pl-c">    </span></ ...
+                                                                 ~
The '<' operator is reserved for future use.
At C:\Program Files\PowerShell\6\profile.ps1:874 char:122
+ ... le-line"><span class="pl-c">    * $WslDefaultParameterValues[&quot;gr ...
+                                                                  ~
Array index expression is missing or not valid.
Not all parse errors were reported.  Correct the reported errors and try again.
+ CategoryInfo          : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : RedirectionNotSupported

Any advice, please?

`zsh:1: permission denied:`

Describe the bug
Hi. Thanks a lot for this project. It used to work very well, but recently I tried again and apparently all commands are failing with zsh:1: permission denied:.

To Reproduce
I can reproduce it locally, feel free to ask me any details.

Expected behavior
To work, I guess.

Screenshots
image

Desktop (please complete the following information):

  • OS: Windows 11 22H2
  • Version: 0.4.1

Additional context
Any help is really appreciated. Thanks!

Alias resolution doesn't work with & $variable invocation (like in PS pager use)

@mikebattista raised this issue offline today. It's an edge case, but strikes when $env:PAGER is set to less in PowerShell.

Reproduction

$mysteriousCommand = 'grep'

& $mysteriousCommand .log

Expected Behaviour

Runs grep via WSL.

Actual Behaviour

Is unable to resolve the alias, since $MyInvocation.InvocationName is unhelpfully &...

Proposed Solution

Ideally, PowerShell should create more information here, but to work around this, the most straightforward solution might be to create functions rather than aliases, which wrap Invoke-WslCommand:

function grep
{
    Invoke-WslCommand -CommandName grep ...
}

Wsl Default Paramter Error

Hi,

I get an error when using $WslDefaultParameterValues on Powershell 7.0.0 and WslInterop 0.2.2

$WslDefaultParameterValues["ls"] = "-AFh --group-directories-first" InvalidOperation: Cannot index into a null array.

The commands themselves work, but also the resolving of Linux paths does not work. I tested on Windows 10 1809 and 1909.

Best regards

Imported commands fail with path translation failures and mount failure

Describe the bug

Executing an imported command fails with path translation failures and mount failure. See "screenshots" below.

To Reproduce
Steps to reproduce the behavior:

  1. Install-Module WslInterop
  2. Import-WslCommand "fmt", "indent"
  3. Run fmt

Expected behavior
Powershell executes the fmt program from WSL.

Screenshots

C:\Users\user> import-wslcommand "fmt", "indent"
C:\Users\user> fmt
<3>WSL (9) ERROR: CreateProcessParseCommon:711: Failed to translate C:\Users\user
<3>WSL (9) ERROR: CreateProcessParseCommon:757: getpwuid(0) failed 2
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\PowerShell\7
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\OpenSSH\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\Razer Chroma SDK\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Razer Chroma SDK\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\Razer\ChromaBroadcast\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Razer\ChromaBroadcast\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Microsoft\jdk-11.0.19.7-hotspot\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\system32
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\Wbem
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\WindowsPowerShell\v1.0\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\OpenSSH\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\Windows Kits\10\Microsoft Application Virtualization\Sequencer\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\MATLAB\R2022b\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Microsoft SQL Server\150\Tools\Binn\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Git LFS
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\dotnet\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\AppData\Local\Microsoft\WindowsApps
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\AppData\Local\Programs\Microsoft VS Code\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\.dotnet\tools
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Vim\vim90\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\AppData\Local\Microsoft\WinGet\Links
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Windows Kits\10\Testing\Runtimes\TAEF\x64
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Windows Kits\10\Testing\Runtimes\TAEF\x86
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\PowerShell\7\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Docker\Docker\resources\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\GnuPG\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Go\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\GitHub CLI\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Git\cmd
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Amazon\AWSCLIV2\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\AppData\Local\Microsoft\WindowsApps
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\AppData\Local\Programs\Microsoft VS Code\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\.dotnet\tools
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Vim\vim91\
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\AppData\Local\Microsoft\WinGet\Links
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\texlive\2023\bin\windows
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\GNU Octave\Octave-8.4.0\mingw64\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Windows Kits\10\Testing\Runtimes\TAEF\x64
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Windows Kits\10\Testing\Runtimes\TAEF\x86
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\user\go\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\GitHub CLI
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Graphviz\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\doxygen\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\x64\bin
<3>WSL (9) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\7-Zip
Processing fstab with mount -a failed.
Failed to mount C:\, see dmesg for more details.

<3>WSL (9) ERROR: CreateProcessEntryCommon:334: getpwuid(0) failed 2
<3>WSL (9) ERROR: CreateProcessEntryCommon:505: execvpe /bin/sh failed 2
<3>WSL (9) ERROR: CreateProcessEntryCommon:508: Create process not expected to return
C:\Users\user>

Desktop (please complete the following information):

  • OS: Windows 11 Pro 23H2
  • Powershell 7.4.2
C:\Users\user> $PSEdition
Core
C:\Users\user> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Importing WSL Bash aliases to Powershell

Hi, I recently learned about this project and it is a game changer. Thanks for this.

However, since it is painful (at least for me) to write aliases for Powershell, I was wondering if it is possible to import bash aliases to PowerShell. This will help a lot.

Thanks a lot.

Piping to less doesn't work with WSL2 distro

Describe the bug
I have followed the instructions to map various Linux command, including less. If I try to pipe output via less, the command hangs at the colon prompt until I hit CTRL-C. This happens on a distro running as WSL v2. Version 1 distros are fine.

To Reproduce
Steps to reproduce the behavior:

  1. Install WslInterop.
  2. Add following line to PS profile:
Import-WslCommand "apt", "awk", "grep", "head", "less", "sed", "seq", "tail", "dos2unix", "du", "df", "wget", "gzip", "cat", "free", "top", "uname", "wc"
  1. Install Ubuntu from Microsoft Store.
  2. Set this to the default:
wsl -s Ubuntu
  1. Set to WSL 2:
wsl --set-version Ubuntu 2
  1. In new PowerShell (using Windows Terminal), type:
cd <a folder with more than a page of files>
ls | less

The command hangs at :. You can't, for example, press f or b.

Expected behavior
Piping output to less should allow you to page up/down, search, etc.

Screenshots
N/A

Desktop (please complete the following information):

  • OS: Windows 10 20H2
  • PowerShell v7.1.2

Additional context
Changing the distribution to WSL 1 fixes the issue:

wsl --set-version Ubuntu 1

In a new Terminal session, the following now works as expected:

ls | less

Thank you for providing this incredibly useful module.

Bash Completion for python based commands (Ansible)

Describe the bug
The argument completer does not find the correct completion for python based commands.

To Reproduce
Steps to reproduce the behavior:

apt install ansible
apt install python3-argcomplete

Test functionality in wsl, e.g.

ansible-playbook --a
this results in
ansible-playbook --ask

Leave wsl and create the command in PowerShell:
Import-WslCommand ansible-playbook

Expected behavior
Getting the same result in PowerShell, but

ansible-playbook --a
results in
ansible-playbook --a

So nothing happens.

Additional context
The behavior is clear, if I query the following in PowerShell

$global:WslCompletionFunctions

I can see that the value for ansible-playbook is "_minimal".

So basically the problem is in line 126 of WslInterop.psm1:

$global:WslCompletionFunctions[$command] = wsl.exe bash -c ". /usr/share/bash-completion/bash_completion 2> /dev/null; __load_completion $command 2> /dev/null; complete -p $command 2> /dev/null | sed -E 's/^complete.-F ([^ ]+).`$/\1/'"

My bash knowledge is way too low to fix this.

Do you have any idea how to fix this or at least what I can manually set by:

$global:WslCompletionFunctions["ansible-playbook"] = "???"

Thx for your great module, I enjoy it almost every day, but this would improve my work even a little more.

Feature Request: Ability to define bash functions that can be used either in the shell or PowerShell profile.

Is your feature request related to a problem? Please describe.
I want to add the following .bashrc function for use (convert string file sizes to bytes):

toBytes() {
 echo $1 | echo $((`sed 's/.*/\L\0/;s/t/Xg/;s/g/Xm/;s/m/Xk/;s/k/X/;s/b//;s/X/ *1024/g'`))
}

Describe the solution you'd like
A way to define bash functions that can be used in my PowerShell profile or anywhere else.

Describe alternatives you've considered
Converting the bash function to a PowerShell function. This can be done, but I'd really like the option to just use the bash function.

Additional context
https://stackoverflow.com/questions/17615881/simplest-method-to-convert-file-size-with-suffix-to-bytes

wsl.exe ls is super slow (is this due to vmIdleTimeout=60 seconds)

Hi, Not sure if this is the correct place to ask this, my apologies if not. I followed the awesome blog that links to this repo and It has worked for a long time. However on windows 11 I'm noticing that the first time you call ls (for example) it's very slow (3 to 5 seconds to list a couple of files). It seems to speed up after that but then will occasionally become slow again.

I was wondering if it were due to the new vmIdleTimeout setting for wsl2 on windows 11; however I cannot seem to get consistent result to confirm that this is the case (even when setting it to 3600000 (A.K.A 1 hour).

Has anyone else experienced the same issue?

Paths with spaces that are enclosed in quotes are not translated properly when passed to wslpath.

Repro:

  • less 'C:\Program Files\PowerShell\7-preview\LICENSE.txt'

Expected Behavior:
Works as expected.

Actual Behavior:
'C:/Program Files/PowerShell/7-preview/LICENSE.txt': No such file or directory

The single quotes are considered part of the path.

This is a problem for any paths with spaces since they need to be surrounded with quotes. Windows completion automatically inserts quotes when completing paths with spaces.

Note that running wsl wslpath 'C:\Program Files\PowerShell\7-preview\LICENSE.txt' at the command line directly works as expected, so the escaped quotes in the function wrapper that solved other issues seem to be causing problems here.

Escape chars with sed

Describe the bug
Backslash in escape chars are converted to forward-slash, which makes regex in sed, grep, etc. not work.

To Reproduce
Steps to reproduce the behavior:

PS > echo "asdf" | sed 's/d/\n/'
asnf
PS > cat Env:\Path | sed 's/;/\n/g'
sed: -e expression #1, char 2: unterminated 's' command
/bin/bash: /n/g: No such file or directory

Expected behavior

PS > echo "asdf" | sed 's/d/\n/'
as
f
PS > cat Env:\Path | sed 's/;/\n/g'
C:\Windows\system32
C:\Windows
...

Desktop (please complete the following information):

  • OS: Windows 10
  • Powershell 7.0 preview4
  • git master 257eec1

Make WslDefaultParameterValues dynamic not static

Is your feature request related to a problem? Please describe.
Currently we can set the default parameter values in a hashtable named WslDefaultParameterValues.

This is basically a Dictionary<string, string>.

Describe the solution you'd like
I would like to see a modification, e.g. $WslDefaultParameters.

This should check the type. If it is a string, then work like before.

If it is a scriptblock, then get the value at runtime by executiong the function.

Additional context
In case you are asking why I want this:

I am using a commandline tool accepting passwords and want to use PowerShell´s secret store for that.

To combine these, I can to the following:

ansible-playbook playbook.yaml -i /hosts --extra-vars "ansible_become_pass=$((Get-Secret MySecret).Password | ConvertFrom-SecureString -AsPlainText)"

Of course the last parameter is almost always needed and very clumsy to write in this notation.

So what I want to do, is store this expression as a default parameter.

But this does not work as when I put it this way in the dictionary, the $(xxx) part is not evaluated.

I can do the following:
$WslDefaultParameterValues = @{ "ansible-playbook" = "--extra-vars ansible_become_pass=$((Get-Secret MySecret).Password | ConvertFrom-SecureString -AsPlainText)" }

This works of course, but now

$WslDefaultParameterValues[ "ansible-playbook"]

shows my password.

To make the usage conveniant, I would suggest accepting strings and functions. So the typical case works like before, but there is an additional option to make the default parameter generation dynamic.

Completion not working for default shells other than bash

If I'm not mistaken, the readme claims that by importing a wsl command both command and argument completion is enabled automatically for pwsh as well:

  • Command completion is enabled by PowerShell's command completion
  • Argument completion is enabled by registering an ArgumentCompleter that shims bash's programmable completion

To Reproduce
Steps to reproduce the behavior:

  1. Run pwsh:
PowerShell 7.0.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

PS C:\Users\x> pwsh --version
PowerShell 7.0.0
  1. Select a wsl that has completion enabled in wsl bash:
[x@DESKTOP-04NUF17 ~]$ echo $0
bash
[x@DESKTOP-04NUF17 ~]$ git <tab><tab>
add               blame             cherry-pick       config            format-patch      gui               log.cmd           pull              remote            restore           show              status            worktree
am                branch            citool            describe          fsck              help              merge             push              repack            revert            show-branch       submodule
apply             bundle            clean             diff              gc                init              mergetool         range-diff        replace           rm                sparse-checkout   switch
archive           checkout          clone             difftool          gitk              instaweb          mv                rebase            request-pull      send-email        stage             tag
bisect            cherry            commit            fetch             grep              log               notes             reflog            reset             shortlog          stash             whatchanged
[x@DESKTOP-04NUF17 ~]$ grep --<tab><tab>
--after-context=         --byte-offset            --dereference-recursive  --exclude-from=          --fixed-strings          --invert-match           --max-count=             --null-data              --regexp=
--basic-regexp           --color                  --devices=               --extended-regexp        --help                   --label=                 --no-filename            --only-matching          --text
--before-context=        --colour                 --directories=           --file=                  --ignore-case            --line-buffered          --no-ignore-case         --perl-regexp            --version
--binary                 --context=               --exclude=               --files-with-matches     --include=               --line-number            --no-messages            --quiet                  --with-filename
--binary-files=          --count                  --exclude-dir=           --files-without-match    --initial-tab            --line-regexp            --null                   --recursive              --word-regexp
[lenni@DESKTOP-04NUF17 ~]$ ls --<tab><tab>
--all                                      --dereference-command-line-symlink-to-dir  --hide-control-chars                       --numeric-uid-gid                          --tabsize=
--almost-all                               --directory                                --human-readable                           --quote-name                               --time
--author                                   --dired                                    --hyperlink                                --quoting-style=                           --time=
--block-size=                              --escape                                   --ignore=                                  --recursive                                --time-style=
--classify                                 --file-type                                --ignore-backups                           --reverse                                  --version
--color                                    --format=                                  --indicator-style=                         --show-control-chars                       --width=
--color=                                   --full-time                                --inode                                    --si
--context                                  --group-directories-first                  --kibibytes                                --size
--dereference                              --help                                     --literal                                  --sort
--dereference-command-line                 --hide=                                    --no-group                                 --sort=
  1. Import the wsl command(s) into pwsh:
PS C:\Users\x> Import-WslCommand "git", "grep", "ls"
PS C:\Users\x>
  1. Completion does not work:
PS C:\Users\x> git .\.bash_profile<tab>^C
PS C:\Users\x> grep --<tab><tab>^C
PS C:\Users\x> ls --<tab><tab>^C
PS C:\Users\x>

Expected behavior
Tabbing on those commands in pwsh would provide me with the same completion commands as in bash.

Desktop (please complete the following information):

  • Windows 2004 (OS Build (19041.173)
  • Arch Linux on Windows 10 x86_64 on 4.19.84-microsoft-standard
  • GNU bash, version 5.0.16(1)-release (x86_64-pc-linux-gnu)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.