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.
This commit is contained in:
Danielle Maywood 2025-08-04 13:00:13 +01:00 committed by GitHub
parent 3efc22c589
commit 258591833f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 2 deletions

View File

@ -15,7 +15,7 @@ The devcontainers-cli module provides an easy way to install [`@devcontainers/cl
```tf ```tf
module "devcontainers-cli" { module "devcontainers-cli" {
source = "registry.coder.com/coder/devcontainers-cli/coder" source = "registry.coder.com/coder/devcontainers-cli/coder"
version = "1.0.31" version = "1.0.32"
agent_id = coder_agent.example.id agent_id = coder_agent.example.id
} }
``` ```

View File

@ -45,6 +45,8 @@ const executeScriptInContainerWithPackageManager = async (
console.log(path); console.log(path);
await execContainer(id, [shell, "-c", "mkdir -p /tmp/coder-script-data"]);
const resp = await execContainer( const resp = await execContainer(
id, id,
[shell, "-c", instance.script], [shell, "-c", instance.script],
@ -52,6 +54,8 @@ const executeScriptInContainerWithPackageManager = async (
"--env", "--env",
"CODER_SCRIPT_BIN_DIR=/tmp/coder-script-data/bin", "CODER_SCRIPT_BIN_DIR=/tmp/coder-script-data/bin",
"--env", "--env",
"CODER_SCRIPT_DATA_DIR=/tmp/coder-script-data",
"--env",
`PATH=${path}:/tmp/coder-script-data/bin`, `PATH=${path}:/tmp/coder-script-data/bin`,
], ],
); );

8
registry/coder/modules/devcontainers-cli/run.sh Normal file → Executable file
View File

@ -1,5 +1,11 @@
#!/usr/bin/env sh #!/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 @devcontainers/cli is already installed, we can skip
if command -v devcontainer >/dev/null 2>&1; then if command -v devcontainer >/dev/null 2>&1; then
echo "🥳 @devcontainers/cli is already installed into $(which devcontainer)!" echo "🥳 @devcontainers/cli is already installed into $(which devcontainer)!"
@ -34,7 +40,7 @@ install() {
# so that the devcontainer command is available # so that the devcontainer command is available
if [ -z "$PNPM_HOME" ]; then if [ -z "$PNPM_HOME" ]; then
PNPM_HOME="$CODER_SCRIPT_BIN_DIR" PNPM_HOME="$CODER_SCRIPT_BIN_DIR"
export M_HOME export PNPM_HOME
fi fi
pnpm add -g @devcontainers/cli pnpm add -g @devcontainers/cli
elif [ "$PACKAGE_MANAGER" = "yarn" ]; then elif [ "$PACKAGE_MANAGER" = "yarn" ]; then