berry icon indicating copy to clipboard operation
berry copied to clipboard

[Bug?]: Yarn 4 resets "bin" field in package.json after install/link operations

Open mfang0126 opened this issue 1 year ago • 3 comments
trafficstars

Self-service

  • [ ] I'd be willing to implement a fix

Describe the bug

When using Yarn 4.2.2, the "bin" field in the package.json file is being reset to "bin": "./dist/index.js" after running any Yarn command that includes install steps, such as yarn link, yarn, or yarn install. This behavior makes it impossible to customize the "bin" field for a Node CLI project.

To reproduce

  1. Create a new Node.js project.
  2. Add a "bin" field to the package.json file, specifying a custom script path:
    {
      // ...
      "packageManager": "[email protected]",
      "bin": {
        "my-script": "./dist/index.js"
      },
      // ...
    }
    
  3. Run any of the following Yarn commands:
    • yarn link
    • yarn
    • yarn install
  4. Observe that the "bin" field in the package.json file has been reset to "bin": "./dist/index.js".

Environment

System:
  OS: macOS 14.5
  CPU: (12) arm64 Apple M3 Pro
Binaries:
  Node: 20.11.0 - /private/var/folders/8p/k84m_zzx6yz_s258bw82jt880000gr/T/xfs-4d591356/node
  Yarn: 4.2.2 - /private/var/folders/8p/k84m_zzx6yz_s258bw82jt880000gr/T/xfs-4d591356/yarn
  npm: 10.2.4 - ~/.nvm/versions/node/v20.11.0/bin/npm
  bun: 1.1.8 - ~/.bun/bin/bun

Additional context

No response

mfang0126 avatar Jun 07 '24 02:06 mfang0126

Yarn reformats the package.json. If it did this change, it's that it wouldn't have had functional change (ie your package is called my-script, so "bin as a string" is implicitly mapped to the proper name).

arcanis avatar Jun 07 '24 06:06 arcanis

Yarn reformats the package.json. If it did this change, it's that it wouldn't have had functional change (ie your package is called my-script, so "bin as a string" is implicitly mapped to the proper name).

Thanks @arcanis. I mostly using npm previously. I am trying to figure it out how to ensure it work the same as npm way. If I do npm build and npm link I can simply test it in my local with npx my-script. Do you know how to do it in yarn, please?

mfang0126 avatar Jun 18 '24 00:06 mfang0126

I came here because I noticed the same behavior.

This is parts of my package.json file

{
  "name": "setup-rwgc",
  "version": "0.2.1",
  "description": "Setup rwgc in a RedwoodJS project",
  "repository": {
    "type": "git",
    "url": "https://github.com/Tobbe/setup-rwgc"
  },
  "main": "./dist/cli.js",
  "bin": {
    "setup-rwgc": "./dist/cli.js"
  },
  "scripts": {

So @arcanis, you're saying that because "name": "setup-rwgc" matches what I'm trying to map my bin to (setup-rwgc) that mapping is redundant and so yarn gets rid of it?

Ever since I noticed this I've just been manually reverting the change, because I didn't understand it. Thanks for explaining!

What do you think about logging a message explaining what's going on when yarn rewrites something like this?

Tobbe avatar Jun 23 '24 02:06 Tobbe

Hi! 👋

It seems like this issue as been marked as probably resolved, or missing important information blocking its progression. As a result, it'll be closed in a few days unless a maintainer explicitly vouches for it.

yarnbot avatar Jul 23 '24 03:07 yarnbot