TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

tsconfig.json: Specify <root> in "include" and "exclude" for extends configs

Open karlhorky opened this issue 4 years ago β€’ 3 comments

Suggestion

As always, thanks again for all of your hard work on the TypeScript tooling and ecosystem! πŸ™Œ

Extends configs (configs referenced from "extends") can specify "include" and "exclude", but they are relative to the tsconfig.json file (which could be deep in node_modules) instead of the root of the directory, per the include docs:

These filenames are resolved relative to the directory containing the tsconfig.json file.

This means that the following configuration would not work as expected:

tsconfig.json

{
  "extends": "@upleveled/eslint-config-upleveled/templates/tsconfig.json"
}

node_modules/@upleveled/eslint-config-upleveled/templates/tsconfig.json

  "include": [
    "**/.eslintrc.cjs",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.js",
    "**/*.jsx",
    "**/*.cjs",
    "**/*.mjs"
  ],

(this would resolve relative to the node_modules deep path, which would be useless)

Removal of such broken config can also be seen in the wild here: https://github.com/tsconfig/bases/pull/41

It would be great to get a <root> capability to add to include and exclude paths to refer to the directory with the package.json file, like Jest's <rootDir> option:

  "include": [
    "<root>/**/*.ts"
  ]

πŸ” Search Terms

extends, extended, root directory, root folder, configs, tsconfig

βœ… Viability Checklist

My suggestion meets these guidelines:

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

⭐ Suggestion

In the description above

πŸ“ƒ Motivating Example

In the description above

πŸ’» Use Cases

Workaround

Specify the paths using relative paths:

  "include": [
    "../../../../**/.eslintrc.cjs",
    "../../../../next-env.d.ts",
    "../../../../**/*.ts",
    "../../../../**/*.tsx",
    "../../../../**/*.js",
    "../../../../**/*.jsx",
    "../../../../**/*.cjs",
    "../../../../**/*.mjs"
  ],

But here this encodes the directory structure of node_modules into the config as well, which is brittle.

karlhorky avatar Oct 18 '22 11:10 karlhorky

Related: #37227

MartinJohns avatar Oct 18 '22 12:10 MartinJohns

Using include in an extends’d config seems useless in general (or at least highly situational) because according to the docs:

It’s worth noting that files, include and exclude from the inheriting config file overwrite those from the base config file

fatcerberus avatar Oct 18 '22 13:10 fatcerberus

Related: #20110

MartinJohns avatar Oct 18 '22 13:10 MartinJohns

Using include in an extends’d config seems useless in general (or at least highly situational)

@fatcerberus Consider projects which do not need to update the config in the extends (eg. they just want to include all files of all extensions). These projects can be very common in situations where:

  1. Companies create multiple projects based on their "boilerplate", with no customizations needed for include or exclude
  2. Students create multiple projects based on a programming boilerplate, with no customizations needed for include or exclude (this is our use case)

I think there are other scenarios for this type of boilerplate as well.

karlhorky avatar Jan 03 '23 12:01 karlhorky

Another idea for implementation would be to have a code-based config (ideally tsconfig.ts, as @weswigham mentions in https://github.com/microsoft/TypeScript/issues/20110) - I'm guessing this would open up alternative ways to retrieve the root directory of the project.

Allowing TS / JS configs is a common pattern in developer tooling, and covers many use cases beyond this issue.

But I think @mhegazy is against a code-based config tsconfig.ts.

Asking whether this can be reconsidered here: https://github.com/microsoft/TypeScript/issues/20110#issuecomment-1369722893

I will also add this to the issue description above.

karlhorky avatar Jan 03 '23 12:01 karlhorky

Using include in an extends’d config seems useless in general (or at least highly situational)

@fatcerberus It is useful in monorepositories where you share infrastructure configurations across multiple packages.

isqua avatar May 31 '23 14:05 isqua

I am using a monorepo, there is no way to control the output of relative file, I mean inclue, exclude, outdir, all of them are relative to the base conf. What I need is to have a base conf, that have settings that applies relatively to the current project. I do not have inheritence working for files's outputs with tsconfig.

AxeloLabs avatar Jun 20 '23 13:06 AxeloLabs

Considering the severe limitations and perf problems that typescript-eslint has with lots of tsconfig.json files in a repo, this is sorely needed.

shellscape avatar Feb 26 '24 21:02 shellscape

Thanks @sheetalkamat for implementing the ${configDir} template variable πŸŽ‰πŸš€

  • Issue: https://github.com/microsoft/TypeScript/issues/57485
  • PR: #58042

Looks like will be a part of TS 5.5:

  • https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#the-configdir-template-variable-for-configuration-files

karlhorky avatar Apr 19 '24 16:04 karlhorky