Skip to content

Commit 62f7f7f

Browse files
Allow installing yarn via corepack on Debian-based systems, too (#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 <[email protected]> * Update src/node/devcontainer-feature.json Co-authored-by: Samruddhi Khandale <[email protected]> * 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 <[email protected]>
1 parent 7953318 commit 62f7f7f

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

src/node/devcontainer-feature.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "node",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"name": "Node.js (via nvm), yarn and pnpm",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/node",
66
"description": "Installs Node.js, nvm, yarn, pnpm, and needed dependencies.",
@@ -36,6 +36,11 @@
3636
],
3737
"default": "latest",
3838
"description": "Version of NVM to install."
39+
},
40+
"installYarnUsingApt": {
41+
"type": "boolean",
42+
"default": true,
43+
"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."
3944
}
4045
},
4146
"customizations": {

src/node/install.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export NODE_VERSION="${VERSION:-"lts"}"
1111
export NVM_VERSION="${NVMVERSION:-"latest"}"
1212
export NVM_DIR="${NVMINSTALLPATH:-"/usr/local/share/nvm"}"
1313
INSTALL_TOOLS_FOR_NODE_GYP="${NODEGYPDEPENDENCIES:-true}"
14+
export INSTALL_YARN_USING_APT="${INSTALLYARNUSINGAPT:-true}" # only concerns Debian-based systems
1415

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

190191
install_yarn() {
191-
if [ "${ADJUSTED_ID}" = "debian" ]; then
192+
if [ "${ADJUSTED_ID}" = "debian" ] && [ "${INSTALL_YARN_USING_APT}" = "true" ]; then
192193
# for backward compatiblity with existing devcontainer features, install yarn
193194
# via apt-get on Debian systems
194195
if ! type yarn >/dev/null 2>&1; then
@@ -202,8 +203,9 @@ install_yarn() {
202203
fi
203204
else
204205
local _ver=${1:-node}
205-
# on non-debian systems, prefer corepack, fallback to npm based installation of yarn...
206-
# Try to leverage corepack if possible
206+
# on non-debian systems or if user opted not to use APT, prefer corepack
207+
# Fallback to npm based installation of yarn.
208+
# But try to leverage corepack if possible
207209
# From https://yarnpkg.com:
208210
# The preferred way to manage Yarn is by-project and through Corepack, a tool
209211
# shipped by default with Node.js. Modern releases of Yarn aren't meant to be
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
YARN_VERSION="4.3.0"
10+
11+
# Corepack provides shims for package managers like yarn. The first time yarn is invoked via the "yarn"
12+
# command, corepack will interactively request permission to download the yarn binary. To
13+
# avoid this interactive mode and download the binary automatically, we explicitly call "corepack use yarn"
14+
# instead (doesn't require user input). Once that command completes, "yarn" can be used in a non-interactive mode.
15+
check "yarn shim location" bash -c ". /usr/local/share/nvm/nvm.sh && type yarn &> /dev/null"
16+
check "download yarn" bash -c ". /usr/local/share/nvm/nvm.sh && corepack use yarn@${YARN_VERSION}"
17+
check "yarn version" bash -c ". /usr/local/share/nvm/nvm.sh && yarn --version | grep ${YARN_VERSION}"
18+
19+
# Report result
20+
reportResults

test/node/scenarios.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,13 @@
185185
"version": "lts"
186186
}
187187
}
188+
},
189+
"debian_yarn_from_corepack": {
190+
"image": "debian:11",
191+
"features": {
192+
"node": {
193+
"installYarnUsingApt": false
194+
}
195+
}
188196
}
189197
}

0 commit comments

Comments
 (0)