Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(devcontainers-cli): add PNPM_HOME for pnpm package manager #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 18, 2025

Conversation

defelmnq
Copy link
Contributor

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.

@defelmnq defelmnq self-assigned this Apr 17, 2025
@@ -30,7 +30,7 @@ const executeScriptInContainerWithPackageManager = async (
await execContainer(id, [
shell,
"-c",
"apk add nodejs npm && npm install -g pnpm",
`wget -qO- https://get.pnpm.io/install.sh | ENV="$HOME/.shrc" SHELL="$(which sh)" sh -`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely testing purpose, done to not rely on NPM.

@@ -12,12 +12,12 @@ if ! command -v docker > /dev/null 2>&1; then
fi

# Determine the package manager to use: npm, pnpm, or yarn
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the ordering to use PNPM as a last option, if NPM is installed too.

&& echo "🥳 @devcontainers/cli has been installed into $(which devcontainer)!"
elif [ "$PACKAGE_MANAGER" = "pnpm" ]; then
# if PNPM_HOME is not set, set it to the bin directory of the script
if [ -z "$PNPM_HOME" ]; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main part of the fix, adding $PNPM_HOME if not set yet.

@defelmnq defelmnq requested a review from mafredri April 17, 2025 14:04
Comment on lines 33 to 36
# if PNPM_HOME is not set, set it to the bin directory of the script
if [ -z "$PNPM_HOME" ]; then
export PNPM_HOME="$CODER_SCRIPT_BIN_DIR"
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my education, how does that work? What is CODER_SCRIPT_BIN_DIR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically /tmp/coder-script-data/bin, a place where all scripts can place binaries and they will be part of PATH (guaranteed by agent).

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggesting a minor improvement to the script but otherwise looks alright.

$PACKAGE_MANAGER install -g @devcontainers/cli \
&& echo "🥳 @devcontainers/cli has been installed into $(which devcontainer)!"
elif [ "$PACKAGE_MANAGER" = "pnpm" ]; then
# if PNPM_HOME is not set, set it to the bin directory of the script
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment could better explain why we need to do this, for posterity.

Comment on lines 33 to 36
# if PNPM_HOME is not set, set it to the bin directory of the script
if [ -z "$PNPM_HOME" ]; then
export PNPM_HOME="$CODER_SCRIPT_BIN_DIR"
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically /tmp/coder-script-data/bin, a place where all scripts can place binaries and they will be part of PATH (guaranteed by agent).

export PNPM_HOME="$CODER_SCRIPT_BIN_DIR"
fi
$PACKAGE_MANAGER add -g @devcontainers/cli \
&& echo "🥳 @devcontainers/cli has been installed into $(which devcontainer)!"
elif [ "$PACKAGE_MANAGER" = "yarn" ]; then
$PACKAGE_MANAGER global add @devcontainers/cli \
&& echo "🥳 @devcontainers/cli has been installed into $(which devcontainer)!"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to factor out this repetition, might I suggest the following?

install() {
  if [ "$PACKAGE_MANAGER" = "npm" ]; then
    npm install -g @devcontainers/cli
  elif [ "$PACKAGE_MANAGER" = "pnpm" ]; then
    # <reason we set this>
    if [ -z "$PNPM_HOME" ]; then
      PNPM_HOME="$CODER_SCRIPT_BIN_DIR"
      export PNPM_HOME
    fi
    pnpm add -g @devcontainers/cli
  elif [ "$PACKAGE_MANAGER" = "yarn" ]; then
    yarn global add @devcontainers/cli
  fi
}

if ! install; then
  echo "Failed to install @devcontainers/cli" >&2
  exit 1
fi

if ! command -v devcontainer > /dev/null 2>&1; then
  echo "Installation completed but 'devcontainer' command not found in PATH" >&2
  exit 1
fi

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

Adds a bit more detail and validates the binary is present.

@matifali
Copy link
Member

Make version 1.0.1, please, in the README.md

@matifali
Copy link
Member

@mafredri for version constraints, I am thinking of introducing labels like version: patch, version: minor, and version: major, and a workflow that checks if the README version has been bumped to match this constraint from the label. This workflow would keep failing unless the required version bump is made.

@defelmnq defelmnq requested a review from matifali April 18, 2025 17:02
@defelmnq defelmnq merged commit 9d6dd83 into main Apr 18, 2025
4 checks passed
@defelmnq defelmnq deleted the vvi-16432-fix branch April 18, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants