microsoft / microsoft/vscode-remote-release

Cannot run devcontainer-cli outside of its command folder (Possible fix)

Open
#8,738 5 comments 0 reactions 1 assignee View on GitHub

@chrmarti is already working on this.

Since Jun 19, 2023.

bug containers
Dominant language
Dockerfile
Stars
4.2k
Forks
470
Avg merge
1d 1h
Merged PRs (30d)
1

Description

I can't say when it started or if it was after a devcontainer update or somethig else but devcontainer cli command stopped working from WSL.
Now i'm testing it on version 0.295.0 but previous versions or pre-release still do not work.

The command devcontainer --help (or other commands) works fine inside Windows (in cmd) but hangs indefinitely in wsl console.
I debugged the script and it works fine in WLS if i run inside devcontainer command folder (if i cd into '/mnt/c/Users//AppData/Roaming/Code/User/globalStorage/ms-vscode-remote.remote-containers/cli-bin/' folder that i got with which devcontainer).

I managed to make it work by adding a cd $basedir on line 47 (two lines before exit $? in wsl part of the final if)

I will write here my temporary, please do add a proper fix to this problem.

My temporary fix

#!/bin/sh
script_file=$0
while [ -h "$script_file" ]; do
        script_file=$(readlink $script_file)
done
basedir=$(dirname "$(echo "$script_file" | sed -e 's,\\,/,g')")

# This file is placed in a stable (version-independent) location
# and forwards to the currently installed version

# WSL detection borrowed from VS Code
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
                IN_WSL=true
        fi
fi
VSCODE_PATH=$(cat "$basedir/vscode-path")
DEVCONTAINER_CLI_PATH=$script_file
REMOTE_CONTAINERS_PATH=
if [ -f "$basedir/remote-containers-path" ]; then
        REMOTE_CONTAINERS_PATH=$(cat "$basedir/remote-containers-path")
        if [ $IN_WSL = true ]; then
                REMOTE_CONTAINERS_PATH=$(wslpath -u $REMOTE_CONTAINERS_PATH)
        fi
        if [ ! -f "$REMOTE_CONTAINERS_PATH/dev-containers-user-cli/cli.js" ]; then
                REMOTE_CONTAINERS_PATH=
        fi
fi

if [ -z "$REMOTE_CONTAINERS_PATH" ]; then
        echo "Failed to determine Dev Containers path"
        exit 1
fi

if [ $IN_WSL = true ]; then
        export WSLENV="ELECTRON_RUN_AS_NODE/w:DEVCONTAINER_CLI_PATH/p:IN_WSL:WSL_DISTRO_NAME:$WSLENV"
        CLI=$(wslpath -m "$REMOTE_CONTAINERS_PATH/dev-containers-user-cli/cli.js")
        ELECTRON=$(wslpath -u "$VSCODE_PATH")

        # TODO - piping to `cat` is a temporary workaround for the CRLF->LF conversion
        # that occurs when calling Windows Electron binary from WSL
        cd $basedir
        IN_WSL=$IN_WSL DEVCONTAINER_CLI_PATH="$DEVCONTAINER_CLI_PATH" ELECTRON_RUN_AS_NODE=1 "$ELECTRON" --ms-enable-electron-run-as-node "$CLI" "$@" | cat
        exit $?
else
        ELECTRON="$VSCODE_PATH"
        CLI=$REMOTE_CONTAINERS_PATH/dev-containers-user-cli/cli.js
        DEVCONTAINER_CLI_PATH="$DEVCONTAINER_CLI_PATH" ELECTRON_RUN_AS_NODE=1 "$ELECTRON" --ms-enable-electron-run-as-node "$CLI" "$@"
        exit $?
fi

Original file

#!/bin/sh
script_file=$0
while [ -h "$script_file" ]; do
        script_file=$(readlink $script_file)
done
basedir=$(dirname "$(echo "$script_file" | sed -e 's,\\,/,g')")

# This file is placed in a stable (version-independent) location
# and forwards to the currently installed version

# WSL detection borrowed from VS Code
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
                IN_WSL=true
        fi
fi
VSCODE_PATH=$(cat "$basedir/vscode-path")
DEVCONTAINER_CLI_PATH=$script_file
REMOTE_CONTAINERS_PATH=
if [ -f "$basedir/remote-containers-path" ]; then
        REMOTE_CONTAINERS_PATH=$(cat "$basedir/remote-containers-path")
        if [ $IN_WSL = true ]; then
                REMOTE_CONTAINERS_PATH=$(wslpath -u $REMOTE_CONTAINERS_PATH)
        fi
        if [ ! -f "$REMOTE_CONTAINERS_PATH/dev-containers-user-cli/cli.js" ]; then
                REMOTE_CONTAINERS_PATH=
        fi
fi

if [ -z "$REMOTE_CONTAINERS_PATH" ]; then
        echo "Failed to determine Dev Containers path"
        exit 1
fi

if [ $IN_WSL = true ]; then
        export WSLENV="ELECTRON_RUN_AS_NODE/w:DEVCONTAINER_CLI_PATH/p:IN_WSL:WSL_DISTRO_NAME:$WSLENV"
        CLI=$(wslpath -m "$REMOTE_CONTAINERS_PATH/dev-containers-user-cli/cli.js")
        ELECTRON=$(wslpath -u "$VSCODE_PATH")

        # TODO - piping to `cat` is a temporary workaround for the CRLF->LF conversion
        # that occurs when calling Windows Electron binary from WSL
        IN_WSL=$IN_WSL DEVCONTAINER_CLI_PATH="$DEVCONTAINER_CLI_PATH" ELECTRON_RUN_AS_NODE=1 "$ELECTRON" --ms-enable-electron-run-as-node "$CLI" "$@" | cat
        exit $?
else
        ELECTRON="$VSCODE_PATH"
        CLI=$REMOTE_CONTAINERS_PATH/dev-containers-user-cli/cli.js
        DEVCONTAINER_CLI_PATH="$DEVCONTAINER_CLI_PATH" ELECTRON_RUN_AS_NODE=1 "$ELECTRON" --ms-enable-electron-run-as-node "$CLI" "$@"
        exit $?
fi

Note: Opening the project with VsCode and then running "Open in devcontainer" always work but i do prefer running the command directly from the console as it's faster and i have a LOT of devcontainer projects.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.