WSL: "Invalid argument" when executing Windows commands on Ubuntu 20.04

Environment

Windows build number: 10.0.19041.572
Your Distribution version: Ubuntu 20.044
Whether the issue is on WSL 2 and/or WSL 1: WSL 2

Steps to reproduce

wsl-ubuntu

WSL logs:

Expected behavior

When I run code . inside the root path or some subfolder of linux, VS Code on Windows should be opened, just like it happen when I run the command from the mount folder.

Same for the explorer.exe command (and all Windows commands). When I run them from root or a subfolder, the command should be executed.

Actual behavior

The system print me back an error saying Invalid argument

Note: I searched in previous issues for some help on how to solve this, but didn’t found how to

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 50
  • Comments: 78 (3 by maintainers)

Most upvoted comments

UPDATE Better solution here: https://github.com/microsoft/WSL/issues/6170#issuecomment-1117689071


I have this issue sometimes. Not yet sure why. It’s because the wsl network drives aren’t mapped correctly. So the path then won’t work and this is why it is an Invalid argument. The path can simply not be resolved. A fast fix in cmd or ps shell:

wsl --shutdown
wsl

The above release doesn’t appear to have fixed the issue for me.

I’m on WSL Release 2.1.0 (in a windows terminal):

>wsl --version
WSL version: 2.1.0.0

but from inside Ubuntu on WSL from my home directory:

$ ping.exe
/mnt/c/Windows/system32/ping.exe: Invalid argument

For me, WSL may work for a few hours without this problem, but at some point it appears. Once it starts, every .exe program will show this error. The only “fix” is to wsl --shutdown which is obviously very inconvenient.

Interestingly WSL is in this state almost everytime I start my computer.

If I do a wsl --shutdown then the problem goes away wether or not I use --cd or not.

