Skip to content

Commit

Permalink
chore: compat checks fail if a release has happened (#19017)
Browse files Browse the repository at this point in the history
If a PR is branched off of a version that introduces a new
package, and lives longer than the release, the "does package
exist?" check is wrong, and fails the PR build.

This is usually fixed by another "Update from master", but that's
an annoying step that sometimes takes a while.

- Let's say the branch was forked at 1.142.0
- The branch adds a new package
- That package is released as its initial version as 1.143.0
- When the PR next builds (still at 1.142.0), it will do the following
  checks:
  - Does the package exist at all? (Answer: yes)
  - If so, try to install it at 1.142.0, because that's what we're
    diffing against (This explodes, because the package does not exist
    at that version).

Instead of doing a versionless "does the package exist at all" check,
always check the existence of the actual version we'll be diffing
against.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr committed Feb 21, 2022
1 parent 64d26cc commit 9c83ee5
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions scripts/check-api-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package_name() {
# Doesn't use 'npm view' as that is slow. Direct curl'ing npmjs is better
package_exists_on_npm() {
pkg=$1
ver=$2 # optional
ver=${2:-} # optional
curl -I 2>/dev/null https://registry.npmjs.org/$pkg/$ver | head -n 1 | grep 200 >/dev/null
}

Expand All @@ -32,10 +32,13 @@ jsii_package_dirs=$(list_jsii_packages)

#----------------------------------------------------------------------

# Input a directory, output the directory IF it exists on NPM
# dirs_for_existing_pkgs DIRECTORY VERSION
#
# Input a directory and a version, output the directory IF it exists on NPM at that version
dirs_for_existing_pkgs() {
local dir="$1"
if package_exists_on_npm $(package_name $dir); then
local ver="$2"
if package_exists_on_npm $(package_name $dir) $ver; then
echo "$dir"
echo -n "." >&2
else
Expand All @@ -49,30 +52,24 @@ export -f dirs_for_existing_pkgs

if ! ${SKIP_DOWNLOAD:-false}; then
echo "Filtering on existing packages on NPM..." >&2
# In parallel
existing_pkg_dirs=$(echo "$jsii_package_dirs" | xargs -n1 -P4 -I {} bash -c 'dirs_for_existing_pkgs "$@"' _ {})
existing_names=$(echo "$existing_pkg_dirs" | xargs -n1 -P4 -I {} bash -c 'package_name "$@"' _ {})
echo " Done." >&2

echo "Determining baseline version..." >&2
version=$(node -p 'require("./scripts/resolve-version.js").version')
echo " Current version is $version." >&2
echo "Using version '$version' as the baseline..."

if ! package_exists_on_npm aws-cdk $version; then
major_version=$(echo "$version" | sed -e 's/\..*//g')
echo " Version $version does not exist in npm. Falling back to package major version ${major_version}" >&2
existing_names=$(echo "$existing_names" | sed -e "s/$/@$major_version/")
else
echo "Using version '$version' as the baseline..."
existing_names=$(echo "$existing_names" | sed -e "s/$/@$version/")
fi
# Filter packages by existing at the target version
existing_pkg_dirs=$(echo "$jsii_package_dirs" | xargs -n1 -P4 -I {} bash -c 'dirs_for_existing_pkgs "$@" "'$version'"' _ {})
existing_names=$(echo "$existing_pkg_dirs" | xargs -n1 -P4 -I {} bash -c 'package_name "$@"' _ {})
install_versions=$(for name in $existing_names; do echo "${name}@${version}"; done)
echo " Done." >&2

rm -rf $tmpdir
mkdir -p $tmpdir

echo "Installing from NPM..." >&2
# use npm7 to automatically install peer dependencies
(cd $tmpdir && npx npm@^7.0.0 install --prefix $tmpdir $existing_names)
(cd $tmpdir && npx npm@^7.0.0 install --prefix $tmpdir $install_versions)
fi

#----------------------------------------------------------------------
Expand Down

0 comments on commit 9c83ee5

Please sign in to comment.