Skip to content

Commit

Permalink
Handle impliedNodeFormat when handling sourceFileCache
Browse files Browse the repository at this point in the history
Fixes #50872
  • Loading branch information
sheetalkamat committed Sep 27, 2022
1 parent 05d739b commit c8ebfb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 62 deletions.
15 changes: 9 additions & 6 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ namespace ts {
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 sourceFileCache = new Map<string, ESMap<SourceFile["impliedNodeFormat"], SourceFile>>();

const readFileWithCache = (fileName: string): string | undefined => {
const key = toPath(fileName);
Expand All @@ -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 forPath = sourceFileCache.get(key);
const value = forPath?.get(impliedNodeFormat);
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(key, (forPath || new Map()).set(impliedNodeFormat, sourceFile));
}
return sourceFile;
} : undefined;
Expand All @@ -228,7 +230,8 @@ namespace ts {
sourceFileCache.delete(key);
}
else if (getSourceFileWithCache) {
const sourceFile = sourceFileCache.get(key);
const sourceFileMap = sourceFileCache.get(key);
const sourceFile = sourceFileMap && firstDefinedIterator(sourceFileMap.values(), identity);
if (sourceFile && sourceFile.text !== data) {
sourceFileCache.delete(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,27 @@ Resolving real path for '/src/projects/node_modules/@types/pg/index.d.ts', resul
======== Type reference directive 'pg' was successfully resolved to '/src/projects/node_modules/@types/pg/index.d.ts', primary: true. ========
File '/lib/package.json' does not exist.
File '/package.json' does not exist.
src/projects/b/src/index.ts:1:8 - error TS1192: Module '"/src/projects/node_modules/@types/pg/index"' has no default export.
1 import pg from "pg";
   ~~
lib/lib.es2022.full.d.ts
Default library for target 'es2022'
src/projects/node_modules/@types/pg/index.d.ts
Imported via "pg" from file 'src/projects/b/src/index.ts'
Entry point for implicit type library 'pg'
File is CommonJS module because 'src/projects/node_modules/@types/pg/package.json' does not have field "type"
src/projects/b/src/index.ts
Matched by default include pattern '**/*'
File is ECMAScript module because 'src/projects/b/package.json' has field "type" with value "module"
Found 1 error.
exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
exitCode:: ExitStatus.Success
//// [/src/projects/a/src/index.js]
"use strict";
//// [/src/projects/b/src/index.js]
import pg from "pg";
pg.foo();
Change:: no-change-run
Expand All @@ -139,58 +137,14 @@ Input::
Output::
/lib/tsc -b /src/projects/a /src/projects/b --verbose --traceResolution --explainFiles
[[90m12:00:28 AM[0m] Projects in this build:
[[90m12:00:29 AM[0m] Projects in this build:
* src/projects/a/tsconfig.json
* src/projects/b/tsconfig.json
[[90m12:00:29 AM[0m] Project 'src/projects/a/tsconfig.json' is up to date because newest input 'src/projects/a/src/index.ts' is older than output 'src/projects/a/src/index.js'
[[90m12:00:30 AM[0m] Project 'src/projects/a/tsconfig.json' is up to date because newest input 'src/projects/a/src/index.ts' is older than output 'src/projects/a/src/index.js'
[[90m12:00:30 AM[0m] Project 'src/projects/b/tsconfig.json' is out of date because output file 'src/projects/b/src/index.js' does not exist
[[90m12:00:31 AM[0m] Project 'src/projects/b/tsconfig.json' is up to date because newest input 'src/projects/b/src/index.ts' is older than output 'src/projects/b/src/index.js'

[12:00:31 AM] Building project '/src/projects/b/tsconfig.json'...
File '/src/projects/b/src/package.json' does not exist.
Found 'package.json' at '/src/projects/b/package.json'.
'package.json' does not have a 'typesVersions' field.
======== Resolving module 'pg' from '/src/projects/b/src/index.ts'. ========
Module resolution kind is not specified, using 'Node16'.
File '/src/projects/b/src/package.json' does not exist according to earlier cached lookups.
File '/src/projects/b/package.json' exists according to earlier cached lookups.
Loading module 'pg' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/src/projects/b/src/node_modules' does not exist, skipping all lookups in it.
Directory '/src/projects/b/node_modules' does not exist, skipping all lookups in it.
Found 'package.json' at '/src/projects/node_modules/@types/pg/package.json'.
'package.json' does not have a 'typesVersions' field.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field 'index.d.ts' that references '/src/projects/node_modules/@types/pg/index.d.ts'.
File '/src/projects/node_modules/@types/pg/index.d.ts' exist - use it as a name resolution result.
Resolving real path for '/src/projects/node_modules/@types/pg/index.d.ts', result '/src/projects/node_modules/@types/pg/index.d.ts'.
======== Module name 'pg' was successfully resolved to '/src/projects/node_modules/@types/pg/index.d.ts'. ========
File '/src/projects/node_modules/@types/pg/package.json' exists according to earlier cached lookups.
======== Resolving type reference directive 'pg', containing file '/src/projects/b/__inferred type names__.ts', root directory '/src/projects/node_modules/@types'. ========
Resolving with primary search path '/src/projects/node_modules/@types'.
File '/src/projects/node_modules/@types/pg/package.json' exists according to earlier cached lookups.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field 'index.d.ts' that references '/src/projects/node_modules/@types/pg/index.d.ts'.
File '/src/projects/node_modules/@types/pg/index.d.ts' exist - use it as a name resolution result.
Resolving real path for '/src/projects/node_modules/@types/pg/index.d.ts', result '/src/projects/node_modules/@types/pg/index.d.ts'.
======== Type reference directive 'pg' was successfully resolved to '/src/projects/node_modules/@types/pg/index.d.ts', primary: true. ========
File '/lib/package.json' does not exist.
File '/package.json' does not exist.
lib/lib.es2022.full.d.ts
Default library for target 'es2022'
src/projects/node_modules/@types/pg/index.d.ts
Imported via "pg" from file 'src/projects/b/src/index.ts'
Entry point for implicit type library 'pg'
File is CommonJS module because 'src/projects/node_modules/@types/pg/package.json' does not have field "type"
src/projects/b/src/index.ts
Matched by default include pattern '**/*'
File is ECMAScript module because 'src/projects/b/package.json' has field "type" with value "module"
exitCode:: ExitStatus.Success


//// [/src/projects/b/src/index.js]
import pg from "pg";
pg.foo();


0 comments on commit c8ebfb8

Please sign in to comment.