From 11ffff24298457601f31dabd959ae3bc73a984fe Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 4 Aug 2025 09:11:59 +0000 Subject: [PATCH 1/5] fix(devcontainers-cli): allow yarn to install when `packageManager` not `yarn` On our dogfood box, 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 the script 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 | 5 ++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/registry/coder/modules/devcontainers-cli/README.md b/registry/coder/modules/devcontainers-cli/README.md index ed2a4cb17..9622cceb0 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 6cfe4d04e..5a4d34e8e 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 index f7bf852c6..4f0600c0b 100644 --- a/registry/coder/modules/devcontainers-cli/run.sh +++ b/registry/coder/modules/devcontainers-cli/run.sh @@ -38,7 +38,10 @@ install() { fi pnpm add -g @devcontainers/cli elif [ "$PACKAGE_MANAGER" = "yarn" ]; then - yarn global add @devcontainers/cli --prefix "$(dirname "$CODER_SCRIPT_BIN_DIR")" + # 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 `yarn`. When this happens, `yarn global add` will fail to install. + cd "$CODER_SCRIPT_DATA_DIR" && yarn global add @devcontainers/cli --prefix "$(dirname "$CODER_SCRIPT_BIN_DIR")" fi } From 89f57b67b7686613d91367239b126ab39825ee8c Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 4 Aug 2025 09:55:22 +0000 Subject: [PATCH 2/5] chore: drive by fixes --- registry/coder/modules/devcontainers-cli/run.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) mode change 100644 => 100755 registry/coder/modules/devcontainers-cli/run.sh 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 4f0600c0b..f1840066b --- a/registry/coder/modules/devcontainers-cli/run.sh +++ b/registry/coder/modules/devcontainers-cli/run.sh @@ -34,14 +34,16 @@ 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 # 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 `yarn`. When this happens, `yarn global add` will fail to install. - cd "$CODER_SCRIPT_DATA_DIR" && yarn global add @devcontainers/cli --prefix "$(dirname "$CODER_SCRIPT_BIN_DIR")" + pushd "$CODER_SCRIPT_DATA_DIR" && \ + yarn global add @devcontainers/cli --prefix "$(dirname "$CODER_SCRIPT_BIN_DIR")" && \ + popd fi } From ce2529207c7bd1273bf86555f39b3173c83f26ec Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 4 Aug 2025 10:01:34 +0000 Subject: [PATCH 3/5] fix: `sh` -> `bash` --- registry/coder/modules/devcontainers-cli/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/coder/modules/devcontainers-cli/run.sh b/registry/coder/modules/devcontainers-cli/run.sh index f1840066b..e71a3d276 100755 --- a/registry/coder/modules/devcontainers-cli/run.sh +++ b/registry/coder/modules/devcontainers-cli/run.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash # If @devcontainers/cli is already installed, we can skip if command -v devcontainer >/dev/null 2>&1; then From 881635126ef9dd2ebe084c736b29a99f03ef8a2a Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 4 Aug 2025 10:44:07 +0000 Subject: [PATCH 4/5] test: install bash and execute with bash --- registry/coder/modules/devcontainers-cli/main.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/registry/coder/modules/devcontainers-cli/main.test.ts b/registry/coder/modules/devcontainers-cli/main.test.ts index 5a4d34e8e..7bbba6704 100644 --- a/registry/coder/modules/devcontainers-cli/main.test.ts +++ b/registry/coder/modules/devcontainers-cli/main.test.ts @@ -23,6 +23,9 @@ const executeScriptInContainerWithPackageManager = async ( const instance = findResourceInstance(state, "coder_script"); const id = await runContainer(image); + // Install bash + await execContainer(id, [shell, "-c", "apk add bash"]); + // Install the specified package manager if (packageManager === "npm") { await execContainer(id, [shell, "-c", "apk add nodejs npm"]); @@ -49,7 +52,7 @@ const executeScriptInContainerWithPackageManager = async ( const resp = await execContainer( id, - [shell, "-c", instance.script], + ["bash", "-c", instance.script], [ "--env", "CODER_SCRIPT_BIN_DIR=/tmp/coder-script-data/bin", From f08f23ebc53339a9f35e385c43e6cf2b61612413 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 4 Aug 2025 11:22:53 +0000 Subject: [PATCH 5/5] chore: revert back to `sh` instead of `bash`, `cd` at top-level --- .../coder/modules/devcontainers-cli/main.test.ts | 5 +---- registry/coder/modules/devcontainers-cli/run.sh | 15 ++++++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/registry/coder/modules/devcontainers-cli/main.test.ts b/registry/coder/modules/devcontainers-cli/main.test.ts index 7bbba6704..5a4d34e8e 100644 --- a/registry/coder/modules/devcontainers-cli/main.test.ts +++ b/registry/coder/modules/devcontainers-cli/main.test.ts @@ -23,9 +23,6 @@ const executeScriptInContainerWithPackageManager = async ( const instance = findResourceInstance(state, "coder_script"); const id = await runContainer(image); - // Install bash - await execContainer(id, [shell, "-c", "apk add bash"]); - // Install the specified package manager if (packageManager === "npm") { await execContainer(id, [shell, "-c", "apk add nodejs npm"]); @@ -52,7 +49,7 @@ const executeScriptInContainerWithPackageManager = async ( const resp = await execContainer( id, - ["bash", "-c", instance.script], + [shell, "-c", instance.script], [ "--env", "CODER_SCRIPT_BIN_DIR=/tmp/coder-script-data/bin", diff --git a/registry/coder/modules/devcontainers-cli/run.sh b/registry/coder/modules/devcontainers-cli/run.sh index e71a3d276..7be78616d 100755 --- a/registry/coder/modules/devcontainers-cli/run.sh +++ b/registry/coder/modules/devcontainers-cli/run.sh @@ -1,4 +1,10 @@ -#!/usr/bin/env bash +#!/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 @@ -38,12 +44,7 @@ install() { fi pnpm add -g @devcontainers/cli elif [ "$PACKAGE_MANAGER" = "yarn" ]; then - # 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 `yarn`. When this happens, `yarn global add` will fail to install. - pushd "$CODER_SCRIPT_DATA_DIR" && \ - yarn global add @devcontainers/cli --prefix "$(dirname "$CODER_SCRIPT_BIN_DIR")" && \ - popd + yarn global add @devcontainers/cli --prefix "$(dirname "$CODER_SCRIPT_BIN_DIR")" fi }