Skip to content

Commit ff40714

Browse files
committed
Fix issue with impliedNodeFormat change adding extra ref to the file
1 parent 533145d commit ff40714

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

src/compiler/program.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,8 +2369,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
23692369
for (const oldSourceFile of oldSourceFiles) {
23702370
const sourceFileOptions = getCreateSourceFileOptions(oldSourceFile.fileName, moduleResolutionCache, host, options);
23712371
let newSourceFile = host.getSourceFileByPath
2372-
? host.getSourceFileByPath(oldSourceFile.fileName, oldSourceFile.resolvedPath, sourceFileOptions, /*onError*/ undefined, shouldCreateNewSourceFile || sourceFileOptions.impliedNodeFormat !== oldSourceFile.impliedNodeFormat)
2373-
: host.getSourceFile(oldSourceFile.fileName, sourceFileOptions, /*onError*/ undefined, shouldCreateNewSourceFile || sourceFileOptions.impliedNodeFormat !== oldSourceFile.impliedNodeFormat); // TODO: GH#18217
2372+
? host.getSourceFileByPath(oldSourceFile.fileName, oldSourceFile.resolvedPath, sourceFileOptions, /*onError*/ undefined, shouldCreateNewSourceFile)
2373+
: host.getSourceFile(oldSourceFile.fileName, sourceFileOptions, /*onError*/ undefined, shouldCreateNewSourceFile); // TODO: GH#18217
23742374

23752375
if (!newSourceFile) {
23762376
return StructureIsReused.Not;
@@ -3615,7 +3615,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
36153615
fileName,
36163616
sourceFileOptions,
36173617
hostErrorMessage => addFilePreprocessingFileExplainingDiagnostic(/*file*/ undefined, reason, Diagnostics.Cannot_read_file_0_Colon_1, [fileName, hostErrorMessage]),
3618-
shouldCreateNewSourceFile || (oldProgram?.getSourceFileByPath(toPath(fileName))?.impliedNodeFormat !== sourceFileOptions.impliedNodeFormat)
3618+
shouldCreateNewSourceFile,
36193619
);
36203620

36213621
if (packageId) {

src/compiler/watchPublic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,8 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
724724
}
725725

726726
// Create new source file if requested or the versions dont match
727-
if (hostSourceFile === undefined || shouldCreateNewSourceFile || isFilePresenceUnknownOnHost(hostSourceFile)) {
727+
const impliedNodeFormat = typeof languageVersionOrOptions === "object" ? languageVersionOrOptions.impliedNodeFormat : undefined;
728+
if (hostSourceFile === undefined || shouldCreateNewSourceFile || isFilePresenceUnknownOnHost(hostSourceFile) || hostSourceFile.sourceFile.impliedNodeFormat !== impliedNodeFormat) {
728729
const sourceFile = getNewSourceFile(fileName, languageVersionOrOptions, onError);
729730
if (hostSourceFile) {
730731
if (sourceFile) {

tests/baselines/reference/tsserver/moduleResolution/package-json-file-is-edited-when-package-json-with-type-module-exists.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,13 @@ Info seq [hh:mm:ss:mss] File '/user/username/projects/package.json' does not ex
581581
Info seq [hh:mm:ss:mss] File '/user/username/package.json' does not exist according to earlier cached lookups.
582582
Info seq [hh:mm:ss:mss] File '/user/package.json' does not exist according to earlier cached lookups.
583583
Info seq [hh:mm:ss:mss] File '/package.json' does not exist according to earlier cached lookups.
584+
Info seq [hh:mm:ss:mss] ======== Resolving module './fileB.mjs' from '/user/username/projects/myproject/src/fileA.ts'. ========
585+
Info seq [hh:mm:ss:mss] Module resolution kind is not specified, using 'Node16'.
586+
Info seq [hh:mm:ss:mss] Resolving in CJS mode with conditions 'require', 'types', 'node'.
587+
Info seq [hh:mm:ss:mss] Loading module as file / folder, candidate module location '/user/username/projects/myproject/src/fileB.mjs', target file types: TypeScript, JavaScript, Declaration.
588+
Info seq [hh:mm:ss:mss] File name '/user/username/projects/myproject/src/fileB.mjs' has a '.mjs' extension - stripping it.
589+
Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/src/fileB.mts' exists - use it as a name resolution result.
590+
Info seq [hh:mm:ss:mss] ======== Module name './fileB.mjs' was successfully resolved to '/user/username/projects/myproject/src/fileB.mts'. ========
584591
Info seq [hh:mm:ss:mss] File '/a/lib/package.json' does not exist according to earlier cached lookups.
585592
Info seq [hh:mm:ss:mss] File '/a/package.json' does not exist according to earlier cached lookups.
586593
Info seq [hh:mm:ss:mss] File '/package.json' does not exist according to earlier cached lookups.
@@ -770,13 +777,7 @@ Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/src/package.jso
770777
Info seq [hh:mm:ss:mss] Found 'package.json' at '/user/username/projects/myproject/package.json'.
771778
Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/src/package.json' does not exist according to earlier cached lookups.
772779
Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/package.json' exists according to earlier cached lookups.
773-
Info seq [hh:mm:ss:mss] ======== Resolving module './fileB.mjs' from '/user/username/projects/myproject/src/fileA.ts'. ========
774-
Info seq [hh:mm:ss:mss] Module resolution kind is not specified, using 'Node16'.
775-
Info seq [hh:mm:ss:mss] Resolving in CJS mode with conditions 'require', 'types', 'node'.
776-
Info seq [hh:mm:ss:mss] Loading module as file / folder, candidate module location '/user/username/projects/myproject/src/fileB.mjs', target file types: TypeScript, JavaScript, Declaration.
777-
Info seq [hh:mm:ss:mss] File name '/user/username/projects/myproject/src/fileB.mjs' has a '.mjs' extension - stripping it.
778-
Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/src/fileB.mts' exists - use it as a name resolution result.
779-
Info seq [hh:mm:ss:mss] ======== Module name './fileB.mjs' was successfully resolved to '/user/username/projects/myproject/src/fileB.mts'. ========
780+
Info seq [hh:mm:ss:mss] Reusing resolution of module './fileB.mjs' from '/user/username/projects/myproject/src/fileA.ts' of old program, it was successfully resolved to '/user/username/projects/myproject/src/fileB.mts'.
780781
Info seq [hh:mm:ss:mss] File '/a/lib/package.json' does not exist according to earlier cached lookups.
781782
Info seq [hh:mm:ss:mss] File '/a/package.json' does not exist according to earlier cached lookups.
782783
Info seq [hh:mm:ss:mss] File '/package.json' does not exist according to earlier cached lookups.

tests/baselines/reference/tsserver/moduleResolution/package-json-file-is-edited.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,13 @@ Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/src/package.jso
771771
Info seq [hh:mm:ss:mss] Found 'package.json' at '/user/username/projects/myproject/package.json'.
772772
Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/src/package.json' does not exist according to earlier cached lookups.
773773
Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/package.json' exists according to earlier cached lookups.
774+
Info seq [hh:mm:ss:mss] ======== Resolving module './fileB.mjs' from '/user/username/projects/myproject/src/fileA.ts'. ========
775+
Info seq [hh:mm:ss:mss] Module resolution kind is not specified, using 'Node16'.
776+
Info seq [hh:mm:ss:mss] Resolving in ESM mode with conditions 'import', 'types', 'node'.
777+
Info seq [hh:mm:ss:mss] Loading module as file / folder, candidate module location '/user/username/projects/myproject/src/fileB.mjs', target file types: TypeScript, JavaScript, Declaration.
778+
Info seq [hh:mm:ss:mss] File name '/user/username/projects/myproject/src/fileB.mjs' has a '.mjs' extension - stripping it.
779+
Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/src/fileB.mts' exists - use it as a name resolution result.
780+
Info seq [hh:mm:ss:mss] ======== Module name './fileB.mjs' was successfully resolved to '/user/username/projects/myproject/src/fileB.mts'. ========
774781
Info seq [hh:mm:ss:mss] File '/a/lib/package.json' does not exist according to earlier cached lookups.
775782
Info seq [hh:mm:ss:mss] File '/a/package.json' does not exist according to earlier cached lookups.
776783
Info seq [hh:mm:ss:mss] File '/package.json' does not exist according to earlier cached lookups.
@@ -954,6 +961,13 @@ Info seq [hh:mm:ss:mss] File '/user/username/projects/package.json' does not ex
954961
Info seq [hh:mm:ss:mss] File '/user/username/package.json' does not exist according to earlier cached lookups.
955962
Info seq [hh:mm:ss:mss] File '/user/package.json' does not exist according to earlier cached lookups.
956963
Info seq [hh:mm:ss:mss] File '/package.json' does not exist according to earlier cached lookups.
964+
Info seq [hh:mm:ss:mss] ======== Resolving module './fileB.mjs' from '/user/username/projects/myproject/src/fileA.ts'. ========
965+
Info seq [hh:mm:ss:mss] Module resolution kind is not specified, using 'Node16'.
966+
Info seq [hh:mm:ss:mss] Resolving in CJS mode with conditions 'require', 'types', 'node'.
967+
Info seq [hh:mm:ss:mss] Loading module as file / folder, candidate module location '/user/username/projects/myproject/src/fileB.mjs', target file types: TypeScript, JavaScript, Declaration.
968+
Info seq [hh:mm:ss:mss] File name '/user/username/projects/myproject/src/fileB.mjs' has a '.mjs' extension - stripping it.
969+
Info seq [hh:mm:ss:mss] File '/user/username/projects/myproject/src/fileB.mts' exists - use it as a name resolution result.
970+
Info seq [hh:mm:ss:mss] ======== Module name './fileB.mjs' was successfully resolved to '/user/username/projects/myproject/src/fileB.mts'. ========
957971
Info seq [hh:mm:ss:mss] File '/a/lib/package.json' does not exist according to earlier cached lookups.
958972
Info seq [hh:mm:ss:mss] File '/a/package.json' does not exist according to earlier cached lookups.
959973
Info seq [hh:mm:ss:mss] File '/package.json' does not exist according to earlier cached lookups.

0 commit comments

Comments
 (0)