From 258591833f0514544d6d434d62ad3d476504937a Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 4 Aug 2025 13:00:13 +0100 Subject: [PATCH] fix(devcontainers-cli): allow yarn to install when `packageManager` not `yarn` (#287) On our dogfood workspaces, we fail to install `@devcontainers/cli` with `yarn` because our agent directory `/home/coder/coder` contains a `package.json` with `packageManager` being set to `pnpm`. This change instead ensures to run `yarn global add` inside the `$CODER_SCRIPT_DATA_DIR` so that we don't read a `package.json` and cause things to break. --- registry/coder/modules/devcontainers-cli/README.md | 2 +- registry/coder/modules/devcontainers-cli/main.test.ts | 4 ++++ registry/coder/modules/devcontainers-cli/run.sh | 8 +++++++- 3 files changed, 12 insertions(+), 2 deletions(-) mode change 100644 => 100755 registry/coder/modules/devcontainers-cli/run.sh diff --git a/registry/coder/modules/devcontainers-cli/README.md b/registry/coder/modules/devcontainers-cli/README.md index ed2a4cb1..9622cceb 100644 --- a/registry/coder/modules/devcontainers-cli/README.md +++ b/registry/coder/modules/devcontainers-cli/README.md @@ -15,7 +15,7 @@ The devcontainers-cli module provides an easy way to install [`@devcontainers/cl ```tf module "devcontainers-cli" { source = "registry.coder.com/coder/devcontainers-cli/coder" - version = "1.0.31" + version = "1.0.32" agent_id = coder_agent.example.id } ``` diff --git a/registry/coder/modules/devcontainers-cli/main.test.ts b/registry/coder/modules/devcontainers-cli/main.test.ts index 6cfe4d04..5a4d34e8 100644 --- a/registry/coder/modules/devcontainers-cli/main.test.ts +++ b/registry/coder/modules/devcontainers-cli/main.test.ts @@ -45,6 +45,8 @@ const executeScriptInContainerWithPackageManager = async ( console.log(path); + await execContainer(id, [shell, "-c", "mkdir -p /tmp/coder-script-data"]); + const resp = await execContainer( id, [shell, "-c", instance.script], @@ -52,6 +54,8 @@ const executeScriptInContainerWithPackageManager = async ( "--env", "CODER_SCRIPT_BIN_DIR=/tmp/coder-script-data/bin", "--env", + "CODER_SCRIPT_DATA_DIR=/tmp/coder-script-data", + "--env", `PATH=${path}:/tmp/coder-script-data/bin`, ], ); diff --git a/registry/coder/modules/devcontainers-cli/run.sh b/registry/coder/modules/devcontainers-cli/run.sh old mode 100644 new mode 100755 index f7bf852c..7be78616 --- a/registry/coder/modules/devcontainers-cli/run.sh +++ b/registry/coder/modules/devcontainers-cli/run.sh @@ -1,5 +1,11 @@ #!/usr/bin/env sh +# We want to cd into `$CODER_SCRIPT_DATA_DIR` as the current directory +# might contain a `package.json` with `packageManager` set to something +# other than the detected package manager. When this happens, it can +# cause the installation to fail. +cd "$CODER_SCRIPT_DATA_DIR" + # If @devcontainers/cli is already installed, we can skip if command -v devcontainer >/dev/null 2>&1; then echo "🥳 @devcontainers/cli is already installed into $(which devcontainer)!" @@ -34,7 +40,7 @@ install() { # so that the devcontainer command is available if [ -z "$PNPM_HOME" ]; then PNPM_HOME="$CODER_SCRIPT_BIN_DIR" - export M_HOME + export PNPM_HOME fi pnpm add -g @devcontainers/cli elif [ "$PACKAGE_MANAGER" = "yarn" ]; then