Skip to content

Commit

Permalink
Improved CI package manager detection (#1313)
Browse files Browse the repository at this point in the history
* Improved CI package manager detection

The previous method for detecting package managers works well when `yarn`, `pnpm`, or `npm` binaries are installed in the traditional way but fails when `corepack` shims are present. This PR changes the detection logic to use package metadata when determining which package manager to use.
  • Loading branch information
colincasey authored Sep 4, 2024
1 parent 15350cd commit c40573d
Show file tree
Hide file tree
Showing 25 changed files with 1,121 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- Added Node.js version 22.8.0.
- Fixed issue with auto-detection during Heroku CI runs. ([#1313](https://github.com/heroku/heroku-buildpack-nodejs/pull/1313))

## [v263] - 2024-08-27

Expand Down
1 change: 1 addition & 0 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ source "$BP_DIR/lib/yarn-2.sh"
export PATH="$BUILD_DIR/.heroku/node/bin:$BUILD_DIR/.heroku/yarn/bin":$PATH

export COREPACK_HOME="$BUILD_DIR/.heroku/corepack"
export COREPACK_ENABLE_DOWNLOAD_PROMPT=0

LOG_FILE=$(mktemp -t node-build-log.XXXXX)
echo "" > "$LOG_FILE"
Expand Down
26 changes: 24 additions & 2 deletions bin/test
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
#!/usr/bin/env bash

BUILD_DIR=${1:-}
if pnpm --version > /dev/null 2>&1; then

read_package_json() {
local key="$1"
# shellcheck disable=SC2002
cat "$BUILD_DIR/package.json" | jq -c -M --raw-output "$key // \"\"" || return 1
}

which_tool() {
yarn_engine=$(read_package_json ".engines.yarn")
pnpm_engine=$(read_package_json ".engines.pnpm")
package_manager=$(read_package_json ".packageManager")
if [ -n "$pnpm_engine" ] || [[ "$package_manager" == pnpm* ]]; then
echo "pnpm"
elif [ -n "$yarn_engine" ] || [[ "$package_manager" == yarn* ]]; then
echo "yarn"
else
echo "npm"
fi
}

tool=$(which_tool)

if [[ "$tool" == "pnpm" ]]; then
cd "$BUILD_DIR" && pnpm test
elif yarn --version > /dev/null 2>&1; then
elif [[ "$tool" == "yarn" ]]; then
cd "$BUILD_DIR" && yarn test
else
cd "$BUILD_DIR" && npm test
Expand Down
2 changes: 2 additions & 0 deletions ci-profile/nodejs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
export PATH="$HOME/.heroku/node/bin:$HOME/.heroku/yarn/bin:$PATH:$HOME/bin:$HOME/node_modules/.bin"
export NODE_HOME="$HOME/.heroku/node"
export NODE_ENV=${NODE_ENV:-test}
export COREPACK_HOME="$BUILD_DIR/.heroku/corepack"
export COREPACK_ENABLE_DOWNLOAD_PROMPT=0
2 changes: 1 addition & 1 deletion profile/nodejs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export PATH="$HOME/.heroku/node/bin:$HOME/.heroku/yarn/bin:$PATH:$HOME/bin:$HOME
export NODE_HOME="$HOME/.heroku/node"
export NODE_ENV=${NODE_ENV:-production}
export COREPACK_HOME="$HOME/.heroku/corepack"

export COREPACK_ENABLE_DOWNLOAD_PROMPT=0

# If the user has opted into the feature
if [[ -n "$HEROKU_METRICS_URL" ]] && \
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/test-pnpm-with-corepack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test-pnpm-with-corepack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"pnpm test run\""
},
"keywords": [],
"author": "",
"license": "ISC",
"packageManager": "[email protected]+sha512.60c18acd138bff695d339be6ad13f7e936eea6745660d4cc4a776d5247c540d0edee1a563695c183a66eb917ef88f2b4feb1fc25f32a7adcadc7aaf3438e99c1"
}
9 changes: 9 additions & 0 deletions test/fixtures/test-pnpm-with-corepack/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/fixtures/test-pnpm-with-engine/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "test-pnpm-with-engine",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"pnpm test run\""
},
"keywords": [],
"author": "",
"license": "ISC",
"engines": {
"pnpm": "9.9.0"
}
}
9 changes: 9 additions & 0 deletions test/fixtures/test-pnpm-with-engine/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/fixtures/test-yarn-with-corepack/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions test/fixtures/test-yarn-with-corepack/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.yarn/** linguist-vendored
/.yarn/releases/* binary
/.yarn/plugins/**/* binary
/.pnp.* binary linguist-generated
13 changes: 13 additions & 0 deletions test/fixtures/test-yarn-with-corepack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Swap the comments on the following lines if you wish to use zero-installs
# In that case, don't forget to run `yarn config set enableGlobalCache false`!
# Documentation here: https://yarnpkg.com/features/caching#zero-installs

