Skip to content

Commit 12402f2

Browse files
authored
Ensure that we are checking if correct file with resolved path is present in the new program when removing the existing packageJson watching (#57988)
1 parent 35f4f03 commit 12402f2

File tree

6 files changed

+2239
-86
lines changed

6 files changed

+2239
-86
lines changed

src/compiler/resolutionCache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
750750
else impliedFormatPackageJsons.delete(newFile.resolvedPath);
751751
});
752752
impliedFormatPackageJsons.forEach((existing, path) => {
753-
if (!newProgram?.getSourceFileByPath(path)) {
753+
const newFile = newProgram?.getSourceFileByPath(path);
754+
if (!newFile || newFile.resolvedPath !== path) {
754755
existing.forEach(location => fileWatchesOfAffectingLocations.get(location)!.files--);
755756
impliedFormatPackageJsons.delete(path);
756757
}

src/testRunner/unittests/helpers/contents.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ export interface FsContents {
2929
export function libPath(forLib: string) {
3030
return `${ts.getDirectoryPath(libFile.path)}/lib.${forLib}.d.ts`;
3131
}
32+
33+
export function getProjectConfigWithNodeNext(withNodeNext: boolean | undefined) {
34+
return withNodeNext ? { module: "nodenext", target: "es5" } : undefined;
35+
}

src/testRunner/unittests/helpers/sampleProjectReferences.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "../helpers";
77
import {
88
FsContents,
9+
getProjectConfigWithNodeNext,
910
} from "./contents";
1011
import {
1112
loadProjectFromFiles,
@@ -16,13 +17,10 @@ import {
1617
libFile,
1718
} from "./virtualFileSystemWithWatch";
1819

19-
export function getSampleProjectConfigWithNodeNext(withNodeNext: boolean | undefined) {
20-
return withNodeNext ? { module: "nodenext", target: "es5" } : undefined;
21-
}
2220
export function getFsContentsForSampleProjectReferencesLogicConfig(withNodeNext?: boolean) {
2321
return jsonToReadableText({
2422
compilerOptions: {
25-
...getSampleProjectConfigWithNodeNext(withNodeNext),
23+
...getProjectConfigWithNodeNext(withNodeNext),
2624
composite: true,
2725
declaration: true,
2826
sourceMap: true,
@@ -39,7 +37,7 @@ export function getFsContentsForSampleProjectReferences(withNodeNext?: boolean):
3937
[libFile.path]: libFile.content,
4038
"/user/username/projects/sample1/core/tsconfig.json": jsonToReadableText({
4139
compilerOptions: {
42-
...getSampleProjectConfigWithNodeNext(withNodeNext),
40+
...getProjectConfigWithNodeNext(withNodeNext),
4341
composite: true,
4442
declaration: true,
4543
declarationMap: true,
@@ -69,7 +67,7 @@ export function getFsContentsForSampleProjectReferences(withNodeNext?: boolean):
6967
],
7068
files: ["index.ts"],
7169
compilerOptions: {
72-
...getSampleProjectConfigWithNodeNext(withNodeNext),
70+
...getProjectConfigWithNodeNext(withNodeNext),
7371
composite: true,
7472
declaration: true,
7573
forceConsistentCasingInFileNames: true,

src/testRunner/unittests/helpers/transitiveReferences.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "../helpers";
77
import {
88
FsContents,
9+
getProjectConfigWithNodeNext,
910
libContent,
1011
} from "./contents";
1112
import {
@@ -19,9 +20,10 @@ export function getFsContentsForTransitiveReferencesRefsAdts() {
1920
`;
2021
}
2122

22-
export function getFsContentsForTransitiveReferencesBConfig() {
23+
export function getFsContentsForTransitiveReferencesBConfig(withNodeNext: boolean) {
2324
return jsonToReadableText({
2425
compilerOptions: {
26+
...getProjectConfigWithNodeNext(withNodeNext),
2527
composite: true,
2628
baseUrl: "./",
2729
paths: {
@@ -33,14 +35,17 @@ export function getFsContentsForTransitiveReferencesBConfig() {
3335
});
3436
}
3537

36-
export function getFsContentsForTransitiveReferencesAConfig() {
38+
export function getFsContentsForTransitiveReferencesAConfig(withNodeNext: boolean) {
3739
return jsonToReadableText({
38-
compilerOptions: { composite: true },
40+
compilerOptions: {
41+
...getProjectConfigWithNodeNext(withNodeNext),
42+
composite: true,
43+
},
3944
files: ["a.ts"],
4045
});
4146
}
4247

43-
export function getFsContentsForTransitiveReferences(): FsContents {
48+
export function getFsContentsForTransitiveReferences(withNodeNext?: boolean): FsContents {
4449
return {
4550
"/user/username/projects/transitiveReferences/refs/a.d.ts": getFsContentsForTransitiveReferencesRefsAdts(),
4651
"/user/username/projects/transitiveReferences/a.ts": dedent`
@@ -56,11 +61,12 @@ export function getFsContentsForTransitiveReferences(): FsContents {
5661
b;
5762
X;
5863
`,
59-
"/user/username/projects/transitiveReferences/tsconfig.a.json": getFsContentsForTransitiveReferencesAConfig(),
60-
"/user/username/projects/transitiveReferences/tsconfig.b.json": getFsContentsForTransitiveReferencesBConfig(),
64+
"/user/username/projects/transitiveReferences/tsconfig.a.json": getFsContentsForTransitiveReferencesAConfig(!!withNodeNext),
65+
"/user/username/projects/transitiveReferences/tsconfig.b.json": getFsContentsForTransitiveReferencesBConfig(!!withNodeNext),
6166
"/user/username/projects/transitiveReferences/tsconfig.c.json": jsonToReadableText({
6267
files: ["c.ts"],
6368
compilerOptions: {
69+
...getProjectConfigWithNodeNext(withNodeNext),
6470
baseUrl: "./",
6571
paths: {
6672
"@ref/*": ["./refs/*"],

src/testRunner/unittests/tscWatch/projectsWithReferences.ts

Lines changed: 79 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
jsonToReadableText,
66
} from "../helpers";
77
import {
8-
getSampleProjectConfigWithNodeNext,
8+
getProjectConfigWithNodeNext,
9+
} from "../helpers/contents";
10+
import {
911
getSysForSampleProjectReferences,
1012
} from "../helpers/sampleProjectReferences";
1113
import {
@@ -28,7 +30,7 @@ import {
2830
} from "../helpers/virtualFileSystemWithWatch";
2931

3032
describe("unittests:: tsc-watch:: projects with references: invoking when references are already built", () => {
31-
function verify(withNodeNext: boolean) {
33+
function verifySampleProject(withNodeNext: boolean) {
3234
verifyTscWatch({
3335
scenario: "projectsWithReferences",
3436
subScenario: `on sample project${withNodeNext ? " with nodenext" : ""}`,
@@ -66,7 +68,7 @@ describe("unittests:: tsc-watch:: projects with references: invoking when refere
6668
"/user/username/projects/sample1/logic/tsconfig.json",
6769
jsonToReadableText({
6870
compilerOptions: {
69-
...getSampleProjectConfigWithNodeNext(withNodeNext),
71+
...getProjectConfigWithNodeNext(withNodeNext),
7072
composite: true,
7173
declaration: true,
7274
declarationDir: "decls",
@@ -83,86 +85,90 @@ describe("unittests:: tsc-watch:: projects with references: invoking when refere
8385
baselineDependencies: true,
8486
});
8587
}
86-
verify(/*withNodeNext*/ false);
87-
verify(/*withNodeNext*/ true);
88+
verifySampleProject(/*withNodeNext*/ false);
89+
verifySampleProject(/*withNodeNext*/ true);
8890

8991
function changeCompilerOpitonsPaths(sys: TestServerHost, config: string, newPaths: object) {
9092
const configJson = JSON.parse(sys.readFile(config)!);
9193
configJson.compilerOptions.paths = newPaths;
9294
sys.writeFile(config, jsonToReadableText(configJson));
9395
}
9496

95-
verifyTscWatch({
96-
scenario: "projectsWithReferences",
97-
subScenario: "on transitive references",
98-
sys: () =>
99-
solutionBuildWithBaseline(
100-
createWatchedSystem(
101-
getFsContentsForTransitiveReferences(),
102-
{ currentDirectory: `/user/username/projects/transitiveReferences` },
97+
function verifyTransitiveReferences(withNodeNext: boolean) {
98+
verifyTscWatch({
99+
scenario: "projectsWithReferences",
100+
subScenario: `on transitive references${withNodeNext ? " with nodenext" : ""}`,
101+
sys: () =>
102+
solutionBuildWithBaseline(
103+
createWatchedSystem(
104+
getFsContentsForTransitiveReferences(withNodeNext),
105+
{ currentDirectory: `/user/username/projects/transitiveReferences` },
106+
),
107+
["tsconfig.c.json"],
103108
),
104-
["tsconfig.c.json"],
105-
),
106-
commandLineArgs: ["-w", "-p", "tsconfig.c.json", "--traceResolution", "--explainFiles"],
107-
edits: [
108-
{
109-
caption: "non local edit b ts, and build b",
110-
edit: sys => {
111-
sys.appendFile("b.ts", `export function gfoo() { }`);
112-
const solutionBuilder = createSolutionBuilder(sys, ["tsconfig.b.json"]);
113-
solutionBuilder.build();
109+
commandLineArgs: ["-w", "-p", "tsconfig.c.json", "--traceResolution", "--explainFiles"],
110+
edits: [
111+
{
112+
caption: "non local edit b ts, and build b",
113+
edit: sys => {
114+
sys.appendFile("b.ts", `export function gfoo() { }`);
115+
const solutionBuilder = createSolutionBuilder(sys, ["tsconfig.b.json"]);
116+
solutionBuilder.build();
117+
},
118+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
114119
},
115-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
116-
},
117-
{
118-
caption: "edit on config file",
119-
edit: sys => {
120-
sys.ensureFileOrFolder({
121-
path: "/user/username/projects/transitiveReferences/nrefs/a.d.ts",
122-
content: sys.readFile("/user/username/projects/transitiveReferences/refs/a.d.ts")!,
123-
});
124-
changeCompilerOpitonsPaths(sys, "tsconfig.c.json", { "@ref/*": ["./nrefs/*"] });
120+
{
121+
caption: "edit on config file",
122+
edit: sys => {
123+
sys.ensureFileOrFolder({
124+
path: "/user/username/projects/transitiveReferences/nrefs/a.d.ts",
125+
content: sys.readFile("/user/username/projects/transitiveReferences/refs/a.d.ts")!,
126+
});
127+
changeCompilerOpitonsPaths(sys, "tsconfig.c.json", { "@ref/*": ["./nrefs/*"] });
128+
},
129+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
125130
},
126-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
127-
},
128-
{
129-
caption: "Revert config file edit",
130-
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.c.json", { "@ref/*": ["./refs/*"] }),
131-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
132-
},
133-
{
134-
caption: "edit in referenced config file",
135-
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.b.json", { "@ref/*": ["./nrefs/*"] }),
136-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
137-
},
138-
{
139-
caption: "Revert referenced config file edit",
140-
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.b.json", { "@ref/*": ["./refs/*"] }),
141-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
142-
},
143-
{
144-
caption: "deleting referenced config file",
145-
edit: sys => sys.deleteFile("tsconfig.b.json"),
146-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
147-
},
148-
{
149-
caption: "Revert deleting referenced config file",
150-
edit: sys => sys.writeFile("tsconfig.b.json", getFsContentsForTransitiveReferencesBConfig()),
151-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
152-
},
153-
{
154-
caption: "deleting transitively referenced config file",
155-
edit: sys => sys.deleteFile("tsconfig.a.json"),
156-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
157-
},
158-
{
159-
caption: "Revert deleting transitively referenced config file",
160-
edit: sys => sys.writeFile("tsconfig.a.json", getFsContentsForTransitiveReferencesAConfig()),
161-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
162-
},
163-
],
164-
baselineDependencies: true,
165-
});
131+
{
132+
caption: "Revert config file edit",
133+
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.c.json", { "@ref/*": ["./refs/*"] }),
134+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
135+
},
136+
{
137+
caption: "edit in referenced config file",
138+
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.b.json", { "@ref/*": ["./nrefs/*"] }),
139+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
140+
},
141+
{
142+
caption: "Revert referenced config file edit",
143+
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.b.json", { "@ref/*": ["./refs/*"] }),
144+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
145+
},
146+
{
147+
caption: "deleting referenced config file",
148+
edit: sys => sys.deleteFile("tsconfig.b.json"),
149+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
150+
},
151+
{
152+
caption: "Revert deleting referenced config file",
153+
edit: sys => sys.writeFile("tsconfig.b.json", getFsContentsForTransitiveReferencesBConfig(withNodeNext)),
154+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
155+
},
156+
{
157+
caption: "deleting transitively referenced config file",
158+
edit: sys => sys.deleteFile("tsconfig.a.json"),
159+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
160+
},
161+
{
162+
caption: "Revert deleting transitively referenced config file",
163+
edit: sys => sys.writeFile("tsconfig.a.json", getFsContentsForTransitiveReferencesAConfig(withNodeNext)),
164+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
165+
},
166+
],
167+
baselineDependencies: true,
168+
});
169+
}
170+
verifyTransitiveReferences(/*withNodeNext*/ false);
171+
verifyTransitiveReferences(/*withNodeNext*/ true);
166172

167173
verifyTscWatch({
168174
scenario: "projectsWithReferences",

0 commit comments

Comments
 (0)