vite: [2.5 beta] json parse error for tsconfig.json that includes trailing comma in object

Describe the bug

the code reading tsconfig.json via tsconfig dependency that was added in this #4279 causes a json parse error if the tsconfig.json contains a trailing comma.

trailing commas and comments are allowed in tsconfig.json

also filed an upstream issue: https://github.com/TypeStrong/tsconfig/issues/30

Obvious workaround is to just remove the trailing comma, leaving this issue here anyways for visibility.

Reproduction

in any vite ts project linked to local vite with sufficient version

  • add a trailing comma to tsconfig.json
  • start dev
  • parse error on cli

found via https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/e2e-tests/ts-type-import/

System Info

observed with local vite @ rev 69f91a1c03a129ab324c20fddfd2a07e729add96

Used Package Manager

pnpm

Logs

10:23:18 PM [vite] Internal server error: Unexpected token } in JSON at position 107
  Plugin: vite:esbuild
  File: /home/dominikg/develop/sveltejs/vite-plugin-svelte/packages/e2e-tests/ts-type-import/src/index.ts
      at JSON.parse (<anonymous>)
      at parse (/home/dominikg/develop/vite/node_modules/tsconfig/dist/tsconfig.js:135:17)
      at /home/dominikg/develop/vite/node_modules/tsconfig/dist/tsconfig.js:116:32
      at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:73:3)

Validations

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (16 by maintainers)

Commits related to this issue

Most upvoted comments

who wants to convince esbuild to expose their parser?

@dominikg tag you’re it

no, it’s a general issue with the 2.5.0 beta

npm init vite -t vanilla-ts vite-vanilla-ts
cd vite-vanilla-ts
npm i
# add trailing comma to tsconfig
sed -i -e 's/"noImplicitReturns": true/"noImplicitReturns": true,/g' tsconfig.json
npm run dev 
# open browser, works without issue, close browser and devserver
# install 2.5 beta and try again
npm i -D vite@beta
npm run dev
# open browser

now it fails

9:44:37 AM [vite] Internal server error: Unexpected token } in JSON at position 351
  Plugin: vite:esbuild
  File: /home/dominikg/develop/reproductions/vite-ts-vanilla/src/main.ts
      at JSON.parse (<anonymous>)
      at parse$e (/home/dominikg/develop/reproductions/vite-ts-vanilla/node_modules/vite/dist/node/chunks/dep-bfd63c7d.js:30121:17)
      at /home/dominikg/develop/reproductions/vite-ts-vanilla/node_modules/vite/dist/node/chunks/dep-bfd63c7d.js:30102:32
      at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:73:3)

edited for clarity, it works in 2.4.4 and fails in 2.5.0-beta

However, dealing with the typescript peer dependency* only for the trailing comma issue seems overkill to me. I don’t feel like putting effort into the extra complexity myself.

Contributions are welcome, though.


* typescript must be an optional peer dependency of vite, so we have to check its existence during run time.