Skip to content

Commit fb609c0

Browse files
committed
Persist resolutions in services
1 parent d0947f2 commit fb609c0

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

src/compiler/watchPublic.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ namespace ts {
44
getCurrentDirectory(): string;
55
readFile(fileName: string): string | undefined;
66
}
7-
export function readBuilderProgram(compilerOptions: CompilerOptions, host: ReadBuildProgramHost) {
8-
if (outFileWithoutPersistResolutions(compilerOptions)) return undefined;
7+
/*@internal*/
8+
export function readBuildInfoForProgram(compilerOptions: CompilerOptions, host: ReadBuildProgramHost) {
99
const buildInfoPath = getTsBuildInfoEmitOutputFilePath(compilerOptions);
1010
if (!buildInfoPath) return undefined;
11-
const content = host.readFile(buildInfoPath);
11+
const content = host.readFile?.(buildInfoPath);
1212
if (!content) return undefined;
1313
const buildInfo = getBuildInfo(content);
1414
if (buildInfo.version !== version) return undefined;
15-
if (!buildInfo.program) return undefined;
16-
return createBuildProgramUsingProgramBuildInfo(buildInfo.program, buildInfoPath, host);
15+
return { buildInfo, buildInfoPath };
16+
}
17+
export function readBuilderProgram(compilerOptions: CompilerOptions, host: ReadBuildProgramHost) {
18+
if (outFileWithoutPersistResolutions(compilerOptions)) return undefined;
19+
const result = readBuildInfoForProgram(compilerOptions, host);
20+
if (!result?.buildInfo.program) return undefined;
21+
return createBuildProgramUsingProgramBuildInfo(result.buildInfo.program, result.buildInfoPath, host);
1722
}
1823

1924
export interface CleanPersistedProgramOfTsBuildInfoHost {

src/services/services.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,15 @@ namespace ts {
12721272
return sourceFile;
12731273
}
12741274

1275+
function getOldProgram(options: CompilerOptions, compilerHost: CompilerHost): Program | ProgramFromBuildInfo | undefined {
1276+
if (program) return program;
1277+
if (!options.persistResolutions) return undefined;
1278+
const buildInfoResult = readBuildInfoForProgram(options, compilerHost);
1279+
if (!buildInfoResult?.buildInfo.program?.peristedProgram) return undefined;
1280+
const result = createProgramFromBuildInfo(buildInfoResult.buildInfo.program, buildInfoResult.buildInfoPath, compilerHost);
1281+
return result?.program;
1282+
}
1283+
12751284
function synchronizeHostData(): void {
12761285
Debug.assert(languageServiceMode !== LanguageServiceMode.Syntactic);
12771286
// perform fast check if host supports it
@@ -1361,7 +1370,7 @@ namespace ts {
13611370
rootNames: rootFileNames,
13621371
options: newSettings,
13631372
host: compilerHost,
1364-
oldProgram: program,
1373+
oldProgram: getOldProgram(newSettings, compilerHost),
13651374
projectReferences
13661375
});
13671376

tests/baselines/reference/tsserver/persistResolutions/uses-saved-resolution-for-program.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,11 @@ FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/src/globalFil
3333
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/src/globalMain.ts 500 undefined WatchType: Closed Script info
3434
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/src/types.ts 500 undefined WatchType: Closed Script info
3535
Starting updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json
36-
======== Resolving module './filePresent' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts'. ========
37-
Module resolution kind is not specified, using 'Classic'.
38-
File '/user/username/projects/myproject/src/filePresent.ts' exist - use it as a name resolution result.
39-
======== Module name './filePresent' was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'. ========
40-
======== Resolving module './fileNotFound' from '/user/username/projects/myproject/src/anotherFileReusingResolution.ts'. ========
41-
Module resolution kind is not specified, using 'Classic'.
42-
File '/user/username/projects/myproject/src/fileNotFound.ts' does not exist.
43-
File '/user/username/projects/myproject/src/fileNotFound.tsx' does not exist.
44-
File '/user/username/projects/myproject/src/fileNotFound.d.ts' does not exist.
45-
File '/user/username/projects/myproject/src/fileNotFound.js' does not exist.
46-
File '/user/username/projects/myproject/src/fileNotFound.jsx' does not exist.
47-
======== Module name './fileNotFound' was not resolved. ========
48-
======== Resolving module './filePresent' from '/user/username/projects/myproject/src/main.ts'. ========
49-
Resolution for module './filePresent' was found in cache from location '/user/username/projects/myproject/src'.
50-
======== Module name './filePresent' was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'. ========
51-
======== Resolving module './filePresent' from '/user/username/projects/myproject/src/main.ts'. ========
52-
Resolution for module './filePresent' was found in cache from location '/user/username/projects/myproject/src'.
53-
======== Module name './filePresent' was successfully resolved to '/user/username/projects/myproject/src/filePresent.ts'. ========
54-
======== Resolving module './fileNotFound' from '/user/username/projects/myproject/src/main.ts'. ========
55-
Resolution for module './fileNotFound' was found in cache from location '/user/username/projects/myproject/src'.
56-
======== Module name './fileNotFound' was not resolved. ========
5736
FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 500 undefined WatchType: Closed Script info
5837
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/src/globalFileNotFound.ts 500 undefined Project: /user/username/projects/myproject/tsconfig.json WatchType: Missing file
5938
DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types 1 undefined Project: /user/username/projects/myproject/tsconfig.json WatchType: Type roots
6039
Elapsed:: *ms DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types 1 undefined Project: /user/username/projects/myproject/tsconfig.json WatchType: Type roots
61-
Finishing updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json Version: 1 structureChanged: true structureIsReused:: Not Elapsed:: *ms
40+
Finishing updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json Version: 1 structureChanged: true structureIsReused:: Completely Elapsed:: *ms
6241
Project '/user/username/projects/myproject/tsconfig.json' (Configured)
6342
Files (9)
6443
/a/lib/lib.d.ts

0 commit comments

Comments
 (0)