yarnpkg / berry

📦🐈 Active development trunk for Yarn ⚒
https://yarnpkg.com
BSD 2-Clause "Simplified" License
7.48k stars 1.12k forks source link

[Bug?]: `yarn version prerelease` breaks due to `packageJson.stableVersion` #4328

Open samijaber opened 2 years ago

samijaber commented 2 years ago

Self-service

Describe the bug

Duplicate of #3868 with a Sherlock reproduction.

The issue is that:

Workaround

A solution is to remove stableVersion right before you run yarn version <strategy>. You can make sure this jq script runs before every call to yarn version:

./scripts/remove-stableVersion.sh:

#!/bin/bash

# Remove `stableVersion` before relreasing, as it's buggy.
# https://github.com/yarnpkg/berry/issues/3868
echo "$(jq 'del(.stableVersion)' package.json)" >package.json

package.json:

    "remove-stableVersion": "./scripts/remove-stableVersion.sh",
    "version:prerelease": "yarn run remove-stableVersion && yarn version prerelease",

To reproduce

const {promises: {readFile}} = require(`fs`);

await packageJsonAndInstall({
version: "0.0.1",
});

// Run `yarn version prerelease` for the first time.
// This will create the `stableVersion` key
await yarn(`version`, `prerelease`);
const pkgJson = JSON.parse(await readFile(`package.json`, `utf8`));
expect(pkgJson.version).toBe('0.0.2-0')
expect(pkgJson.stableVersion).toBe('0.0.1')

// Run `yarn version prerelase` with `stableVersion` present.
// This is where the bug occurs.
await yarn(`version`, `prerelease`);
const pkgJson2 = JSON.parse(await readFile(`package.json`, `utf8`));
expect(pkgJson2.version).toBe('0.0.2-1')

Environment

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 14.19.0 - /private/var/folders/qr/md00ttgs1f93zww0lvtv2d6h0000gn/T/com.apple.WorkflowKit.BackgroundShortcutRunner/xfs-3b1e9ba2/node
    Yarn: 3.2.0 - /private/var/folders/qr/md00ttgs1f93zww0lvtv2d6h0000gn/T/com.apple.WorkflowKit.BackgroundShortcutRunner/xfs-3b1e9ba2/yarn
    npm: 6.14.16 - ~/.asdf/plugins/nodejs/shims/npm
  npmPackages:
    jest: ^24.1.0 => 24.9.0

Additional context

No response

yarnbot commented 2 years ago

This issue reproduces on master:

Error: expect(received).toBe(expected) // Object.is equality

Expected: "0.0.2-1"
Received: "0.0.2-0"
    at module.exports (evalmachine.<anonymous>:19:26)
    at async /github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:57:13
    at async executeInTempDirectory (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:18:16)
    at async executeRepro (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:25:12)
    at async ExecCommand.execute (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/commands/exec.js:26:38)
    at async ExecCommand.validateAndExecute (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Command.js:161:26)
    at async Cli.run (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Cli.js:74:24)
    at async Cli.runExit (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Cli.js:83:28)
yarnbot commented 2 years ago

This issue reproduces on master:

Error: expect(received).toBe(expected) // Object.is equality

Expected: "0.0.2-1"
Received: "0.0.2-0"
    at module.exports (evalmachine.<anonymous>:19:26)
    at async /github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:57:13
    at async executeInTempDirectory (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:18:16)
    at async executeRepro (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:25:12)
    at async ExecCommand.execute (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/commands/exec.js:26:38)
    at async ExecCommand.validateAndExecute (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Command.js:161:26)
    at async Cli.run (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Cli.js:74:24)
    at async Cli.runExit (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Cli.js:83:28)
sid-the commented 1 year ago

Still getting this issue

oleksandr-danylchenko commented 8 months ago

Yep, it's still relevant for the Yarn 4.1.11. The prerelease version gets stuck:

image
sagarscript commented 8 months ago

I'm having the same issue.

I've noticed 2 things that will increment the prerelease version.

https://github.com/yarnpkg/berry/assets/15949513/6a0a6502-ee23-44fb-b4aa-67fe7e4531f1

This is definitely not the behaviour one would expect - at least from my understanding of the developer docs.

This command will bump the version number for the given package, following the specified strategy:

  • If major, the first number from the semver range will be increased (X.0.0).
  • If minor, the second number from the semver range will be increased (0.X.0).
  • If patch, the third number from the semver range will be increased (0.0.X).
  • If prefixed by pre (premajor, ...), a -0 suffix will be set (0.0.0-0).
  • If prerelease, the suffix will be increased (0.0.0-X); the third number from the semver range will also be increased if there was no suffix in the previous version.
  • If decline, the nonce will be increased for yarn version check to pass without version bump.
  • If a valid semver range, it will be used as new version.
  • If unspecified, Yarn will ask you for guidance.

Furthermore, the pre functionality does not work either:

  • If prefixed by pre (premajor, ...), a -0 suffix will be set (0.0.0-0).

command yarn version -i premajor returns this error:

Usage Error: Invalid value for enumeration: "premajor" (expected one of "decline", "major", "minor", "patch", "prerelease")

$ yarn version [-d,--deferred] [-i,--immediate] <strategy>
EvanSanderson commented 3 months ago

Any news on this? Or insight into the recommended way of managing a workflow with a prerelease version? What we'd really like is the same functionality presented by npm (https://stackoverflow.com/questions/39206082/npm-version-to-add-alpha-postfix).

Generally, it would be great to get documentation (even something small) describing an example scenario for developing and iterating on a library using Yarn 4 that isn't focused on the workspace/project release flow.

pbrennand-francis commented 3 weeks ago

I thought I'd share an expansion on the workaround that applies to workspaces with multiple nested package.json files:

# The same workaround as shared above
removeStableVersion() {
  echo "$(jq 'del(.stableVersion)' $1)" >$1
}

# A simple script to find all package.json files and run the workaround on them, filtering `node_modules` and `.nx` cache files
find . -type d \( -name node_modules -o -name .nx \) -prune -o -name 'package.json' -print|while read fname; do
  removeStableVersion $fname
done

There is probably a more performant way, but this was simple and works for us.