Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Disable formatting of package.json bin object to string #6184

Open
1 task done
trivikr opened this issue Mar 27, 2024 · 13 comments
Open
1 task done

[Bug]: Disable formatting of package.json bin object to string #6184

trivikr opened this issue Mar 27, 2024 · 13 comments
Labels
bug Something isn't working

Comments

@trivikr
Copy link
Contributor

trivikr commented Mar 27, 2024

Self-service

  • I'd be willing to implement a fix

Describe the bug

Yarn package formats package.json bin object to string, when there's single executable same as the name of the package.

Although this is recommended in npm documentation at the time of issue creation, it is going to be removed as per npm/package-json#83 (comment)

To reproduce

$ mkdir test-yarn && cd test-yarn

$ yarn set version 4.1.1
➤ YN0000: Done in 0s 4ms

$ yarn init -y
➤ YN0000: · Yarn 4.1.1
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 29ms

$ cat package.json 
{
  "name": "test-yarn",
  "packageManager": "[email protected]"
}

$ brew install jq

$ jq '.bin={"test-yarn": "./path/to/file"}' package.json > test.json

$ mv test.json package.json

$ cat package.json 
{
  "name": "test-yarn",
  "packageManager": "[email protected]",
  "bin": {
    "test-yarn": "./path/to/file"
  }
}

$ yarn
➤ YN0000: · Yarn 4.1.1
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 36ms

$ cat package.json
{
  "name": "test-yarn",
  "packageManager": "[email protected]",
  "bin": "./path/to/file"
}

Environment

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 20.11.1 - /private/var/folders/42/54jl1_3x4hz06cf7bc_kzd4h0000gn/T/xfs-8dd2dbeb/node
    Yarn: 4.1.1 - /private/var/folders/42/54jl1_3x4hz06cf7bc_kzd4h0000gn/T/xfs-8dd2dbeb/yarn
    npm: 10.2.4 - ~/Library/Caches/fnm_multishells/42983_1711069674367/bin/npm

Additional context

No response

@trivikr
Copy link
Contributor Author

trivikr commented Mar 27, 2024

I believe the code change needs to be made in Manifest.ts

if (this.bin.size === 1 && this.name !== null && this.bin.has(this.name.name)) {
data.bin = this.bin.get(this.name.name)!;
} else if (this.bin.size > 0) {

@trivikr
Copy link
Contributor Author

trivikr commented Mar 29, 2024

npm v10 currently emits warning during publish

🦋  error npm WARN publish npm auto-corrected some errors in your package.json when publishing.  Please run "npm pkg fix" to address these errors.
🦋  error npm WARN publish errors corrected:
🦋  error npm WARN publish "bin" was converted to an object

This was observed in aws/aws-sdk-js-codemod#818 (comment)

@arcanis
Copy link
Member

arcanis commented Mar 29, 2024

I'm a little hesitant to make this change in Yarn since this warning is a client-side message that a third-party tool emits (yarn npm publish doesn't).

Yarn sends the same metadata as the ones contained in the tarball to the registry, so we don't have reasons to make this change (by contrast npm was turning the field into an object during publish, so for them it makes sense to stop doing that).

@trivikr
Copy link
Contributor Author

trivikr commented Mar 29, 2024

The request in this issue is not to format package.json#bin from object to string when running yarn install.
If the repository uses string for package.json#bin, there's no change needed.

Right now, yarn package manage forces authors to use short form.

@arcanis
Copy link
Member

arcanis commented Mar 29, 2024

Yes, but that's not the only change we perform. For instance we also normalize the dependencies map to sort everything. If we were to try to preserve the initial style it'd add complexity (we'd need to keep track of what was the initial style, which we don't do at the moment) for a fairly unconsequential change.

@mrazauskas
Copy link

Perhaps it would be possible to have an option which simply disables styling? I mean, one can use sort-package-json as styling tool. Or any other.

@ssbarnea
Copy link

yarn npm publish cannot take an artifact, so it cannot be used to publish an already packaged package. npm publish does.

This puts us in an inconvenient place where we are forced to use npm publish which gives those warnings about bin object, ones that we cannot address.

Note: build artifacts/archives are usually used in CI pipelines to pass the packages between different jobs. This allows to publish the exact version that was package and tested and avoid rebuilding the package during release, where there would be a risk of building it in a different way/context than the one that was tested.

@kariudo
Copy link

kariudo commented Jul 12, 2024

This behavior of forcing styling on package.json is personally annoying since it conflicts with other formatters in our hooks, causing a bunch of back and forth for no reason. I don't see why tampering with the format (inserting line breaks) should be the default behavior when running yarn.

@Zhengqbbb
Copy link

🙄 Why rewrite package.json.bin and i can't disable the error behavior 👎

CleanShot 2024-07-22 at 18 12 25@2x

@Tobbe
Copy link

Tobbe commented Jul 29, 2024

This has been tripping me up on a couple of occasions too.
See #6322
and redwoodjs/redwood#11113

I would love for yarn to follow npm's lead here. But of course I don't have the full picture that the Yarn maintainers have, so I don't want to come across as too pushy or entitled here 🙂

@TheAsda
Copy link

TheAsda commented Aug 12, 2024

I see the point here, but I have a package that has name with scope like @scope/pkg and the "bin" field that contains an object with "pkg": "dist/cli.js" so the direct mapping package.name === bin.name does not work here but [email protected] replaces the object with the string path anyway.

@arcanis
Copy link
Member

arcanis commented Aug 12, 2024

Yes, because the implicit bin name is always the package name without its scope (otherwise the slash would make it impossible to add to the PATH).

@TheAsda
Copy link

TheAsda commented Aug 13, 2024

Thanks for the reply. I could not find information about such behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants