Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 9d6dd83

Browse files
authored
fix(devcontainers-cli): add PNPM_HOME for pnpm package manager (#433)
PNPM requires `$PNPM_HOME` to be set to install packages. This PR is a follow-up on the one creating @devcontainers-cli module - to ensure PNPM_HOME is set, based on values set by Coder.
1 parent 201acb7 commit 9d6dd83

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

devcontainers-cli/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The devcontainers-cli module provides an easy way to install [`@devcontainers/cl
1616
```tf
1717
module "devcontainers-cli" {
1818
source = "registry.coder.com/modules/devcontainers-cli/coder"
19-
version = "1.0.0"
19+
version = "1.0.1"
2020
agent_id = coder_agent.example.id
2121
}
2222
```

devcontainers-cli/main.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const executeScriptInContainerWithPackageManager = async (
3030
await execContainer(id, [
3131
shell,
3232
"-c",
33-
"apk add nodejs npm && npm install -g pnpm",
33+
`wget -qO- https://get.pnpm.io/install.sh | ENV="$HOME/.shrc" SHELL="$(which sh)" sh -`,
3434
]);
3535
} else if (packageManager === "yarn") {
3636
await execContainer(id, [
@@ -86,7 +86,7 @@ describe("devcontainers-cli", async () => {
8686
expect(output.stdout[output.stdout.length - 1]).toEqual(
8787
"🥳 @devcontainers/cli has been installed into /usr/local/bin/devcontainer!",
8888
);
89-
});
89+
}, 15000);
9090

9191
it("installs devcontainers-cli with yarn", async () => {
9292
const state = await runTerraformApply(import.meta.dir, {
@@ -106,7 +106,7 @@ describe("devcontainers-cli", async () => {
106106
expect(output.stdout[output.stdout.length - 1]).toEqual(
107107
"🥳 @devcontainers/cli has been installed into /usr/local/bin/devcontainer!",
108108
);
109-
});
109+
}, 15000);
110110

111111
it("displays warning if docker is not installed", async () => {
112112
const state = await runTerraformApply(import.meta.dir, {
@@ -126,5 +126,5 @@ describe("devcontainers-cli", async () => {
126126
expect(output.stdout[output.stdout.length - 1]).toEqual(
127127
"🥳 @devcontainers/cli has been installed into /usr/local/bin/devcontainer!",
128128
);
129-
});
129+
}, 15000);
130130
});

devcontainers-cli/run.sh

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,45 @@ if ! command -v docker > /dev/null 2>&1; then
1212
fi
1313

1414
# Determine the package manager to use: npm, pnpm, or yarn
15-
if command -v pnpm > /dev/null 2>&1; then
16-
PACKAGE_MANAGER="pnpm"
17-
elif command -v yarn > /dev/null 2>&1; then
15+
if command -v yarn > /dev/null 2>&1; then
1816
PACKAGE_MANAGER="yarn"
1917
elif command -v npm > /dev/null 2>&1; then
2018
PACKAGE_MANAGER="npm"
19+
elif command -v pnpm > /dev/null 2>&1; then
20+
PACKAGE_MANAGER="pnpm"
2121
else
2222
echo "ERROR: No supported package manager (npm, pnpm, yarn) is installed. Please install one first." 1>&2
2323
exit 1
2424
fi
2525

26-
echo "Installing @devcontainers/cli using $PACKAGE_MANAGER..."
26+
install() {
27+
echo "Installing @devcontainers/cli using $PACKAGE_MANAGER..."
28+
if [ "$PACKAGE_MANAGER" = "npm" ]; then
29+
npm install -g @devcontainers/cli
30+
elif [ "$PACKAGE_MANAGER" = "pnpm" ]; then
31+
# Check if PNPM_HOME is set, if not, set it to the script's bin directory
32+
# pnpm needs this to be set to install binaries
33+
# coder agent ensures this part is part of the PATH
34+
# so that the devcontainer command is available
35+
if [ -z "$PNPM_HOME" ]; then
36+
PNPM_HOME="$CODER_SCRIPT_BIN_DIR"
37+
export M_HOME
38+
fi
39+
pnpm add -g @devcontainers/cli
40+
elif [ "$PACKAGE_MANAGER" = "yarn" ]; then
41+
yarn global add @devcontainers/cli
42+
fi
43+
}
44+
45+
if ! install; then
46+
echo "Failed to install @devcontainers/cli" >&2
47+
exit 1
48+
fi
2749

28-
# Install @devcontainers/cli using the selected package manager
29-
if [ "$PACKAGE_MANAGER" = "npm" ] || [ "$PACKAGE_MANAGER" = "pnpm" ]; then
30-
$PACKAGE_MANAGER install -g @devcontainers/cli \
31-
&& echo "🥳 @devcontainers/cli has been installed into $(which devcontainer)!"
32-
elif [ "$PACKAGE_MANAGER" = "yarn" ]; then
33-
$PACKAGE_MANAGER global add @devcontainers/cli \
34-
&& echo "🥳 @devcontainers/cli has been installed into $(which devcontainer)!"
50+
if ! command -v devcontainer > /dev/null 2>&1; then
51+
echo "Installation completed but 'devcontainer' command not found in PATH" >&2
52+
exit 1
3553
fi
3654

55+
echo "🥳 @devcontainers/cli has been installed into $(which devcontainer)!"
3756
exit 0

0 commit comments

Comments
 (0)