Skip to content

Commit f37482c

Browse files
authored
Always watch package jsons for the sourceFile (#59311)
1 parent bf39ecc commit f37482c

File tree

3 files changed

+398
-2
lines changed

3 files changed

+398
-2
lines changed

src/compiler/resolutionCache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import {
3838
isDiskPathRoot,
3939
isEmittedFileOfProgram,
4040
isExternalModuleNameRelative,
41-
isExternalOrCommonJsModule,
4241
isNodeModulesDirectory,
4342
isRootedDiskPath,
4443
isTraceEnabled,
@@ -779,7 +778,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
779778
if (newProgram !== oldProgram) {
780779
cleanupLibResolutionWatching(newProgram);
781780
newProgram?.getSourceFiles().forEach(newFile => {
782-
const expected = isExternalOrCommonJsModule(newFile) ? newFile.packageJsonLocations?.length ?? 0 : 0;
781+
const expected = newFile.packageJsonLocations?.length ?? 0;
783782
const existing = impliedFormatPackageJsons.get(newFile.resolvedPath) ?? emptyArray;
784783
for (let i = existing.length; i < expected; i++) {
785784
createFileWatcherOfAffectingLocation(newFile.packageJsonLocations![i], /*forResolution*/ false);

src/testRunner/unittests/tscWatch/moduleResolution.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import {
2+
ModuleDetectionKind,
3+
ModuleKind,
4+
ModuleResolutionKind,
5+
} from "../../_namespaces/ts.js";
16
import * as Utils from "../../_namespaces/Utils.js";
27
import { jsonToReadableText } from "../helpers.js";
38
import {
@@ -6,6 +11,7 @@ import {
611
getFsContentsForAlternateResultDts,
712
getFsContentsForAlternateResultPackageJson,
813
} from "../helpers/alternateResult.js";
14+
import { compilerOptionsToConfigJson } from "../helpers/contents.js";
915
import { verifyTscWatch } from "../helpers/tscWatch.js";
1016
import {
1117
createWatchedSystem,
@@ -712,4 +718,69 @@ describe("unittests:: tsc-watch:: moduleResolution::", () => {
712718
},
713719
],
714720
});
721+
722+
verifyTscWatch({
723+
scenario: "moduleResolution",
724+
subScenario: "type reference resolutions with impliedMode",
725+
sys: () =>
726+
createWatchedSystem({
727+
"/user/username/projects/myproject/package.json": jsonToReadableText({
728+
name: "myproject",
729+
version: "1.0.0",
730+
type: "module",
731+
}),
732+
"/user/username/projects/myproject/tsconfig.json": jsonToReadableText({
733+
compilerOptions: compilerOptionsToConfigJson({
734+
moduleResolution: ModuleResolutionKind.Node16,
735+
module: ModuleKind.Node16,
736+
moduleDetection: ModuleDetectionKind.Legacy,
737+
types: [],
738+
}),
739+
}),
740+
"/user/username/projects/myproject/index.ts": Utils.dedent`
741+
/// <reference types="pkg"/>
742+
interface LocalInterface extends RequireInterface {}
743+
`,
744+
"/user/username/projects/myproject/node_modules/@types/pkg/package.json": jsonToReadableText({
745+
name: "pkg",
746+
version: "0.0.1",
747+
exports: {
748+
import: "./import.js",
749+
require: "./require.js",
750+
},
751+
}),
752+
"/user/username/projects/myproject/node_modules/@types/pkg/import.d.ts": Utils.dedent`
753+
export {};
754+
declare global {
755+
interface ImportInterface {}
756+
}
757+
`,
758+
"/user/username/projects/myproject/node_modules/@types/pkg/require.d.ts": Utils.dedent`
759+
export {};
760+
declare global {
761+
interface RequireInterface {}
762+
}
763+
`,
764+
[libFile.path]: libFile.content,
765+
["/a/lib/lib.es2022.full.d.ts"]: libFile.content,
766+
}, { currentDirectory: "/user/username/projects/myproject" }),
767+
commandLineArgs: ["-w", "--traceResolution", "--explainFiles"],
768+
edits: [
769+
{
770+
caption: "Modify package json",
771+
edit: sys =>
772+
sys.prependFile(
773+
"/user/username/projects/myproject/package.json",
774+
jsonToReadableText({
775+
name: "myproject",
776+
version: "1.0.0",
777+
}),
778+
),
779+
timeouts: sys => {
780+
sys.runQueuedTimeoutCallbacks();
781+
sys.runQueuedTimeoutCallbacks();
782+
},
783+
},
784+
],
785+
});
715786
});

0 commit comments

Comments
 (0)