Skip to content
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

Allow installing yarn via corepack on Debian-based systems, too #1009

Merged
7 changes: 6 additions & 1 deletion src/node/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "node",
"version": "1.4.1",
"version": "1.5.0",
"name": "Node.js (via nvm), yarn and pnpm",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/node",
"description": "Installs Node.js, nvm, yarn, pnpm, and needed dependencies.",
Expand Down Expand Up @@ -36,6 +36,11 @@
],
"default": "latest",
"description": "Version of NVM to install."
},
"installYarnUsingApt": {
"type": "boolean",
"default": true,
"description": "On Debian & Ubuntu systems, you can choose whether to install Yarn globally via APT. If this option is set to false, Yarn will be set up via Corepack instead. This setting does not apply to other Linux distributions, where Yarn is always installed using Corepack (with a fallback to installation via NPM in case of an error)."
samruddhikhandale marked this conversation as resolved.
Show resolved Hide resolved
}
},
"customizations": {
Expand Down
8 changes: 5 additions & 3 deletions src/node/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export NODE_VERSION="${VERSION:-"lts"}"
export NVM_VERSION="${NVMVERSION:-"latest"}"
export NVM_DIR="${NVMINSTALLPATH:-"/usr/local/share/nvm"}"
INSTALL_TOOLS_FOR_NODE_GYP="${NODEGYPDEPENDENCIES:-true}"
export INSTALL_YARN_USING_APT="${INSTALLYARNUSINGAPT:-true}" # only concerns Debian-based systems

# Comma-separated list of node versions to be installed (with nvm)
# alongside NODE_VERSION, but not set as default.
Expand Down Expand Up @@ -188,7 +189,7 @@ find_version_from_git_tags() {
}

install_yarn() {
if [ "${ADJUSTED_ID}" = "debian" ]; then
if [ "${ADJUSTED_ID}" = "debian" ] && [ "${INSTALL_YARN_USING_APT}" = "true" ]; then
# for backward compatiblity with existing devcontainer features, install yarn
# via apt-get on Debian systems
if ! type yarn >/dev/null 2>&1; then
Expand All @@ -202,8 +203,9 @@ install_yarn() {
fi
else
local _ver=${1:-node}
# on non-debian systems, prefer corepack, fallback to npm based installation of yarn...
# Try to leverage corepack if possible
# on non-debian systems or if user opted not to use APT, prefer corepack
# Fallback to npm based installation of yarn.
# But try to leverage corepack if possible
# From https://yarnpkg.com:
# The preferred way to manage Yarn is by-project and through Corepack, a tool
# shipped by default with Node.js. Modern releases of Yarn aren't meant to be
Expand Down
20 changes: 20 additions & 0 deletions test/node/debian_yarn_from_corepack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
YARN_VERSION="4.3.0"

# Corepack provides shims for package managers like yarn. When yarn is invoked by the "yarn"
hoechenberger marked this conversation as resolved.
Show resolved Hide resolved
# command, corepack will interactively request permission to download the yarn binary. To
# avoid this interactive mode and download the binary automatically, we call "corepack use yarn"
# instead. Once that command completes, "yarn" can be used normally and in a non-interactive mode.
check "yarn shim location" bash -c ". /usr/local/share/nvm/nvm.sh && type yarn &> /dev/null"
check "download yarn" bash -c ". /usr/local/share/nvm/nvm.sh && corepack use yarn@${YARN_VERSION}"
check "yarn version" bash -c ". /usr/local/share/nvm/nvm.sh && yarn --version | grep ${YARN_VERSION}"

# Report result
reportResults
8 changes: 8 additions & 0 deletions test/node/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,13 @@
"version": "lts"
}
}
},
"debian_yarn_from_corepack": {
"image": "debian:11",
"features": {
"node": {
"installYarnUsingApt": false
}
}
}
}