Skip to content

Commit

Permalink
Cherry-pick PR #50977 into release-4.9 (#51363)
Browse files Browse the repository at this point in the history
Component commits:
05d739b Add test that fails

c8ebfb8 Handle impliedNodeFormat when handling sourceFileCache Fixes #50872

5c98b9c Revert the fix

651a47f Make sure impliedNodeFormat is set for the sourceFile

2c22989 Revert "Make sure impliedNodeFormat is set for the sourceFile"
This reverts commit 651a47f.

92bf32d Revert "Revert the fix"
This reverts commit 5c98b9c.

5f7bc05 Merge branch 'main' into tsBuildImpliedNodeFormat

4bd8b9b Swap the keys for map

Co-authored-by: Sheetal Nandi <shkamat@microsoft.com>
  • Loading branch information
typescript-bot and sheetalkamat committed Oct 31, 2022
1 parent 1e2fa7a commit 31bee56
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/compiler/program.ts
Expand Up @@ -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);
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 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;
Expand All @@ -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);
};
Expand Down
1 change: 1 addition & 0 deletions src/harness/fakesHosts.ts
Expand Up @@ -41,6 +41,7 @@ namespace fakes {
}

public write(message: string) {
if (ts.Debug.isDebugging) console.log(message);
this.output.push(message);
}

Expand Down
1 change: 1 addition & 0 deletions src/harness/virtualFileSystemWithWatch.ts
Expand Up @@ -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);
}

Expand Down
34 changes: 33 additions & 1 deletion src/testRunner/unittests/tsbuild/moduleResolution.ts
Expand Up @@ -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
});
});
}
@@ -0,0 +1,151 @@
Input::
//// [/lib/lib.d.ts]
/// <reference no-default-lib="true"/>
interface Boolean {}
interface Function {}
interface CallableFunction {}
interface NewableFunction {}
interface IArguments {}
interface Number { toExponential: any; }
interface Object {}
interface RegExp {}
interface String { charAt: any; }
interface Array<T> { length: number; [n: number]: T; }
interface ReadonlyArray<T> {}
declare const console: { log(msg: any): void; };

//// [/lib/lib.es2022.full.d.ts]
/// <reference no-default-lib="true"/>
interface Boolean {}
interface Function {}
interface CallableFunction {}
interface NewableFunction {}
interface IArguments {}
interface Number { toExponential: any; }
interface Object {}
interface RegExp {}
interface String { charAt: any; }
interface Array<T> { length: number; [n: number]: T; }

//// [/src/projects/a/src/index.ts]


//// [/src/projects/a/tsconfig.json]
{"compilerOptions":{"strict":true}}

//// [/src/projects/b/package.json]
{"name":"b","type":"module"}

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


//// [/src/projects/b/tsconfig.json]
{"compilerOptions":{"strict":true,"module":"node16"}}

//// [/src/projects/node_modules/@types/pg/index.d.ts]
export function foo(): void;

//// [/src/projects/node_modules/@types/pg/package.json]
{"name":"@types/pg","types":"index.d.ts"}



Output::
/lib/tsc -b /src/projects/a /src/projects/b --verbose --traceResolution --explainFiles
[12:00:22 AM] Projects in this build:
* src/projects/a/tsconfig.json
* src/projects/b/tsconfig.json

[12:00:23 AM] Project 'src/projects/a/tsconfig.json' is out of date because output file 'src/projects/a/src/index.js' does not exist

[12:00:24 AM] Building project '/src/projects/a/tsconfig.json'...

======== Resolving type reference directive 'pg', containing file '/src/projects/a/__inferred type names__.ts', root directory '/src/projects/node_modules/@types'. ========
Resolving with primary search path '/src/projects/node_modules/@types'.
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'.
======== Type reference directive 'pg' was successfully resolved to '/src/projects/node_modules/@types/pg/index.d.ts', primary: true. ========
lib/lib.d.ts
Default library for target 'es3'
src/projects/a/src/index.ts
Matched by default include pattern '**/*'
src/projects/node_modules/@types/pg/index.d.ts
Entry point for implicit type library 'pg'
[12:00:26 AM] Project 'src/projects/b/tsconfig.json' is out of date because output file 'src/projects/b/src/index.js' does not exist
[12:00:27 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'.
Resolving in ESM mode with conditions 'node', 'import', 'types'.
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.
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'.
======== 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/a/src/index.js]
"use strict";
//// [/src/projects/b/src/index.js]
import pg from "pg";
pg.foo();
Change:: no-change-run
Input::
Output::
/lib/tsc -b /src/projects/a /src/projects/b --verbose --traceResolution --explainFiles
[12:00:29 AM] Projects in this build:
* src/projects/a/tsconfig.json
* src/projects/b/tsconfig.json
[12:00:30 AM] 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'
[12:00:31 AM] 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'

exitCode:: ExitStatus.Success


0 comments on commit 31bee56

Please sign in to comment.