WSL2でVSCodeのRemote開発が使えないときの対処

# 対象者
VSCode Remoteを使用してWSL2上のディレクトリで開発しようとしている人
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/308037/c2cf5d2d-8659-5567-ed32-9783eaa415ce.png)

# 発生事象
wsl上のディレクトリを指定してVSCodeを開こうとすると以下のエラーが発生する

```
Microsoft Windows [Version 10.0.18932.1000]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>wsl
root@DESKTOP-KOH0NGK:/mnt/c/WINDOWS/system32# cd /home/
root@DESKTOP-KOH0NGK:/home# code .
The factory method of "vs/server/remoteCli" has thrown an exception
{ Error: spawn EPERM
    at ChildProcess.spawn (internal/child_process.js:358:11)
    at Object.spawn (child_process.js:533:9)
    at l (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/server/remoteCli.js:86:650)
    at Object.<anonymous> (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/server/remoteCli.js:88:1010)
    at Function.t._safeInvokeFunction (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:19:945)
    at Function.t._invokeFactory (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:20:175)
    at t.complete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:20:412)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:614)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:858)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:858)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:858)
    at s._resolve (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:174)
    at s.defineModule (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:24:534)
    at o (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:28:673)
    at Object.l (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:36:291)
    at Function.t._safeInvokeFunction (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:19:945)
    at Function.t._invokeFactory (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:20:175)
    at t.complete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:20:412)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:614)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:858)
    at s._resolve (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:174)
    at s.defineModule (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:24:534)
    at o (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:31:789)
    at c (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:17:118)
    at Object.<anonymous> (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/server/remoteCli.nls.js:4:1)
    at t._createAndEvalScript (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:17:162)
    at /root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:16:762
    at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3) errno: 'EPERM', code: 'EPERM', syscall: 'spawn' }
Error: spawn EPERM
    at ChildProcess.spawn (internal/child_process.js:358:11)
    at Object.spawn (child_process.js:533:9)
    at l (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/server/remoteCli.js:86:650)
    at Object.<anonymous> (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/server/remoteCli.js:88:1010)
    at Function.t._safeInvokeFunction (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:19:945)
    at Function.t._invokeFactory (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:20:175)
    at t.complete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:20:412)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:614)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:858)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:858)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:858)
    at s._resolve (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:174)
    at s.defineModule (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:24:534)
    at o (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:28:673)
    at Object.l (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:36:291)
    at Function.t._safeInvokeFunction (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:19:945)
    at Function.t._invokeFactory (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:20:175)
    at t.complete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:20:412)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:614)
    at s._onModuleComplete (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:858)
    at s._resolve (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:30:174)
    at s.defineModule (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:24:534)
    at o (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:31:789)
    at c (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:17:118)
    at Object.<anonymous> (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/server/remoteCli.nls.js:4:1)
    at t._createAndEvalScript (/root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:17:162)
    at /root/.vscode-server/bin/0f3794b38477eea13fb47fbe15a42798e6129338/out/vs/loader.js:16:762
    at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3)
```

# 対処方法
- wsl上からWindows上にインストールされているVSCode起動用の”Code.exe”を直接叩く
- こんな感じ ： `/mnt/c/Users/xxxx/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe .`
- `/mnt/c` でWindowsのディレクトリのぞけるのでVSCodeを入れた階層を、指定してあげてください
- ちなみにこれは暫定的な対処方法のはず。対応待ち

```
root@DESKTOP-KOH0NGK:/home/xxxx/projects/vuejs# /mnt/c/Users/xxxx/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe .
```

# 参考
他にもgithubのissueが乱立していましたが、私が拝見したのはこのあたりだったので載せておきます

- https://github.com/microsoft/vscode-remote-release/issues/914#
- https://github.com/microsoft/WSL/issues/4255

# その他
[前回の記事](https://qiita.com/ogawayuj/items/7ba34e3fd50f2fdc8753) 
wsl2とvscodeとdockerを併用した開発環境までもう一息…