mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 10:29:18 -05:00
Set impliedNodeFormat on sourceFile we get from host to ensure the field is set correctly (#50977)
* Add test that fails * Handle impliedNodeFormat when handling sourceFileCache Fixes #50872 * Revert the fix * Make sure impliedNodeFormat is set for the sourceFile * Revert "Make sure impliedNodeFormat is set for the sourceFile" This reverts commit651a47fdd6. * Revert "Revert the fix" This reverts commit5c98b9cfc1. * Swap the keys for map
This commit is contained in:
@@ -168,10 +168,10 @@ namespace ts {
|
||||
const originalDirectoryExists = host.directoryExists;
|
||||
const originalCreateDirectory = host.createDirectory;
|
||||
const originalWriteFile = host.writeFile;
|
||||
const readFileCache = new Map<string, string | false>();
|
||||
const fileExistsCache = new Map<string, boolean>();
|
||||
const directoryExistsCache = new Map<string, boolean>();
|
||||
const sourceFileCache = new Map<string, SourceFile>();
|
||||
const readFileCache = new Map<Path, string | false>();
|
||||
const fileExistsCache = new Map<Path, boolean>();
|
||||
const directoryExistsCache = new Map<Path, boolean>();
|
||||
const sourceFileCache = new Map<SourceFile["impliedNodeFormat"], ESMap<Path, SourceFile>>();
|
||||
|
||||
const readFileWithCache = (fileName: string): string | undefined => {
|
||||
const key = toPath(fileName);
|
||||
@@ -196,14 +196,16 @@ namespace ts {
|
||||
return setReadFileCache(key, fileName);
|
||||
};
|
||||
|
||||
const getSourceFileWithCache: CompilerHost["getSourceFile"] | undefined = getSourceFile ? (fileName, languageVersion, onError, shouldCreateNewSourceFile) => {
|
||||
const getSourceFileWithCache: CompilerHost["getSourceFile"] | undefined = getSourceFile ? (fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile) => {
|
||||
const key = toPath(fileName);
|
||||
const value = sourceFileCache.get(key);
|
||||
const impliedNodeFormat: SourceFile["impliedNodeFormat"] = typeof languageVersionOrOptions === "object" ? languageVersionOrOptions.impliedNodeFormat : undefined;
|
||||
const forImpliedNodeFormat = sourceFileCache.get(impliedNodeFormat);
|
||||
const value = forImpliedNodeFormat?.get(key);
|
||||
if (value) return value;
|
||||
|
||||
const sourceFile = getSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile);
|
||||
const sourceFile = getSourceFile(fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile);
|
||||
if (sourceFile && (isDeclarationFileName(fileName) || fileExtensionIs(fileName, Extension.Json))) {
|
||||
sourceFileCache.set(key, sourceFile);
|
||||
sourceFileCache.set(impliedNodeFormat, (forImpliedNodeFormat || new Map()).set(key, sourceFile));
|
||||
}
|
||||
return sourceFile;
|
||||
} : undefined;
|
||||
@@ -225,13 +227,15 @@ namespace ts {
|
||||
const value = readFileCache.get(key);
|
||||
if (value !== undefined && value !== data) {
|
||||
readFileCache.delete(key);
|
||||
sourceFileCache.delete(key);
|
||||
sourceFileCache.forEach(map => map.delete(key));
|
||||
}
|
||||
else if (getSourceFileWithCache) {
|
||||
const sourceFile = sourceFileCache.get(key);
|
||||
if (sourceFile && sourceFile.text !== data) {
|
||||
sourceFileCache.delete(key);
|
||||
}
|
||||
sourceFileCache.forEach(map => {
|
||||
const sourceFile = map.get(key);
|
||||
if (sourceFile && sourceFile.text !== data) {
|
||||
map.delete(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
originalWriteFile.call(host, fileName, data, ...rest);
|
||||
};
|
||||
|
||||
@@ -41,6 +41,7 @@ namespace fakes {
|
||||
}
|
||||
|
||||
public write(message: string) {
|
||||
if (ts.Debug.isDebugging) console.log(message);
|
||||
this.output.push(message);
|
||||
}
|
||||
|
||||
|
||||
@@ -985,6 +985,7 @@ interface Array<T> { length: number; [n: number]: T; }`
|
||||
}
|
||||
|
||||
write(message: string) {
|
||||
if (Debug.isDebugging) console.log(message);
|
||||
this.output.push(message);
|
||||
}
|
||||
|
||||
|
||||
@@ -86,4 +86,36 @@ namespace ts.tscWatch {
|
||||
commandLineArgs: ["-b", "/src/packages/pkg1.tsconfig.json", "/src/packages/pkg2.tsconfig.json", "--verbose", "--traceResolution"],
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe("unittests:: tsbuild:: moduleResolution:: impliedNodeFormat differs between projects for shared file", () => {
|
||||
verifyTscWithEdits({
|
||||
scenario: "moduleResolution",
|
||||
subScenario: "impliedNodeFormat differs between projects for shared file",
|
||||
fs: () => loadProjectFromFiles({
|
||||
"/src/projects/a/src/index.ts": "",
|
||||
"/src/projects/a/tsconfig.json": JSON.stringify({
|
||||
compilerOptions: { strict: true }
|
||||
}),
|
||||
"/src/projects/b/src/index.ts": Utils.dedent`
|
||||
import pg from "pg";
|
||||
pg.foo();
|
||||
`,
|
||||
"/src/projects/b/tsconfig.json": JSON.stringify({
|
||||
compilerOptions: { strict: true, module: "node16" }
|
||||
}),
|
||||
"/src/projects/b/package.json": JSON.stringify({
|
||||
name: "b",
|
||||
type: "module"
|
||||
}),
|
||||
"/src/projects/node_modules/@types/pg/index.d.ts": "export function foo(): void;",
|
||||
"/src/projects/node_modules/@types/pg/package.json": JSON.stringify({
|
||||
name: "@types/pg",
|
||||
types: "index.d.ts",
|
||||
}),
|
||||
}),
|
||||
modifyFs: fs => fs.writeFileSync("/lib/lib.es2022.full.d.ts", libFile.content),
|
||||
commandLineArgs: ["-b", "/src/projects/a", "/src/projects/b", "--verbose", "--traceResolution", "--explainFiles"],
|
||||
edits: noChangeOnlyRuns
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user