#!.yarn/cache
.pnp.*
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/test-yarn-with-corepack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# test-yarn-with-corepack
7 changes: 7 additions & 0 deletions test/fixtures/test-yarn-with-corepack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "test-yarn-with-corepack",
"packageManager": "[email protected]+sha512.f825273d0689cc9ead3259c14998037662f1dcd06912637b21a450e8da7cfeb4b1965bbee73d16927baa1201054126bc385c6f43ff4aa705c8631d26e12460f1",
"scripts": {
"test": "echo \"yarn test run\""
}
}
12 changes: 12 additions & 0 deletions test/fixtures/test-yarn-with-corepack/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
cacheKey: 10c0

"test-yarn-with-corepack@workspace:.":
version: 0.0.0-use.local
resolution: "test-yarn-with-corepack@workspace:."
languageName: unknown
linkType: soft
10 changes: 10 additions & 0 deletions test/fixtures/test-yarn-with-engine/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions test/fixtures/test-yarn-with-engine/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.yarn/** linguist-vendored
/.yarn/releases/* binary
/.yarn/plugins/**/* binary
/.pnp.* binary linguist-generated
13 changes: 13 additions & 0 deletions test/fixtures/test-yarn-with-engine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Swap the comments on the following lines if you wish to use zero-installs
# In that case, don't forget to run `yarn config set enableGlobalCache false`!
# Documentation here: https://yarnpkg.com/features/caching#zero-installs

#!.yarn/cache
.pnp.*
925 changes: 925 additions & 0 deletions test/fixtures/test-yarn-with-engine/.yarn/releases/yarn-4.4.1.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/fixtures/test-yarn-with-engine/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-4.4.1.cjs
1 change: 1 addition & 0 deletions test/fixtures/test-yarn-with-engine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# test-yarn-with-engine
9 changes: 9 additions & 0 deletions test/fixtures/test-yarn-with-engine/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "test-yarn-with-corepack",
"scripts": {
"test": "echo \"yarn test run\""
},
"engines": {
"yarn": "4.4.1"
}
}
12 changes: 12 additions & 0 deletions test/fixtures/test-yarn-with-engine/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
cacheKey: 10c0

"test-yarn-with-corepack@workspace:.":
version: 0.0.0-use.local
resolution: "test-yarn-with-corepack@workspace:."
languageName: unknown
linkType: soft
25 changes: 24 additions & 1 deletion test/run
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,30 @@ testPnpmWorkspaceBinaries() {
testWarningNode_22_5_0() {
compile "node-22.5.0-warning"
assertCaptured "Issues with Node.js v22.5.0"
cat "$STD_OUT"
assertCapturedSuccess
}

testExecWithPnpmAndCorepack() {
compileTest "test-pnpm-with-corepack"
assertCaptured "pnpm test run"
assertCapturedSuccess
}

testExecWithPnpmAndEngine() {
compileTest "test-pnpm-with-engine"
assertCaptured "pnpm test run"
assertCapturedSuccess
}

testExecWithYarnAndCorepack() {
compileTest "test-yarn-with-corepack"
assertCaptured "yarn test run"
assertCapturedSuccess
}

testExecWithYarnAndEngine() {
compileTest "test-yarn-with-engine"
assertCaptured "yarn test run"
assertCapturedSuccess
}

Expand Down

0 comments on commit c40573d

Please sign in to comment.