From 0eadbe7a9a0fa02fb63aede9c68ba4ada9a013e9 Mon Sep 17 00:00:00 2001 From: Luna Wei Date: Fri, 5 Nov 2021 15:07:02 -0700 Subject: [PATCH] Fix npm latest tag issue when releasing patches (#32543) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/32543 Changelog: [Internal] Fix npm `latest` tag issue that occurs when we release a patch on an older minor version Context: * There are two types of tags, git and npm, they are unrelated. * When we publish a stable release, we set the git tag `latest`. This logic is faulty when we release a patch to an older version. * When publishing a package to npm, if you don't provide an explicit tag, the `latest` tag will be applied -- at least that's how I've understood the [docs here](https://docs.npmjs.com/cli/v7/commands/npm-dist-tag#description). This again is faulty logic when we release a patch to an older version. * npm and git's `latest` tag should always point to our most recent stable version This change: * Introduces a `--latest` flag for `bump-oss-script` that will indicate that the release we're running (either a stable or pre-release) should really be considered "latest" * If the version is not a pre-release and the `--latest` flag is set, we will set the git `latest` tag * Later, in the circleCI job that we use to publish the npm package, we will see if the current commit is git-tagged as `latest`. If it is, then we'll explicitly tell npm to use `latest` tag but most importantly, if it's not, we'll set a tag of the form `{major}.{minor}-stable`. * This type of tag (ex. `0.66-stable`) is new and the intention is that it will always point to latest of that minor version. Reviewed By: hramos Differential Revision: D32196239 fbshipit-source-id: 4c881851eebcad8585732ff0c07322413ac46ce5 --- scripts/bump-oss-version.js | 10 ++++++++-- scripts/publish-npm.js | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/scripts/bump-oss-version.js b/scripts/bump-oss-version.js index 7109f8b08a7..d9447449170 100755 --- a/scripts/bump-oss-version.js +++ b/scripts/bump-oss-version.js @@ -34,6 +34,11 @@ let argv = yargs .option('v', { alias: 'to-version', type: 'string', + }) + .option('l', { + alias: 'latest', + type: 'boolean', + default: false, }).argv; const nightlyBuild = argv.nightly; @@ -221,8 +226,9 @@ if (!nightlyBuild) { let remote = argv.remote; exec(`git push ${remote} v${version}`); - // Tag latest if doing stable release - if (prerelease == null) { + // Tag latest if doing stable release. + // This will also tag npm release as `latest` + if (prerelease == null && argv.latest) { exec('git tag -d latest'); exec(`git push ${remote} :latest`); exec('git tag latest'); diff --git a/scripts/publish-npm.js b/scripts/publish-npm.js index 825e0892821..d965724c7b1 100644 --- a/scripts/publish-npm.js +++ b/scripts/publish-npm.js @@ -87,9 +87,11 @@ const rawVersion = buildTag; let version, + major, + minor, prerelease = null; try { - ({version, prerelease} = parseVersion(rawVersion)); + ({version, major, minor, prerelease} = parseVersion(rawVersion)); } catch (e) { echo(e.message); exit(1); @@ -155,12 +157,24 @@ if (dryRunBuild) { exit(0); } +// Running to see if this commit has been git tagged as `latest` +const latestCommit = exec("git rev-list -n 1 'latest'", { + silent: true, +}).stdout.replace('\n', ''); +const isLatest = currentCommit === latestCommit; + +const releaseBranch = `${major}.${minor}-stable`; + // Set the right tag for nightly and prerelease builds +// If a release is not git-tagged as `latest` we use `releaseBranch` to prevent +// npm from overriding the current `latest` version tag, which it will do if no tag is set. const tagFlag = nightlyBuild ? '--tag nightly' : prerelease != null ? '--tag next' - : ''; + : isLatest + ? '--tag latest' + : `--tag ${releaseBranch}`; // use otp from envvars if available const otpFlag = otp ? `--otp ${otp}` : '';