Skip to content

Commit

Permalink
Allow installing yarn via corepack on Debian-based systems, too (#…
Browse files Browse the repository at this point in the history
…1009)

* Allow installing `yarn` via `corepack` on Debian-based systems, too

The default behavior remains unchanged, but users can now opt-out from the APT-based installation to get the latest `yarn` via `corepack`.

Closes #1004

* Update src/node/devcontainer-feature.json

Co-authored-by: Samruddhi Khandale <samruddhikhandale@github.com>

* Update src/node/devcontainer-feature.json

Co-authored-by: Samruddhi Khandale <samruddhikhandale@github.com>

* Rename variable

* Expand comment

* Add test scenario

* Improve tests

* Update test/node/debian_yarn_from_corepack.sh

* Betetr comments

* Update src/node/devcontainer-feature.json

---------

Co-authored-by: Samruddhi Khandale <samruddhikhandale@github.com>
  • Loading branch information
hoechenberger and samruddhikhandale authored Jun 20, 2024
1 parent 7953318 commit 62f7f7f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
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 and Ubuntu systems, you have the option to install Yarn globally via APT. If you choose not to use this option, Yarn will be set up using Corepack instead. This choice is specific to Debian and Ubuntu; for other Linux distributions, Yarn is always installed using Corepack, with a fallback to installation via NPM if an error occurs."
}
},
"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. The first time yarn is invoked via the "yarn"
# command, corepack will interactively request permission to download the yarn binary. To
# avoid this interactive mode and download the binary automatically, we explicitly call "corepack use yarn"
# instead (doesn't require user input). Once that command completes, "yarn" can be used 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
}
}
}
}

0 comments on commit 62f7f7f

Please sign in to comment.