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

TypeScript LanguageService plugin #408

Closed
dcecile opened this issue Sep 5, 2018 · 5 comments · Fixed by #492
Closed

TypeScript LanguageService plugin #408

dcecile opened this issue Sep 5, 2018 · 5 comments · Fixed by #492
Assignees

Comments

@dcecile
Copy link
Contributor

dcecile commented Sep 5, 2018

What do you think about having Wotan act as a plugin for the TypeScript compiler?

I'm imagining an ideal development loop that includes running lint checks continuously together with continuously compiling my TypeScript code.

If Wotan could be used as TypeScript compiler plugin (like tslint-language-service), then maybe this use case could be supported for tsc --watch, Webpack, or any other build system?

Edit: Oops, it looks like TypeScript plugins aren't as powerful as I'd hoped, and wouldn't work for failing a tsc build...

TypeScript Language Service Plugins ("plugins") are for changing the editing experience only. The core TypeScript language remains the same. Plugins can't add new language features such as new syntax or different typechecking behavior, and plugins aren't loaded during normal commandline typechecking or emitting.
https://github.com/Microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin

@ajafff ajafff changed the title Linting together with compile? TypeScript LanguageService plugin Sep 5, 2018
@ajafff
Copy link
Member

ajafff commented Sep 5, 2018

Thank you for the suggestion @dcecile. I think it's really helpful that you pointed out this doesn't change compiler behavior and is only relevant for editors.

This is somewhat related to #396 as the VSCode plugin could simply inject a language service plugin. Unfortunately it would need to change how the language service reads files (to allow for processors). In addition the linter runtime, each rule and every library (e.g. tsutils) used along the way needs to use the TypeScript version powering the language service instead of just using import * as ts from 'typescript';.

IIRC tslint-language-service also has some issues with regard to adding fixable diagnostics and uses a really hacky solution for that.

There's a somewhat related feature request microsoft/TypeScript#15589 to integrate linting functionality directly into TypeScript.


Regarding your initial request of integrating with the compiler:

That could be possible by wrapping tsc. Though that requires a lot of effort to make it work with tsc --build or tsc --watch. And even if one could get this to work, it would only be available as separate CLI tool that's not automatically picked up by other tools using the TypeScript compiler.

@ajafff
Copy link
Member

ajafff commented Dec 11, 2018

The initial implementation is available in branch ls-plugin. It works, but has some rough edges.
It will be shipped in one of the next nightly builds.

Blocked on merging #482
Related upstream issues for improved user experience:
microsoft/TypeScript#28911,
microsoft/TypeScript#28937,
microsoft/TypeScript#28966,
microsoft/TypeScript#28969,
https://github.com/Microsoft/vscode/issues/64853,
microsoft/vscode#64848

@ajafff
Copy link
Member

ajafff commented Dec 17, 2018

For those willing to test the LanguageService plugin: In about 3 hours there will be a new nightly release that includes the plugin. The documentation is located here: https://github.com/fimbullinter/wotan/blob/master/packages/mithotyn/README.md

Note: to install the nightly release you need to execute

npm install --save-dev @fimbul/wotan@next @fimbul/mithotyn@next
# or
yarn add -D @fimbul/wotan@next @fimbul/mithotyn@next

Any kind of feedback is very much appreciated.

@dcecile
Copy link
Contributor Author

dcecile commented Dec 27, 2018

@ajafff I tried out the LanguageService plugin and everything worked well! 👍 The Wotan errors show up as I type according to my Wotan config, and I can switch them to show as warnings using the plugin setting.

In the process I also ran into a couple VS Code things...

To find the plugin, I found that VS Code's TypeScript version definitely needs to be set to the workspace version from node_modules. By chance I'd moved this VS Code setting from a user setting to a workspace setting and apparently this "reverts" the setting (needs to be reactivated). I didn't realize this had happened until I went through the steps of running TSS_LOG="-logToFile true -file /tmp/tss.log -level verbose" code to find out why Wotan was silently not working.

For VS Code's TypeScript support in general, unfortunately errors and warnings don't get shown for files that aren't open (tracked in microsoft/vscode#13953). So the same thing applies to Wotan as a TypeScript plugin. To see the Wotan errors, you need to actually open up the file. Making things more complicated, the Wotan plugin doesn't get run via tsc --watch (the standard workaround to this VS Code issue) because plugins are only supposed to be for editors. I think that a full, live picture of all Wotan errors in a codebase would need auto-running Wotan on file changes (possible with an extension), a fix for the above VS Code issue, or a watch mode for Wotan itself (#479).

@ajafff
Copy link
Member

ajafff commented Dec 31, 2018

@dcecile Thank you for the detailed feedback.

I had a similar problem with VSCode silently reverting my setting to using the bundled TypeScript version instead of the workspace version. I guess that happened during an update.
On a related note: you can enable TS Server logging in VSCode's settings and use TypeScript: Open TS Server log to open the log file.

To fix this I'm putting together a VSCode plugin to automatically inject the LanguageService plugin when using the bundled TypeScript version. #396 basically tracks that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants