-
Notifications
You must be signed in to change notification settings - Fork 30
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
Use in-memory program to generate strict diagnostics for plugin #62
Conversation
I've released beta version with this code https://www.npmjs.com/package/typescript-strict-plugin?activeTab=readme and I will test on my repo if this works. Thanks for contribution |
Unfortunately with this change I have this view in my VSCode. Tested with |
Do you have any minimal repro steps? I tried the version you released the follow way: npm init -y
npm install [email protected] [email protected] @types/node
{
"compilerOptions": {
"strict": false,
"plugins": [
{
"name": "typescript-strict-plugin"
}
]
}
}
import { resolve } from "path";
let foo: number | undefined = 42;
foo = undefined;
resolve("."); Then:
Result: No errors as expected. If I remove I have not tried the released version yet on our 100k+ LOC repo due to Christmas holidays so can't comment on that yet. |
I've tried on my 100k+ LOC project, so I don't have repro steps, but I will try to find how to reproduce it |
Ok, i think I found it. I think that now the plugin reports errors from all of the files in the project. import { resolve } from "path";
//
//
let foo2: number | undefined = 42;
foo2 = undefined;
resolve("."); and then, in |
So I added the a file However, the issue you are having should occur if |
Our project setup looks the same and we have the same version of VSCode. Maybe due to OS (Linux vs macOS) or how vscode is setup? FWIW I cannot reproduce the issue in neovim (using typescript-tools) either. |
I'm using macOS and I can reproduce it in neovim using |
Hmmm... In my case I'm always getting Info 85 [13:27:37.275] [typescript-strict-plugin]: Test SourceFile: undefined, Path: /Users/.../Documents/Work/test/index.ts, languageVersionOrOptions: undefined
Info 86 [13:27:37.275] [typescript-strict-plugin]: Could not obtain SourceFile from language service and 'languageVerisionOrOptions' is not set for path '/Users/.../Documents/Work/test/index.ts'
Info 87 [13:27:37.275] [typescript-strict-plugin]: Could not obtain SourceFile for file with path '/Users/.../Documents/Work/test/index.ts' |
I'm seeing the same behavior and logs for for the beta version on on mac. For anyone else who maybe wants to contribute: A plugin is really a Just thinking about this idea of maintaining two parallel language services and switching between diagnostics in the proxy. The function create(info) {
// Proxy LanguageServiceHost to override compilation settings.
const strictHost = {
...info.languageServiceHost,
getCompilationSettings: () => ({
...info.languageServiceHost.getCompilcationSettings(),
strict: true,
}),
};
const strictLanguageServer = ts.createLanguageServer(strictHost);
proxy.getSemanticDiagnostics = function (filePath) {
const strictFile = new PluginStrictFileChecker(info).isFileStrict(filePath);
if (strictFile) {
return strictLanguageServer.getSemanticDiagnostics(filePath);
} else {
return info.languageService.getSemanticDiagnostics(filePath);
}
};
// Maybe also forward cleanupSemanticCache and dispose calls.
} Just spitballing. Will play around with it more if I've got time. |
Tested my approach in a branch here. Needs some more work, but looking promising! Tested in my work repo and it's working well. |
The PR changes the strategy used to generate the strict diagnostics in the plugin by keeping an in-memory version of the TypeScript program instead of mutating the existing program used by the language service.
The in-memory version is a
SemanticDiagnosticsBuilderProgram
which the same type used in the TypeScript API example "Writing an incremental program watcher". This program type is partially internal since it requires thatSourceFile
s produced by the compiler host are versioned (which is an internal property). Luckily the language service produces versioned source files of the files currently being edited. I have only found one case where this does not happen which is when importing from reference projects, in particular it seems the language service does not keep track of the declaration files of referenced projects. In this case we fallback to the standard compiler host and generate a file version using a hashing algorithm which is also used as a fallback in the TypeScript repo.I have tried to document assumptions made. This PR does not contain any tests and I have not looked at the non-plugin code to see if there is any possibility for code sharing. I have mainly implemented this to resolve #61 for a project where I introduced this plugin (and would be very sad to lose it).
Fixes: #61