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

Add checks for Yarn 2 #759

Merged
merged 5 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add warning for Yarn 2 usage, warn user of ignoring engine from package.json ([#761](https://github.com/heroku/heroku-buildpack-nodejs/pull/761))
- Add warning for if .yarnrc file exists when using Yarn 2 ([#768](https://github.com/heroku/heroku-buildpack-nodejs/pull/768))
- Add warning for if .npmrc file exists when using Yarn 2 ([#764](https://github.com/heroku/heroku-buildpack-nodejs/pull/764))
- Add checks for Yarn 2 ([#759](https://github.com/heroku/heroku-buildpack-nodejs/pull/759))

## master
- Update Travis badge to `master` and other changes in README ([#753](https://github.com/heroku/heroku-buildpack-nodejs/pull/753))
Expand Down
20 changes: 19 additions & 1 deletion bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,36 @@ create_build_env
[ ! "$NPM_CONFIG_CACHE" ] && NPM_CONFIG_CACHE=$(mktemp -d -t npmcache.XXXXX)
export YARN_CACHE_FOLDER NPM_CONFIG_CACHE

### Configure vendored package manager
export YARN2_PATH

if [[ "$YARN2" == "true" ]]; then
if [[ "$NODE_MODULES_CACHE" == "true" ]]; then
warn "
WARNING- You are using Yarn2. The buildpack won't cache, because Yarn 2 does not provide node modules.
"
fi

if [[ -f "$BUILD_DIR/.npmrc" ]]; then
warn "There is an existing .npmrc file that will not be used. All configurations are read from the .yarnrc.yml file."
fi

if [[ -f "$BUILD_DIR/.yarnrc" ]]; then
warn "There is an existing .yarnrc file that will not be used. All configurations are read from the .yarnrc.yml file."
fi

### Configure Yarn 2
# fail for no yarnrc.yml
fail_missing_yarnrc_yml "$BUILD_DIR"

# get yarn_path
YARN2_PATH=$(get_yarn_path "$BUILD_DIR")

# fail for no yarnPath in rc
fail_missing_yarn_path "$BUILD_DIR" "$YARN2_PATH"

# fail for missing yarn in .yarn/releases
fail_missing_yarn_vendor "$BUILD_DIR" "$YARN2_PATH"
fi

install_bins() {
Expand All @@ -162,7 +180,7 @@ install_bins() {
yarn_engine=$(read_json "$BUILD_DIR/package.json" ".engines.yarn")

if [[ "$YARN2" == "true" && -n "$yarn_engine" ]]; then
warn " You should be using a vendored version of Yarn. Heroku will ignore the engine from the package.json. "
warn "You should be using a vendored version of Yarn. Heroku will ignore the engine from the package.json."
fi

meta_set "node-version-request" "$node_engine"
Expand Down
71 changes: 71 additions & 0 deletions lib/failure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,77 @@ fail_invalid_semver() {
fi
}

# Yarn 2 failures

fail_missing_yarnrc_yml() {
local build_dir="$1"

if [[ ! -f "$build_dir/.yarnrc.yml" ]]; then
mcount "failures.missing-yarnrc-yml"
meta_set "failure" "missing-yarnrc-yml"
header "Build failed"
warn "The yarnrc.yml file could not found
Copy link
Member

@Malax Malax Apr 27, 2020

Choose a reason for hiding this comment

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

Nitpick: This is an error, not a warning. Probably suboptimal naming of the logging functions and not related to this PR? This pattern repeats a couple of times in this file.

Copy link
Contributor Author

@danielleadams danielleadams Apr 27, 2020

Choose a reason for hiding this comment

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

Agreed - it makes sense to do some work that lists this as an error with a failure rather than a warn given that one will fail the build and one won't. I'd like to follow up on this outside of this PR.

danielleadams marked this conversation as resolved.
Show resolved Hide resolved

It looks like the yarnrc.yml file is missing from this project. Please
make sure this file is checked into version control and made available to
Heroku.

To generate yarnrc.yml, make sure Yarn 2 is installed on your local
machine and set the version in your project directory with:

$ yarn set version berry
"
fail
fi
}

fail_missing_yarn_path() {
local build_dir="$1"
local yarn_path="$2"

if [[ "$yarn_path" == "" ]]; then
mcount "failures.missing-yarn-path"
meta_set "failure" "missing-yarn-path"
header "Build failed"
warn "The 'yarnPath' could not be read from the yarnrc.yml file

It looks like yarnrc.yml is missing the 'yarnPath' value, which is needed
to identify the location of yarn for this build.

To generate properly set 'yarnPath', make sure Yarn 2 is installed on your
danielleadams marked this conversation as resolved.
Show resolved Hide resolved
local machine and set the version in your project directory with:

$ yarn set version berry
"
fail
fi
}

fail_missing_yarn_vendor() {
local build_dir="$1"
local yarn_path="$2"

if [[ ! -f "$build_dir/$yarn_path" ]]; then
mcount "failures.missing-yarn-vendor"
meta_set "failure" "missing-yarn-vendor"
header "Build failed"
warn "Yarn was not found

It looks like yarn is missing from $yarn_path, which is needed to continue
this build on Heroku. Yarn 2 recommends vendoring Yarn under the '.yarn'
directory, so remember to check the '.yarn' directory into version control
to use during builds.

To generate the vendor correctly, make sure Yarn 2 is installed on your
danielleadams marked this conversation as resolved.
Show resolved Hide resolved
local machine and run the following in your project directory:

$ yarn install
$ yarn set version berry
"
fail
fi
}

log_other_failures() {
local log_file="$1"
if grep -qi "sh: 1: .*: not found" "$log_file"; then
Expand Down
5 changes: 5 additions & 0 deletions lib/yarn2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ detect_yarn2() {
echo "false"
fi
}

get_yarn_path() {
local build_dir="$1"
$YQ r "$build_dir/.yarnrc.yml" yarnPath 2>&1
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
56 changes: 56 additions & 0 deletions test/fixtures/yarn-2-with-engine/.yarn/releases/yarn-rc.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/fixtures/yarn-2-with-engine/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-rc.js
1 change: 1 addition & 0 deletions test/fixtures/yarn-2-without-cache/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-rc.js
Loading