在 WSL Ubuntu 中使用 Chocolatey VS Code

问题描述

我无法让 VS Code code.exe 在 WSL 中正常运行。 (我在这里找到了一个相关的问题,但它并没有解决这个问题 - Launch VS Code from WSL Bash)。

在 WSL(任何发行版)中,我们可以访问 Windows 上的任何可执行文件。例如

alias chrome="\"/mnt/c/Program Files/Google/Chrome/Application/chrome.exe\""  # Open Chrome from WSL
alias notepad++="\"/mnt/c/Program Files/Notepad++/notepad++.exe\""            # Open Notepad++

在 bash 中输入 chrome 将打开 Chrome,而 notepad++ ~/.bashrc 将在 Notepad++ 中打开 .bashrc 这一切都很好。

但是,我遇到了 code.exe 安装提供的 choco inst VisualStudioCode 问题。当我尝试时:

alias vscode="\"/mnt/c/ProgramData/chocolatey/bin/code.exe\""

这真的很失败。

[main 2021-01-24T18:44:17.966Z] update#setState idle
(node:20404) Electron: Loading non-context-aware native module in renderer: '\\?\C:\tools\vscode\resources\app\node_modules.asar.unpacked\vscode-sqlite3\build\Release\sqlite.node'. This is deprecated,see https://github.com/electron/electron/issues/18397.
(node:20404) Electron: Loading non-context-aware native module in renderer: '\\?\C:\tools\vscode\resources\app\node_modules.asar.unpacked\spdlog\build\Release\spdlog.node'. This is deprecated,see https://github.com/electron/electron/issues/18397.
(node:18692) Electron: Loading non-context-aware native module in renderer: '\\?\C:\tools\vscode\resources\app\node_modules.asar.unpacked\spdlog\build\Release\spdlog.node'. This is deprecated,see https://github.com/electron/electron/issues/18397.

所以,我可以忽略它,只在 Ubuntu 中使用 code.exe(因为 WSL 将扫描可执行文件的路径)。这种排序有效,但出现以下错误

'\\wsl$\Ubuntu-20.04\home\boss'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

VS Code 确实打开,但是如果将文件名作为参数,文件将无法打开(同时请注意,对于 notepad++ 示例,所有打开的文件都可以使用上述别名完美运行)。 >

似乎是说 code.exe 不支持 UNC 路径,但它确实支持 UNC 路径,正如我从 PowerShell 中看到的那样。 code.exe \\HPEnvy\Drive-D\test.txt 完美打开。

这真的会完善我的 WSL 设置,以便能够打开我正在使用 VS Code 无缝处理的代码。有没有人知道为什么会发生这种情况/如何解决

解决方法

我的安装有一个 shell 脚本,用于在 ../Microsoft VS Code/bin/code 中启动 VSCode。我相当确定它是与 VSCode 一起安装的,但它有可能来自“Remote - WSL”扩展。

在您的安装中存在吗?如果是,请将该 bin 目录添加到您的路径中(完整安装程序会自动执行此操作)。然后只需使用 code 而不是 code.exe 从 WSL 中启动。

如果没有,首先确保在 VSCode 中安装了“Remote - WSL”扩展(或者更好的是“Remote Development”扩展包,其中包括 WSL 支持)。如果在那之后它仍然不存在,这里是应该存在于 VSCode/bin/code 中的脚本的内容:

#!/usr/bin/env sh
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
if [ "$VSCODE_WSL_DEBUG_INFO" = true ]; then
    set -x
fi

COMMIT="ea3859d4ba2f3e577a159bc91e3074c5d85c0523"
APP_NAME="code"
QUALITY="stable"
NAME="Code"
DATAFOLDER=".vscode"
VSCODE_PATH="$(dirname "$(dirname "$(realpath "$0")")")"
ELECTRON="$VSCODE_PATH/$NAME.exe"

IN_WSL=false
if [ -n "$WSL_DISTRO_NAME" ]; then
    # $WSL_DISTRO_NAME is available since WSL builds 18362,also for WSL2
    IN_WSL=true
else
    WSL_BUILD=$(uname -r | sed -E 's/^[0-9.]+-([0-9]+)-Microsoft.*|.*/\1/')
    if [ -n "$WSL_BUILD" ]; then
        if [ "$WSL_BUILD" -ge 17063 ]; then
            # WSLPATH is available since WSL build 17046
            # WSLENV is available since WSL build 17063
            IN_WSL=true
        else
            # If running under older WSL,don't pass cli.js to Electron as
            # environment vars cannot be transferred from WSL to Windows
            # See: https://github.com/microsoft/BashOnWindows/issues/1363
            #      https://github.com/microsoft/BashOnWindows/issues/1494
            "$ELECTRON" "$@"
            exit $?
        fi
    fi
fi
if [ $IN_WSL = true ]; then

    export WSLENV="ELECTRON_RUN_AS_NODE/w:$WSLENV"
    CLI=$(wslpath -m "$VSCODE_PATH/resources/app/out/cli.js")

    # use the Remote WSL extension if installed
    WSL_EXT_ID="ms-vscode-remote.remote-wsl"

    ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" --locate-extension $WSL_EXT_ID >/tmp/remote-wsl-loc.txt 2>/dev/null </dev/null
    WSL_EXT_WLOC=$(cat /tmp/remote-wsl-loc.txt)

    if [ -n "$WSL_EXT_WLOC" ]; then
        # replace \r\n with \n in WSL_EXT_WLOC
        WSL_CODE=$(wslpath -u "${WSL_EXT_WLOC%%[[:cntrl:]]}")/scripts/wslCode.sh
        "$WSL_CODE" "$COMMIT" "$QUALITY" "$ELECTRON" "$APP_NAME" "$DATAFOLDER" "$@"
        exit $?
    fi

elif [ -x "$(command -v cygpath)" ]; then
    CLI=$(cygpath -m "$VSCODE_PATH/resources/app/out/cli.js")
else
    CLI="$VSCODE_PATH/resources/app/out/cli.js"
fi
ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@"
exit $?

文件的权限是 0777。而且,如上所述,它应该在您的路径中。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...