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

tsconfig should support suffix .jsonc #43121

Open
5 tasks done
felix9 opened this issue Mar 6, 2021 · 6 comments
Open
5 tasks done

tsconfig should support suffix .jsonc #43121

felix9 opened this issue Mar 6, 2021 · 6 comments
Labels
Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Milestone

Comments

@felix9
Copy link

felix9 commented Mar 6, 2021

Suggestion

πŸ” Search Terms

  • json
  • jsonc
  • tsconfig

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

tsconfig should support "extends": "pkg/foo.jsonc"

Currently that will try to read node_modules/pkg/foo.jsonc.json and fail

"extends": "./foo.jsonc" works. It only fails when trying to load a config using the node-resolution codepath.

πŸ“ƒ Motivating Example

Various dev tools complain about comments in .json files.

Many json config files allow comments, so most dev tools also understand a "json-with-comments" format, and the conventional file suffix for that is .jsonc.

Some dev tools special-case tsconfig.json as jsonc, and some tools also special-case variants like tsconfig*.json, but this is awkwardly inconsistent.

Supporting the suffix .jsonc will let people ignore the special-case irregularities and gravitate toward the generic file suffix.

Q: Why not some other json variant like .json5?

A: Other json variants try to fix other "json as configuration" problems in different ways, and none of them seem particularly dominant yet. jsonc is a conservative extension to json that has a lot of support, and it's also a subset of most of the other json variants.

And the tsconfig parser is already a jsonc parser, it doesn't need to change. It just needs support for the .jsonc suffix.

πŸ’» Use Cases

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Mar 8, 2021
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Mar 8, 2021

You're already free to name this file whatever you like; tsc -p myconfig.jsonp and other project-related commands don't require any specific naming convention. tsconfig.json is the default name and would have to remain so for compatibility reasons. Adding another extension would introduce a ton of complexity because there'd have to be tie-breaking strategies (e.g. what if we found both in the same folder, what if we found tsconfig.jsonc in one folder and tsconfig in a parent, etc).

@felix9
Copy link
Author

felix9 commented Mar 8, 2021

No, I know that -p can take any name. This is about "extends", it doesn't like the .jsonc suffix.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Mar 8, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 8, 2021
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Mar 8, 2021
@RyanCavanaugh
Copy link
Member

Thanks for clarifying, you're right

@sheetalkamat sheetalkamat added the Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". label Apr 15, 2021
@bennycode
Copy link
Contributor

@RyanCavanaugh this issue seems to be fixed because I just tested it with TypeScript 4.6.4 and it worked as expected πŸ˜€:

base.jsonc

{
  // A config file with a comment
  "compilerOptions": {
    "outDir": "myOutput",
  }
}

tsconfig.json

{
  "extends": "./base.jsonc",
  "compilerOptions": {
    "lib": ["es2017"],
    "module": "commonjs",
    "moduleResolution": "node",
    "rootDir": "src",
    "strict": true,
    "target": "es6"
  }
}

When running npx tsc I received transpiled output in the "myOutput" directory (as specified in "base.jsonc").

Further DX Improvements

It's great to see that you can feed the TypeScript compiler with JSON5 or JSONC files. I wish that tsc --init would also generate a tsconfig.jsonc (or tsconfig.json5) file but it actually generates a JSON file with comments which is invalid. This behaviour decreases the UX a bit as IDEs don't like it when you put comments into plain JSON:

WebStorm 2022.1.4

image

GitHub is also not a big fan of comments in JSON files and signals that in red:

GitHub File View

image

Conaclos added a commit to bare-ts/lib that referenced this issue Mar 31, 2023
TSC allows comments in `tsconfig.json` files.
Ideally the file should use the `jsonc` extension.
TypeScript team seems rejecting the idea of this support.
See [this issue](microsoft/TypeScript#43121).

By the way, `jsonc` extension can be used for tsconfig that are used
in `extends` of another _tsconfig_.

This fact was observed in this following thread:
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
Conaclos added a commit to bare-ts/bare that referenced this issue Apr 30, 2023
TSC allows comments in `tsconfig.json` files.
Ideally the file should use the `jsonc` extension.
TypeScript team seems rejecting the idea of this support.
See [this issue](microsoft/TypeScript#43121).

By the way, `jsonc` extension can be used for tsconfig that are used
in `extends` of another _tsconfig_.

This fact was observed in this following thread:
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
Conaclos added a commit to bare-ts/bare that referenced this issue May 14, 2023
TSC allows comments in `tsconfig.json` files.
Ideally the file should use the `jsonc` extension.
TypeScript team seems rejecting the idea of this support.
See [this issue](microsoft/TypeScript#43121).

By the way, `jsonc` extension can be used for tsconfig that are used
in `extends` of another _tsconfig_.

This fact was observed in this following thread:
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
@domdomegg
Copy link

@bennycode Unfortunately this issue is not fixed. This issue is about the problem that you cannot import a jsonc file from another package:

tsconfig should support "extends": "pkg/foo.jsonc"
Currently that will try to read node_modules/pkg/foo.jsonc.json and fail
"extends": "./foo.jsonc" works. It only fails when trying to load a config using the node-resolution codepath.

@alexweej
Copy link

My 2c: Not normalizing the use of the filename tsconfig.jsonc is a bit of an unnecessary microhazard. Many TSConfigs are pure JSON, and given that they have the .json filename extension, it'd seem reasonable to expect to be able to parse one using e.g. JSON.parse(...), when implementing tooling. Only potentially much later do you notice the bug when, e.g., someone brings a TSConfig scaffolded with tsc --init, full of comments, and then have to waste time going back fixing this.

Are we at risk of eroding the simplicity of 'JSON' here, in the entire ecosystem, because of things like this? Maybe an overblown statement, but still... the answer is yes IMO :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants