Search code examples
visual-studio-codevscode-devcontainer

How to get VS Code devcontainer to execute bash command?


I'm trying to build the Matter (https://github.com/project-chip/connectedhomeip) project from VS Code using devcontainer on a Windows 11 PC (with WSL and Docker Desktop installed).

The problem is that I'm getting this error when trying to "Reopen in Container" for this project from VS Code:

Running the initializeCommand from devcontainer.json...

[10282 ms] Start: Run: C:\Windows\system32\cmd.exe /c .devcontainer/build.sh --tag matter-dev-environment:local --version 22
'.devcontainer' is not recognized as an internal or external command,
operable program or batch file.

It looks like the problem might be that it's using cmd.exe and not the bash shell to execute the command.

How can I get the devcontainer.json to launch the commands in a bash shell, rather than using cmd.exe?


Solution

  • The official documentation for building with VSCode describes how to set the shell used for initializerCommand, but it seems outdated:

    Update your Visual Studio Code settings as documented here: https://code.visualstudio.com/docs/editor/integrated-terminal#_configuration to use Bash on Ubuntu (on Windows) eg: "terminal.integrated.shell.windows": "C:\Windows\System32\bash.exe"

    The terminal.integrated.shell.windows setting is no longer valid. I tried using the alternatives with no success:

    "terminal.integrated.profiles.windows": {
        "bash": {
            "path": "C:\\Windows\\System32\\bash.exe"
        }
    },    
    "terminal.integrated.defaultProfile.windows": "bash"
    

    and

    "terminal.integrated.automationProfile.windows": {
        "path": "C:\\Windows\\System32\\bash.exe"
    },
    

    For both, initializerCommand still uses cmd. The only working solution I came up with is to edit .devcontainer\devcontainer.json manually by adding bash to the command:

    "initializeCommand": "bash .devcontainer/build.sh --tag matter-dev-environment:local --version 22"

    Although this is not the best way, because you have to change the upstream configuration, it seems to be the only solution for now. Remember to follow the steps in the official documentation, bash is only available for Windows when installing Ubuntu for WSL, and you have to clone the repository using LF instead of CLRF.