Skip to content

Parser reports "Parsing error" for new files added to project (VSCode) #864

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

Closed
ArcanoxDragon opened this issue Aug 15, 2019 · 24 comments · Fixed by eggjs/egg-tslint-to-eslint#3 or #973
Labels
bug Something isn't working has pr there is a PR raised to close this locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: parser Issues related to @typescript-eslint/parser

Comments

@ArcanoxDragon
Copy link

What code were you trying to parse?

A new React component module. This happens on all new TypeScript source files, however. This is being triggered from the instance of ESLint that VSCode runs in the background, meaning that in order to get this error to go away, I have to reload the entire VSCode window.

import React from "react";

import { EditorProps } from ".";
import { Switch } from "@material-ui/core";

export function AreaListMode( { settings, updateSettings }: EditorProps ) {
    return <Switch checked={ settings.areaListStyle } onChange={ e => updateSettings( { areaListStyle: e.target.checked ? "compact" : "default" } ) } />;
}

tsconfig.json include:

"include": [
	"projects/*/typings/**/*",
	"projects/*/src/**/*"
],

What did you expect to happen?
The file should be parsed normally, as its path is matched by the include property in my tsconfig.json file.

What actually happened?
A "Parsing error" is reported on the first line of any file that is created while the VSCode linter instance is running, saying that the file is not included in the project. The error does not go away unless I restart VSCode (or reload the window).

image

Versions

package version
@typescript-eslint/parser 2.0.0
TypeScript 3.4.5
ESLint 6.1.0
node 12.7.0
npm yarn 1.17.3
@ArcanoxDragon ArcanoxDragon added package: parser Issues related to @typescript-eslint/parser triage Waiting for team members to take a look labels Aug 15, 2019
@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for team members to take a look labels Aug 15, 2019
@bradzacher
Copy link
Member

cc @uniqueiniquity - it looks like maybe we haven't handled watch mode well enough?

@uniqueiniquity
Copy link
Contributor

Yeah... this makes sense to me. Currently, once we determine the source files of a tsconfig, we won't add to it - that is, we're not watching for new files in case we need to update.

@bradzacher
Copy link
Member

how does vscode's typescript integration do it?
I'm guessing there's support we need to specifically build in for watch mode?

@uniqueiniquity
Copy link
Contributor

In VS Code, we use TSServer, which handles of the file watching, project creation, and everything else. What we do in typescript-estree is at a lower level in the TS architecture, where we interact directly with the Node API for the TS compiler itself. By stepping up to a higher level of the TS architecture, we'd get more pieces handled for us (including this file watching piece), but may incur more performance overhead.

@bradzacher
Copy link
Member

interesting. We should probably look into the mechanisms that the eslint vscode plugin uses to setup its watch mode - maybe there's something we can listen to to help inform how we watch.

@ArcanoxDragon
Copy link
Author

I should note that this is actually also happening during the Webpack build, i.e. eslint-loader reports the same error as the VSCode extension for new files. I didn't notice that until now.

@bradzacher
Copy link
Member

If it's happening during webpack watch mode, then it's the same issue.

If it's happening on a single build - then your tsconfig isn't including the files.

@G-Rath
Copy link
Contributor

G-Rath commented Aug 20, 2019

I'm seeing this behaviour in IntelliJ as well, so I suspect they share a root cause.

I'd image WebStorms handling of TS is similar enough to vscodes that the same fix should work for both, but someone like @prigara would be able to tell you for sure (+ I figure they'd interested in knowing about this issue).

Since there isn't way to restart the eslint service (WEB-38066 wink wink nudge nudge), I have to open & close the project everytime I add a new file in some places.

No pressure or nothing, but this is actually super annoying 😂

Let me know if there's anything I can do to help - happy to do testing & what-have-you w/ IntelliJ/WebStorm on my end, and can report an issue on YouTrack if needed 🙂


On a side note: would it be possible/worth having an option to revert back to the pre-2.0.0 behaviour in the meantime?

@bradzacher bradzacher mentioned this issue Aug 20, 2019
3 tasks
@bradzacher
Copy link
Member

bradzacher commented Aug 20, 2019

@G-Rath see the information in #890
Please make sure that first you can do a CLI run without any parsing errors.

If that's working for you, then feel free to add createDefaultProgram to your config. (See parser docs for more info on this option and why you shouldn't use it unless you really have to).


We're going to look into a proper fix.

@G-Rath
Copy link
Contributor

G-Rath commented Aug 20, 2019

@bradzacher thanks for the link - I didn't see that issue, but I did see the includes thing in the release notes.

I've confirmed that in this project, running npm run lint gives:

js-bugs@0.0.0 lint C:\Users\G-Rath\workspace\projects-personal\js-bugs
eslint "**/*.{t,j}s{x,}"

C:\Users\G-Rath\workspace\projects-personal\js-bugs\src\index.ts
0:0 error Parsing error: If "parserOptions.project" has been set for @typescript-eslint/parser, C:\Users\G-Rath\workspace\projects-personal\js-bugs\src\index.ts must be included in at least one of the projects provided

✖ 1 problem (1 error, 0 warnings)

As expected since src isn't included, but WebStorm gives that if I make a new .ts file in configs/, which is included via tsconfig.eslint.json.

createDefaultProgram: true fixes this as expected.

Let me know if you'd like me to make a new webstorm-specific issue 🙂