I am running Windows 11 Pro and encountered this same issue. It was working fine at first, but suddenly stopped. As a workaround, I found the following command from the docs to work from the Windows cmd prompt (https://code.visualstudio.com/docs/remote/wsl):

code --remote wsl+<distro name> <path in WSL>

I am wondering if a recent update changed the way paths are handled. I did not notice the remote server was reinstalled in WSL after issuing the above command.

I have the same kind of Schrödinger bug that comes and disappears randomly.

wsl -v
WSL version: 1.0.3.0
Kernel version: 5.15.79.1
WSLg version: 1.0.47
MSRDC version: 1.2.3575
Direct3D version: 1.606.4
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.19045.2486
jlv@ubuntu% lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.1 LTS
Release:	22.04
Codename:	jammy

Right now it’s working, but few days ago here is what I got

jlv@ubuntu% cd /tmp
jlv@ubuntu% cmd.exe
/mnt/c/windows/system32/cmd.exe: Invalid argument
                                                 jlv@ubuntu%

and when moving on a Windows directory

jlv@ubuntu% cd /mnt/c
jlv@ubuntu% cmd.exe
Microsoft Windows [Version 10.0.19045.2364]
(c) Microsoft Corporation. All rights reserved.
C:\>

The wsl --shutdown workaround does fix it temporarily, but it’s a major inconvenience. Both Docker Desktop and VS Code misbehave when wsl is yanked out from under them, so I end up needing to close all my Code windows and shut down Docker before doing it.

I’ve not tracked down what causes the “Invalid argument” to start happening - I used to think it was sleeping/hibernating that was the issue, but I’ve observed it happening even without sleep.

It happens at least once a day. This issue and #4698 are both major barriers preventing me from recommending WSL2 to anyone as a daily driver for work.

I encountered this today on: WSL version: 2.1.5.0 Kernel version: 5.15.146.1-2 WSLg version: 1.0.60 MSRDC version: 1.2.5105 Direct3D version: 1.611.1-81528511 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.19045.4291

wsl --shutdown fixed it for me, we’ll see if it comes back.

@OneBlue or @craigloewen-msft Hello, any news/updates on this issue? Several users have reported this issue going back almost 3 years now, but there have been no fixes, or acknowledgements even.

I am encountering the identical problem

image

Windows version: 10.0.21337 Build 21337 WSL 2 Ubuntu 20.04.2 LTS

+1, I believe this issue is not resolved.

$ wsl --version
WSL version: 2.1.5.0
[from Ubuntu-22.04 wsl instance]
$ code . &
/mnt/c/Program Files/Microsoft VS Code/Code.exe: Invalid argument

FWIW, a wsl --uninstall and re-install solved it for the same version. Its possible that the container was created with an older version and hence the problem randomly manifested

I got this bug after I installed 11 over 10. I noticed something interesting but very weird and hard to debug. If I’m in root’s home or / the error happens but if I cd to /mnt/c it goes away (!).

I have the same issue, but I have verified that it only happens when my pwd is a network drive.

Example:

$ echo test | clip.exe  # this works OK
$ sudo mount -t drfvs '\\path\to\network\drive' /mnt/example
$ cd /mnt/example
$ echo test | clip
/mnt/c/WINDOWS/system32/clip.exe: Invalid argument

As a workaround, make a shell script called clip and place it somewhere in your path. This seems to work.

#!/usr/bin/bash
curdir=$PWD
cd /
cat | clip.exe
cd $curdir

Now you can just do

$ echo test | clip

Same issue here running Debian WSL2 - I can invoke commands at first open, but after running a few commands, they end up stating “Invalid Argument”. Also, WSL2 seems to forget where the current working directory is sporatically. I have a rust program calling env::current_dir() at different times in it’s lifecyle, and the command eventually fails (not at it’s first invocation, but when called in a later method). It will then completely fail on any subsequent execution, until I create a new terminal.

UPDATE: I updated Windows 10 Home to Windows 10 Education (which is Windows 10 Enterprise plus some educational cruft). I uninstalled and reinstalled WSL 2 and Ubuntu 20.04 LTS. THE PROBLEM PERSISTS.

ORIGINAL POST:

I wish I were equipped to help. All I can do is to further confirm the problem.

Edition: Windows 10 Home (I know, but it’s supposed to work now; and essentially all university students using Windows are using Windows 10 Home on a personal laptop, so that’s what we have to target here.) Version: 21H1 OS Build: 19043.1151 (right up to date)

One bit of evidence I can offer: Replacing Ubuntu 20.04 LTS with 18.04 doesn’t help.

Some kind of guidance / repair is urgently needed here (at least by me); I was planning on having this working by the time classes start at a University in just a few weeks. Again, deep apologies, but I’m not equipped to resolve the issue on the WSL / Ubuntu side. Any kind of update would be extremely helpful!

I am facing the same problem. at startup, all good

$ /mnt/c/WINDOWS/system32/cmd.exe

Microsoft Windows [Version 10.0.19042.844]
(c) 2020 Microsoft Corporation. All rights reserved.

after run docker login

$ /mnt/c/WINDOWS/system32/cmd.exe

/mnt/c/WINDOWS/system32/cmd.exe: Invalid argument

I had to implement a workaround to keep our GitHub runners working, so I cannot reproduce this specific problem. However, I created a small repro that may help. Essentially it triggers a race that causes the process creation to fail due to an invalid current directory. Not sure if the root cause is the same (here the current directory being non-existent), but the outcome is the same. I have also seen the problem that the original post is referring to, where I am in the users home directory and the Windows process creation fails consistently (which sounds more like a communication problem with the VM).

I am sending you the logs via e-mail.

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
        int result;
        const char* home = "/home/user";
        const char* path = "/home/user/test";
        result = mkdir(path, 0755);
        printf("mkdir: %d\n", result);
        result = chdir(path);
        printf("chdir: %d\n", result);
        FILE* file = popen("/mnt/c/Windows/system32/cmd.exe /c echo Hello", "w");
        usleep(10000);
        result = chdir(home);
        printf("chdir: %d\n", result);
        result = rmdir(path);
        printf("rmdir: %d\n", result);
        result = pclose(file);
        printf("pclose: %d\n", result);
        return 0;
}
user@host:~$ cc test.c && ./a.out
mkdir: 0
chdir: 0
chdir: 0
rmdir: 0
/mnt/c/Windows/system32/cmd.exe: Invalid argument
pclose: 256

If I make the usleep delay sufficiently large, it works:

user@host:~$ cc test.c && ./a.out
mkdir: 0
chdir: 0
'\\wsl.localhost\Ubuntu-22.04\home\user\test'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
Hello
chdir: 0
rmdir: 0
pclose: 0

This might helps someone else here. I just got this issue this morning. Not sure what changed, everything was working fine yesterday. Last night I hibernated. This morning I noticed that WSL started up faster than usual, which surprised me as it usually takes a minute. Then I do my usual

cd project
code .

And I get that Invalid argument error. Code is working fine from Windows.

Then, I closed WSL session and from Powershell, I did wsl --shutdown

Restarted WSL and now Code is working again!

Had this problem lately when trying to call vscode on WSL2.

Then one day I was looking at my Task Scheduler when I noticed this task:

image

I didn’t create it. Maybe Docker Desktop did?

Anyway I disabled this task and one reboot later, voilá, problem solved.

I created a simple script and added a symlink to replace the normal code:

#!/bin/bash

current_working_directory=$(pwd)
vs_code_directory="/mnt/c/Users/<YOUR_USER_NAME>/AppData/Local/Programs/Microsoft VS Code/bin"
param="$1"

cd "$vs_code_directory"
$(./code "$current_working_directory/$param")
cd "$current_working_directory"

It works for me for what I need (just open VSCode loading a folder or file)

I see this pop up seemingly randomly. Doing a wsl --shutdown and restarting my terminal fixes it for the next hour or two.

It’s definitely have something to do with path. Here is an easy reproducible workaround: Starting test (can be any directory, or even without specifying directory):

wt nt wsl --cd /

Inside WT:

user@host:/$ ping.exe
/mnt/c/WINDOWS/system32/ping.exe: Invalid argument
                                                  user@host:/$ cd .
user@host:/$ ping.exe

Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]

Note: It’s just necessary to issue a ‘cd’ command. But not necessary to actually change the directory. This is not environment related, as the only difference is “OLDPWD”. Setting it explicitly doesn’t work.

WT version 1.12.3472.0 O/S Ubuntu 18.04 WSL version 2

This appears to be the same issue as #4260

So at least for me, I have found the Invalid argument behavior was caused by the WSL_DISTRO_NAME environment variable in Linux not matching my distro name from “wsl --list”, note this behavior is easily reproducible:

(~)[513]> set |grep WSL WSL_DISTRO_NAME=Ubuntu-22.04

(~)[514]> net.exe The syntax of this command is:

NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP | HELPMSG | LOCALGROUP | PAUSE | SESSION | SHARE | START | STATISTICS | STOP | TIME | USE | USER | VIEW ]

(~)[515]> export WSL_DISTRO_NAME=not_the_right_name

(~)[516]> net.exe /mnt/c/Windows/System32/net.exe: Invalid argument

(~)[517]> export WSL_DISTRO_NAME=Ubuntu-22.04

(~)[518]> net.exe The syntax of this command is:

NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP | HELPMSG | LOCALGROUP | PAUSE | SESSION | SHARE | START | STATISTICS | STOP | TIME | USE | USER | VIEW ]

I’m running into the same issue.

wsl -v WSL version: 1.0.3.0 Kernel version: 5.15.79.1 WSLg version: 1.0.47 MSRDC version: 1.2.3575 Direct3D version: 1.606.4 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.19045.2486

$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.1 LTS Release: 22.04 Codename: jammy

