[Bug]: Npm-publish.js Fails If Package.json Version Matches Tag Version

by ADMIN 72 views

Introduction

As a developer, you're likely familiar with the pain of dealing with versioning issues in your projects. In this article, we'll explore a bug in the npm-publish.js script that causes it to fail when the package.json version matches the tag version. We'll delve into the root cause of the issue, provide a step-by-step guide to reproduce the bug, and offer a suggested solution to fix it.

The Bug

When publishing a release by pushing a tag (e.g., v0.2.0), the npm-publish.js script attempts to run pnpm version $version --no-git-tag-version even if the package.json already has the correct version. This causes the workflow to fail with:

npm error Version not changed

This is unnecessary because the version in package.json is already correct (set by previous workflows or PRs).

Expected Behavior

If the version in package.json already matches the tag version, the script should skip running pnpm version ... and proceed with the publish steps.

Steps to Reproduce

  1. Prepare a release branch and update package.json to the new version (e.g., 0.2.0).
  2. Merge to main and push a tag v0.2.0.
  3. The publish workflow runs and fails with npm error Version not changed.

Reproduction Code

N/A (workflow/publish script issue)

Environment

  • OS: GitHub Actions runner (Ubuntu)
  • Node.js version: 22.x
  • Package manager: pnpm 10.x
  • CI/CD Pipeline

Error Logs

Tag: v0.2.0, extracted version: 0.2.0, package.json version: 0.2.0
Comparing tag version (0.2.0) with package.json version (0.2.0)...
npm error Version not changed

Impact Level

Medium (Inconvenient but workable)

Current Workaround

Manually retry after editing the script, or patch the workflow to skip the version set if already correct.

Additional Context

  • The version in package.json is already set by the release process before the tag is pushed.
  • The script should only set the version if it differs from the tag version.
  • See related logic in scripts/npm-publish.js.

Suggested Solution

Update npm-publish.js to check if the version in package.json matches the tag version before running pnpm version .... If they match, skip the command and continue.

Code Changes

To fix the bug, you'll need to modify the npm-publish.js script to include the following code:

const packageJson = require('./package.json');
const tagVersion = process.env.TAG_VERSION;

if (packageJson.version === tagVersion) {
  console.log('Version in package.json matches tag version, skipping version set');
  return;
}

// Rest of the script remains the same

By adding this simple check, you'll ensure that the script only sets the if it differs from the tag version, preventing the workflow from failing due to a version mismatch.

Conclusion

Q: What is the root cause of the bug in npm-publish.js?

A: The root cause of the bug is that the script attempts to run pnpm version $version --no-git-tag-version even if the package.json already has the correct version. This causes the workflow to fail with npm error Version not changed.

Q: Why is the script running pnpm version ... if the version in package.json is already correct?

A: The script should only set the version if it differs from the tag version. However, in the current implementation, it attempts to run pnpm version ... regardless of whether the version in package.json is already correct.

Q: What is the expected behavior of the script?

A: If the version in package.json already matches the tag version, the script should skip running pnpm version ... and proceed with the publish steps.

Q: How can I reproduce the bug?

A: To reproduce the bug, follow these steps:

  1. Prepare a release branch and update package.json to the new version (e.g., 0.2.0).
  2. Merge to main and push a tag v0.2.0.
  3. The publish workflow runs and fails with npm error Version not changed.

Q: What is the impact level of this bug?

A: The impact level of this bug is medium (inconvenient but workable). It requires manual intervention to retry the script or patch the workflow to skip the version set if already correct.

Q: What is the current workaround for this bug?

A: The current workaround is to manually retry after editing the script, or patch the workflow to skip the version set if already correct.

Q: What is the suggested solution to fix this bug?

A: The suggested solution is to update npm-publish.js to check if the version in package.json matches the tag version before running pnpm version .... If they match, skip the command and continue.

Q: How can I implement the suggested solution?

A: To implement the suggested solution, modify the npm-publish.js script to include the following code:

const packageJson = require('./package.json');
const tagVersion = process.env.TAG_VERSION;

if (packageJson.version === tagVersion) {
  console.log('Version in package.json matches tag version, skipping version set');
  return;
}

// Rest of the script remains the same

By adding this simple check, you'll ensure that the script only sets the version if it differs from the tag version, preventing the workflow from failing due to a version mismatch.

Q: Are there any additional context or related logic that I should be aware of?

A: Yes, there are additional context and related logic that you should be aware of:

  • The version in package.json is already set by the release process before the tag is pushed.
  • The script should only set the version if it differs from the tag version.
  • See logic in scripts/npm-publish.js.

Q: Where can I find more information about this bug and its solution?

A: You can find more information about this bug and its solution in the original bug report and this FAQ article.