bun icon indicating copy to clipboard operation
bun copied to clipboard

bun add throwing JSON Lexer bug

Open 0xaf2f6ad opened this issue 3 years ago • 3 comments

Version

0.1.3

Platform

Linux dev 5.15.52-1-lts #1 SMP Sat, 02 Jul 2022 20:04:03 +0000 x86_64 GNU/Linux

What steps will reproduce the bug?

create any JS/TS project with the following jest config in the package.json

{
  "...": "...",
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

How often does it reproduce? Is there a required condition?

it occurs anytime I run bun add

What is the expected behavior?

Bun should just add a package like any other package managers, installing to the node_modules dir and also updating the package.json

What do you see instead?

the following error is thrown:

bun add v0.1.3

error: Syntax Error!!

    "testRegex": ".*\.spec\.ts$",
                 ^
/...path.../Bun_NestJS/app/package.json:83:18 2534
SyntaxError parsing package.json in "/...path.../Bun_NestJS/app/"

Additional information

No response

0xaf2f6ad avatar Jul 12 '22 14:07 0xaf2f6ad

Interesting, the JSON parser is correctly parsing the given JSON file when you import it, but it fails when running bun add ... I'll look into this.

sno2 avatar Jul 13 '22 13:07 sno2

It seems to fail on escapes in strings. However, the js_parser does not fail. I guess we need to find what we are doing differently in the JSON Parser compared to the JS Parser.

sno2 avatar Jul 13 '22 13:07 sno2

@sno2 I've given a quick look into this issue. It seems that the bug is not in the parser, but in the printer. bun add will parse the package.json again after adding the new dependencies here: https://github.com/oven-sh/bun/blob/a8d499208bfc401289bc3d9c4efb7cdf45f6eea5/src/install/install.zig#L4701

And the new json generated will lose the double \, turning ".*\\.spec\\.ts$" into ".*\.spec\.ts$", which causes syntax error.

From my tracing its seems that the error lies in this function: https://github.com/oven-sh/bun/blob/a8d499208bfc401289bc3d9c4efb7cdf45f6eea5/src/js_printer.zig#L2392-L2399 in which printQuotedUTF16 will retain the \\, while printUTF8StringEscapedQuotes won't.

zhuzilin avatar Aug 03 '22 09:08 zhuzilin

[Edit] 0.1.10 Latest version on WSL 2 Ubuntu 22.04.1 LTS I'm using with nestjs default package.json and have same problem.

❯ bun rm **package** # or bun a **package**
[0.02ms] ".env"
bun remove v0.1.10


error: Syntax Error!!

    "testRegex": ".*\.spec\.ts$",
                 ^
/home/**/project/package.json:84:18 2514
SyntaxError parsing package.json in "/home/**/project/"

armada45-pixel avatar Aug 15 '22 03:08 armada45-pixel

This is still happening as of Bun v0.5.5

Jarred-Sumner avatar Feb 07 '23 00:02 Jarred-Sumner

This appears to be due to these factors colliding:

  • Javascript string literals are stored with escaping (needs confirmation but the code appears to assume it)
  • JSON string literals are stored decoded
  • _printUTF8StringEscapedQuotes assumes that the string is stored with escaping but is called to print JSON strings

Thus when bun add parses the package file, adds the dependency, writes the new file to memory, then reparses it we get this failure because the write step didn't re-escape the string.

Next steps:

  • Confirm that Javascript and JSON strings are being stored differently and figure out why
  • Investigate whether quoteForJSON should be used instead as it appears to assume that JSON strings are not stored escaped

jwhear avatar Feb 08 '23 18:02 jwhear

Updated diagnosis: Javascript and JSON literals are stored differently, but it's not a matter of escaping: both are stored decoded. However, Javascript literals are stored as UTF16 and thus hit the printQuotedUTF16 implementation while JSON goes through printUTF8StringEscapedQuotes: https://github.com/oven-sh/bun/blob/995880a7effe692b5482f04d908f07f1227c8f68/src/js_printer.zig#L2519-L2527

printQuotedUTF16 escapes backslashes and a bunch of other stuff while printUTF8StringEscapedQuotes does not. I am working on reimplementing printUTF8StringEscapedQuotes in the hopes that it will resolve this issue.

jwhear avatar Feb 10 '23 17:02 jwhear