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

Don't error on comments & trailing commas in package.json #10287

Merged
merged 14 commits into from
Apr 25, 2024

Conversation

Jarred-Sumner
Copy link
Collaborator

What does this PR do?

This makes bun install, bun run <script>, bun ./<file> , bun build <file> no longer error when package.json contains trailing commas, single-line comments or multi-line comments.

We do not recommend this feature, as it will break usage in other tools (as well as JSON.parse). But enough people have asked for it and it's very easy to add. We trust people can make their own decision whether or not it's worth the tradeoffs.

Fixes #9036

How did you verify your code works?

There are tests

Copy link
Contributor

github-actions bot commented Apr 15, 2024

Copy link
Contributor

github-actions bot commented Apr 15, 2024

Copy link
Contributor

github-actions bot commented Apr 15, 2024

Copy link
Contributor

github-actions bot commented Apr 15, 2024

@chribjel
Copy link

Maybe consider adding a warning in the console, since the feature is not recommended and is not spec compliant.

WARN: You are using comments or trailing commas in your package.json. This is not spec-compliant and might break other tooling.

@Lms24
Copy link

Lms24 commented Apr 17, 2024

Have you considered tooling that reads a user's package.json file? At Sentry, we maintain a wizard that installs and configures our SDKs in users' projects (basically users just run npx @sentry/wizard). The wizard JSON.parses the user's package.json to check if our dependencies were already installed. Of course we catch if parsing throws and warn users about it but it'll lead to problems/early returns in our setup flow.

It's probably reasonable to move to a dynamic import instead of JSON.parse but I'd argue we're by far not the only tooling that relies on parsing package.json this way. All these tools can break or turn to erroneous code paths if they suddenly start encountering comments in package.json.

Copy link
Contributor

github-actions bot commented Apr 21, 2024

@Jarred-Sumner, your commit has failing tests :(

💪 2 failing tests Darwin AARCH64

💻 2 failing tests Darwin x64 baseline

💻 4 failing tests Darwin x64

🐧💪 4 failing tests Linux AARCH64

🐧🖥 3 failing tests Linux x64 baseline

🐧🖥 3 failing tests Linux x64

🪟💻 14 failing tests Windows x64 baseline

🪟💻 14 failing tests Windows x64

View logs

},
// This is a fast pass I guess
2 => {
if (strings.eqlComptime(source.contents[0..1], "\"\"") or strings.eqlComptime(source.contents[0..1], "''")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[0..2] for all of these

},
});
// itBundled("packagejson/SyntaxErrorComment", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just delete these?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, will do in a follow-up

Copy link
Member

@dylan-conway dylan-conway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@miscellaneo
Copy link

Consider the following package.json.

{
  "devDependencies": {
    "@company-repo/prettier-config": "1.0.0",
    // We can't use the new version until the prettier config supports it.
    "prettier": "2.8.8"
  },
  "name": "@company-repo/my-package",
  "prettier": "@company-repo/prettier-config",
  "scripts": {
    "format": "prettier --write ."
  }
}

As it is now, bun run format would be an error, because there's a comment in the JSON. With this PR, Bun can run the script, but it would still error out, because Prettier will try to read this package.json, but fail because there's a comment in the JSON. Now, most users will probably figure "okay, Bun allows this, but Prettier doesn't, so I can't use this feature", erase the comment, and move on. But the problem is that, for a tool that has seen as much adoption as Bun has, there will still be a fair number of users who will see the error and think "okay, Bun allows this, but Prettier doesn't, so I should file an issue with Prettier".

You can probably remember something like this playing out when Bun hit 1.0, where something about Bun's implementation meant certain packages didn't work, and users would file issues with the repo for those packages. It wasn't what Bun intended, users were instructed to file issues with Bun instead, but it still happened because that's the conclusion some users are going to come to.

The difference here is that it's not a bug, it's a feature. The behavior that causes the script to crash will continue, because there's nothing for Bun to fix or implement that will make it stop. So users will keep running into this, not just with Prettier but whenever they try to use this feature along with a package that doesn't parse JSON with comments, and some of them will continue to come to the conclusion that it is an issue with those packages.

You could say that Prettier could avoid this by allowing package.json files to have comments, which would probably be easier for it to do that most packages since, like Bun, it already has code to handle JSON with comments, but that just sort of spreads the problem. Because now it's "okay, Bun and Prettier allow this, but such-and-such doesn't, so I should file an issue with such-and-such", and now such-and-such has to either put effort into getting config to read from JSON with comments or just put up with the extra noise on the issue tracker.

You can say

But enough people have asked for it and it's very easy to add.

but please remember the words of Tony Hoare, "I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement." The trouble this change will cause will be nowhere near the magnitude of issues stemming from that, but it is not nothing, either. It will add friction, going from Node to Bun or back again. It will add tension between Bun and package maintainers.

@Jarred-Sumner Jarred-Sumner merged commit 49fa21f into main Apr 25, 2024
44 of 52 checks passed
@Jarred-Sumner Jarred-Sumner deleted the jarred/packagejson branch April 25, 2024 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants