-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Feature request: support looking up tsconfig.json relative to linted file #101
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
Comments
I think the rationale here was to avoid generating some objects needed to support the type checking if the user doesn’t request them. |
Got hit by this as well. I'd like to use the new
Can I say "please generate the objects needed, I want type info"? Seems like something that should be a boolean turning "look up closest tsconfig.json" on or off |
|
Yeah, something like that'd be perfect :) |
This comment was marked as spam.
This comment was marked as spam.
We won't ever auto-detect the project without a config item. As soon as we do that, we would force every user to go through a complete typescript compile by just including one of rules. However with something like This can be worked around using module.exports = {
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
} https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#configuration If you don't specify i.e. if you were to unravel all of our code and typescript's code, you'll see that essentially the algorithm for resolving the provided path is essentially: const rawConfig = fs.readFileSync(
path.resolve(
process.cwd(),
options.tsconfigRootDir || process.cwd(), // note path.resolve(process.cwd(), process.cwd()) === process.cwd()
options.project,
),
); |
The idea was not to "autodetect when a rule that needs it is included", but to have an option where which The requirement to be able to specify It is a possible workaround, but one that is harder to make use of than an option to enable searching upwards for a |
If you have a tsconfig.json in your root (which I think you need for vscode to handle it??) then it will work as expected as long as your compiler options are consistent between projects in a monorepo.
I didn't think this was a huge problem - from the eyes of your codebase, it's 2 letters in an extension plus a one line change? Don't get me wrong, I do understand that it's not a perfect solution, but it's a workaround for now. |
This would be extremely useful in monorepos, where you have some projects with per-project unique rulesets and you want to be able to run eslint from the monorepo root and the projects roots as well. I prefer yaml for configuration and |
@bencelang I have bad news for you. I would migrate away from YAML to JS to avoid future pain. |
@bradzacher Thanks for the heads up! I'll give the JS migration a shot throughout my projects.
However, are ESM config files expected to be supported at some point? One of the reasons why we started using YAML configuration was to help avoid mixed commonjs/esm codebases |
That RFC was added in 2019, before ESM was properly supported by node. ESLint has since added support for the The ESLint team is currently discussing the migration to ESM. It's a complicated topic to navigate for such a prominent package. |
This issue is very old but very juicy - I'd love to be able to shorthand Edit: this might land in v5 anyway |
It's worth noting that this would cause some perf regressions because disk lookups are going to be more expensive than a static config. Also depending on project setup you might have some "useless" tsconfigs that we waste time interrogating before finding the correct tsconfig for a file. In terms of the config style -
The complicated thing in this algorithm will be determining which tsconfig to use and cache it. It's completely possible for two files in the same folder to be covered by different tsconfigs - so we can't just assume all files in a given folder are covered by the same tsconfig. This means that for every single file we'll need to do a lookup to figure out the tsconfig to use. I believe we can use TS's infra to parse the tsconfig and get the list of files it contains without having to go through the expensive program generation (which should save some time). For CLI runs we can assume tsconfigs are constant and aggressively cache all this stuff so we only need calculate it once per tsconfig. It should be relatively simple to implement for this usecase. For persistent IDE runs it's going to be a lot more complicated because we can't attach disk watchers and we can't assume that tsconfigs are static in content or location. I don't know the best way for us to handle this - maybe we need to investigate how vscode does it? I assume they probably have file watchers. We have invalidation logic which handles the "tsconfig was changed" usecase, but I don't know how we can efficiently handle the "tsconfig was added/moved" usecase without watchers. Maybe we'll just need to fuzzily cache for some time period for perf reasons and hope that it doesn't cause too much weirdness? Not sure what the best solution will be! |
Agreed, and that feels like a followup issue to me 😄. Let's get
Yeah, I've been playing with this. I verified locally that |
Currently, the
eslint-plugin-tslint
throws if any of:or
lintFile
are missing. IflintFile
is missing it'll just lint nothing instead of throwing, unless there are inline configs.parserOptions.project
is completely undocumented in@typescript-eslint/parser
, and at any rate that would need to be autodetectable for this use case to work. Currently, services are just silently not generated if it's missing:This is despite there being a
getASTAndDefaultProject
, which looks like it could do the right thing. It's just unreachable code unless you already gave a project to begin with.On further reading, it looks like the
tsconfig.json
can be relative, but it's by default calculated relative toprocess.cwd()
and not to the currenteslintrc
being processed, or an ancestor lookup relative to the current file. The option to change that (tsconfigRootDir
) is also undocumented and still can't do what I want.Secondly, it should be possible to make the
tslint.json
also be relative to either the currenttsconfig.json
, the currenteslintrc
, or to also do a ancestor lookup from the current file.I think that for most people's use cases the
process.cwd()
orpath.resolve(__dirname, "tslint.json")
approach could work, but in my use case I'm trying to make a single shareable eslint config for a monorepo and simply reuse it with"eslintConfig": { "extends": ["@scope/eslint-config"] }
. This works for me for everything except these use cases. Note thatpath.resolve(__dirname, "tslint.json")
would still be wrong if a specific project has a differenttslint.json
than the one the shared eslint-config has.Even if I leave it to run per project, I still have some specific folders that have a different
tsconfig.json
nested inside an outertsconfig.json
that belongs to the same project. e.g. a service worker would haveworker
instead ofdom
in itslib
config. I can still probably deal with this through refactoring, but it sounds like something that could/should be supported; or at least vscode can support it.The text was updated successfully, but these errors were encountered: