-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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/vfs: Module resolution on createProgram breaks #2801
Comments
I did some discussions in discord and walked through the problem with a debugger. The files that are missing from the system are as follows:
And I've deviced a small hotfix for the issue: import ts, { CompilerOptions } from "typescript";
import fs from "fs";
import { createDefaultMapFromNodeModules, createSystem, createVirtualCompilerHost, } from "@typescript/vfs";
import path from "path";
const compilerOptions: CompilerOptions = {
target: ts.ScriptTarget.ESNext,
}
const fsMap = createDefaultMapFromNodeModules(compilerOptions, ts);
applyHotfix(fsMap);
fsMap.set("test.ts", 'console.log("Foo")')
const system = createSystem(fsMap);
const host = createVirtualCompilerHost(system, compilerOptions, ts);
const typeCheckProgram = ts.createProgram({
rootNames: [...fsMap.keys()],
options: compilerOptions,
host: host.compilerHost
});
console.log(typeCheckProgram.getSourceFile("test.ts"));
function applyHotfix(fsMap: Map<string, string>) {
let hotfixFiles = [
"lib.es5.d.ts",
"lib.decorators.d.ts",
"lib.decorators.legacy.d.ts",
"lib.dom.d.ts",
"lib.webworker.importscripts.d.ts",
"lib.scripthost.d.ts"
];
function loadTypescriptLibFile(filename: string) {
const libDir = path.dirname(require.resolve("typescript"));
return fs.readFileSync(path.resolve(libDir, filename), "utf-8");
}
hotfixFiles.forEach(f => {
fsMap.set("/" + f, loadTypescriptLibFile(f));
});
} The VFS system seems to not include required files for the compiler to work. I also noticed that using the One fix for the type declaration file mismatch would be to query the tslib path in local node setups to enumerate what files are present and only using those instead of a hardcoded list. For web setups this ofcourse is not suitable but for node setups it would be. |
#2802 aims to fix some of the enumeration errors where some hardcoded libraries don't exist |
With a "by documentation" example of vfs setup, the program breaks when trying to create it.
Repro:
Expected behavior:
The Program gets created and is usable
Actual behavior:
The program creation crashes with the stacktrace
The text was updated successfully, but these errors were encountered: