【问题标题】:Using Chocolatey VS Code from within WSL Ubuntu在 WSL Ubuntu 中使用 Chocolatey VS Code
【发布时间】:2021-01-24 19:10:06
【问题描述】:

我无法让 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 这一切都很好。

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

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 无缝处理的代码。有谁知道为什么会发生这种情况/如何解决?

【问题讨论】:

    标签: bash ubuntu windows-subsystem-for-linux chocolatey


    【解决方案1】:

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

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

    如果没有,首先确保在 VSCode 中安装了“Remote - WSL”扩展(或者更好的是,包含 WSL 支持的“Remote Development”扩展包)。如果在那之后它仍然不存在,这里是应该存在于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。如上所述,它应该在您的路径中。

    【讨论】:

    • 你的意思是/bin/ 在 WSL 中吗?谢谢你,但我对你的意思有点困惑。你的意思是什么东西正在将一个名为 code 的 bash 脚本注入到你的 WSL 实例中吗?我找了/bin/code,但它不存在。你能告诉我code shell 脚本中有什么吗,真的很想知道(你可以把它添加到你上面的答案中,我会试试看吗?)。
    • 不在/bin 中,而是在VSCode 安装目录..\{VSCode}\bin 中——它只是VSCode 安装本身的一部分。不过,我已将脚本内容添加到答案中。但这取决于是否在正确的目录中(.exe 的下一级)。
    • 还更新了我的答案以澄清不是/bin/code而是../Microsoft VS Code/bin/code
    • 你说得对,它就在那里,我以前从未见过那个脚本,也没有第一次明白你的意思,所以我设置了alias code="\"/mnt/c/Program Files\Microsoft VS Code/bin/code"",现在这很好用(我不知道) t认为我需要PATH上的文件夹,我可以使用这样的别名)。 ?
    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 2022-07-19
    • 1970-01-01
    • 2018-11-03
    • 2020-04-18
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多