@bradzacher
Copy link
Member

I only raised #890 this morning, to try and consolidate the discussion going forward because people were commenting on closed issues.


Yup, it doesn't matter if it's via eslint --watch, or via an IDE plugin - the mechanism is essentially the same. ESLint runs continually, and sends files to our parser.

Because eslint never "dies", our parser maintains its state - you launch the IDE, it starts eslint, which triggers our parser, which triggers typescript and detects all of the valid files matched by your tsconfig.

This is great for existing files, as we only need to do a small bit of work when you update the file.

However with new files, it means that our project state doesn't contain the brand new file. Unfortunately, we don't currently have a mechanism for telling typescript to "rescan" the workspace to detect new files. Which means ofc your file just throws an error.

createDefaultProgram essentially allows the parser to use old code path. This means that when the parser detects a file that hasn't been parsed, it treats it as the root of a new program, and parses the file and its dependencies. Less performant by a long shot, but covers all the bases.
The reason that it's bad turning this function on, is because you could get caught with a slow lint outside of the IDE because you added a file that's not included in your tsconfig. So yeah, use it with caution! It'll unblock you fine though.


We will figure out how to build the "rescan" functionality above, so that you can turn off createDefaultProgram.

@G-Rath
Copy link
Contributor

G-Rath commented Aug 20, 2019

Yup, it doesn't matter if it's via eslint --watch, or via an IDE plugin - the mechanism is essentially the same. ESLint runs continually, and sends files to our parser.

That's interesting, b/c it's fine if I do eslint config/index3.ts or run npm run lint, which is what I'd expect IDE plugins & tools to be doing if not eslint --watch.

We will figure out how to build the "rescan" functionality above, so that you can turn off createDefaultProgram.

Looking forward to it - as always, let me know if I can do anything to help (but I suspect in this case I'll be less able to help than w/ rewriting indent & co) 🙂


btw thanks for the quick & detailed responses!

@bradzacher
Copy link
Member

In a nutshell, you'll probably find that the IDEs use eslint's internal APIs to trigger a parse of the file based on the file contents the IDE loaded. This is how they do parses whilst you're still typing without saving!

In the interest of saving time and memory however, the IDE will essentially maintain its NodeJS instance, so as far as our parser is concerned - it's one big, long, continuous, never ending lint session.

https://eslint.org/docs/developer-guide/nodejs-api#cliengineexecuteontext

const cli = new CLIEngine(require('.eslintrc.js'));

ide.onFileContentChange((contents, filename) => {
  const report = cli.executeOnText(contents, filename);
});

@jbinto

This comment has been minimized.

aereal added a commit to aereal/musik that referenced this issue Aug 28, 2019
MaximDevoir added a commit to MaximDevoir/labels-manager that referenced this issue Aug 28, 2019
- If we include `test` or `typings` files, we get a compiler error related to microsoft/TypeScript#9858.

- If we exclude (not added to `include`) `test` or `typings` files, we get a lint "Parsing error". Related to typescript-eslint/typescript-eslint#864

We will go with the linter error. Better to have a non-zero return code for the `lint` script compared with the `build` script.
@bradzacher

This comment has been minimized.

@StephenHaney
Copy link

StephenHaney commented Oct 3, 2019

Hey all – just checking in if there's an ETA or a workaround patch... finding that this error is leading developers to disable linting in their IDEs completely. Should we step down to 1.13 in the meantime or is a fix close? Thanks for all the work on this!

@ianwremmel
Copy link

not sure if it's a workaround, exactly, but I use cmd+shift+p and then Developer: Reload Window to get it to pick up new files I've created. It's definitely not ideal, but seems way easier than either downgrading or disabling linting.

@threehams
Copy link
Contributor

The workaround is createDefaultProgram: true added to parserOptions in your eslint config, mentioned in comments about halfway down this page.

Alrefai added a commit to Alrefai/next-tip-calc that referenced this issue Oct 6, 2019
Parser reports "Parsing error" for new files added to project (VSCode) #864
typescript-eslint/typescript-eslint#864
@JamesHenry
Copy link
Member

Now that the file watching has been added, it should no longer be necessary to use createDefaultProgram: true as a way to avoid errors for new files.

createDefaultProgram: true should be avoided whenever possible as it can mask incorrect file glob configurations by users and leads to potentially huge performance hits on lint runs.

@skovhus
Copy link

skovhus commented Oct 14, 2019

When do you plan to release it? :)

@bradzacher
Copy link
Member

https://github.com/typescript-eslint/typescript-eslint/blob/master/README.md#package-versions

Automated release schedule is listed in the readme.

@skovhus
Copy link

skovhus commented Oct 14, 2019

Thanks!

@fubar
Copy link

fubar commented Oct 15, 2019

@bradzacher this is still occurring in Intellij on the latest 2.4.0 (https://github.com/typescript-eslint/typescript-eslint/releases/tag/v2.4.0) which includes your PR #973

Versions

package version
@typescript-eslint/parser 2.4.0
TypeScript 3.6.4
ESLint 6.5.1
node 11.10.1

@bradzacher
Copy link
Member

please raise a new issue with some repro steps.
"this is still occurring" doesn't provide any information for us to debug or investigate.

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Oct 15, 2019
@bradzacher bradzacher added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Apr 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working has pr there is a PR raised to close this locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: parser Issues related to @typescript-eslint/parser
Projects
None yet