Steps to reproduce

$ pwd /home/fabrice $ cmd.exe /mnt/c/windows/system32/cmd.exe: Invalid argument

$ cmd.exe /mnt/c/windows/system32/cmd.exe: Invalid argument

$ cd /mnt/c $ cmd.exe Microsoft Windows [Version 10.0.19045.2486] © Microsoft Corporation. All rights reserved.

C:>exit $ cd - $ pwd /home/fabrice $ cmd.exe /mnt/c/windows/system32/cmd.exe: Invalid argument

I can reproduce this issue on Windows 10 and Windows 11 using the below shell script. Note that you need to run this script twice if the first time succeeds for some reason:

#!/bin/sh

cd /mnt/c

dir_to_mount="/mnt/c/dir_to_mount"
rm -rf "$dir_to_mount"
mkdir -p "$dir_to_mount"

cmd.exe /c echo before docker

docker run --rm --mount type=bind,src="$dir_to_mount",dst=/dir_to_mount/ alpine sh -c 'echo inside docker'
rm -rf "$dir_to_mount"

# Uncommenting this fixes the issue
#cd .

# Fails with "Invalid argument"
cmd.exe /c echo after docker

Output:

before docker
inside docker
/mnt/c/WINDOWS/system32/cmd.exe: Invalid argument

My observations:

  • The issue happens if docker bind-mounts a directory from a Windows drive. Mounting a WSL directory doesn’t trigger it.
  • The bind-mounted directly must be freshly created by the script itself. An existing directory (created before the script is run) doesn’t trigger it.
  • The issue goes away if you cd after running docker. Even a simple cd . fixes it.

root@OSHAH:/my-app# serve -s build ERROR: Cannot copy to clipboard: Command failed: /root/.nvm/versions/node/v18.4.0/lib/node_modules/serve/node_modules/clipboardy/fallbacks/windows/clipboard_x86_64.exe --copy /root/.nvm/versions/node/v18.4.0/lib/node_modules/serve/node_modules/clipboardy/fallbacks/windows/clipboard_x86_64.exe: Invalid argument

I tried to copy the chrome.exe from the Windows “Program Files” directory to /var/tmp/, then run it , got the same issue, why: root@DESKTOP-R0EA0CF:/home/gwz/sec# cp /mnt/c/Program\ Files/Google/Chrome/Application/chrome.exe /var/tmp root@DESKTOP-R0EA0CF:/home/gwz/sec# root@DESKTOP-R0EA0CF:/home/gwz/sec# root@DESKTOP-R0EA0CF:/home/gwz/sec# /var/tmp/chrome.exe /var/tmp/chrome.exe: Invalid argument root@DESKTOP-R0EA0CF:/home/gwz/sec# But if I run the /mnt/c/Program\ Files/Google/Chrome/Application/chrome.exe, it’s ok.

I’m faced with the same problem of getting Invalid Argument. It’s unbelievable that the issue has not been resolved after almost 1.5 years.

And then there’s also the issue with not being able to access the network drive under \\wsl$ from Windows File Explorer unless you map it to e.g. Z:\ drive. I don’t know if they’re related but there are many issues opened related to this, too.

Add VSCode to the path this solved to me.

I have a new piece of information. Windows Terminal was recently changed to let you specify a linux path as the starting directory. When I open Ubuntu-20.04 with my Linux path in there, I get the Invalid Argument errors. If I leave it the default of %USERPROFILE% then it works fine. I think under the hood Windows terminal is using the new command line argument:

--cd <Directory>
        Sets the specified directory as the current working directory.
        If ~ is used the Linux user's home path will be used. If the path begins
        with a / character, it will be interpreted as an absolute Linux path.
        Otherwise, the value must be an absolute Windows path.

If I try this from Powershell, if I do “wsl --cd /home/username” then it can’t run Windows executables.

But if I launch a wsl without the --cd, then it works.