Skip to content

Commit ac962ea

Browse files
authored
Fix incorrect use of "path" instead of "resolvedPath" when watching file's package json locations (#57931)
1 parent 3c0a802 commit ac962ea

File tree

4 files changed

+1712
-53
lines changed

4 files changed

+1712
-53
lines changed

src/compiler/resolutionCache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
737737
cleanupLibResolutionWatching(newProgram);
738738
newProgram?.getSourceFiles().forEach(newFile => {
739739
const expected = isExternalOrCommonJsModule(newFile) ? newFile.packageJsonLocations?.length ?? 0 : 0;
740-
const existing = impliedFormatPackageJsons.get(newFile.path) ?? emptyArray;
740+
const existing = impliedFormatPackageJsons.get(newFile.resolvedPath) ?? emptyArray;
741741
for (let i = existing.length; i < expected; i++) {
742742
createFileWatcherOfAffectingLocation(newFile.packageJsonLocations![i], /*forResolution*/ false);
743743
}
@@ -746,8 +746,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
746746
fileWatchesOfAffectingLocations.get(existing[i])!.files--;
747747
}
748748
}
749-
if (expected) impliedFormatPackageJsons.set(newFile.path, newFile.packageJsonLocations!);
750-
else impliedFormatPackageJsons.delete(newFile.path);
749+
if (expected) impliedFormatPackageJsons.set(newFile.resolvedPath, newFile.packageJsonLocations!);
750+
else impliedFormatPackageJsons.delete(newFile.resolvedPath);
751751
});
752752
impliedFormatPackageJsons.forEach((existing, path) => {
753753
if (!newProgram?.getSourceFileByPath(path)) {

src/testRunner/unittests/helpers/sampleProjectReferences.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import {
1616
libFile,
1717
} from "./virtualFileSystemWithWatch";
1818

19-
export function getFsContentsForSampleProjectReferencesLogicConfig() {
19+
export function getSampleProjectConfigWithNodeNext(withNodeNext: boolean | undefined) {
20+
return withNodeNext ? { module: "nodenext", target: "es5" } : undefined;
21+
}
22+
export function getFsContentsForSampleProjectReferencesLogicConfig(withNodeNext?: boolean) {
2023
return jsonToReadableText({
2124
compilerOptions: {
25+
...getSampleProjectConfigWithNodeNext(withNodeNext),
2226
composite: true,
2327
declaration: true,
2428
sourceMap: true,
@@ -30,11 +34,12 @@ export function getFsContentsForSampleProjectReferencesLogicConfig() {
3034
],
3135
});
3236
}
33-
export function getFsContentsForSampleProjectReferences(): FsContents {
37+
export function getFsContentsForSampleProjectReferences(withNodeNext?: boolean): FsContents {
3438
return {
3539
[libFile.path]: libFile.content,
3640
"/user/username/projects/sample1/core/tsconfig.json": jsonToReadableText({
3741
compilerOptions: {
42+
...getSampleProjectConfigWithNodeNext(withNodeNext),
3843
composite: true,
3944
declaration: true,
4045
declarationMap: true,
@@ -48,7 +53,7 @@ export function getFsContentsForSampleProjectReferences(): FsContents {
4853
`,
4954
"/user/username/projects/sample1/core/some_decl.d.ts": `declare const dts: any;`,
5055
"/user/username/projects/sample1/core/anotherModule.ts": `export const World = "hello";`,
51-
"/user/username/projects/sample1/logic/tsconfig.json": getFsContentsForSampleProjectReferencesLogicConfig(),
56+
"/user/username/projects/sample1/logic/tsconfig.json": getFsContentsForSampleProjectReferencesLogicConfig(withNodeNext),
5257
"/user/username/projects/sample1/logic/index.ts": dedent`
5358
import * as c from '../core/index';
5459
export function getSecondsInDay() {
@@ -64,6 +69,7 @@ export function getFsContentsForSampleProjectReferences(): FsContents {
6469
],
6570
files: ["index.ts"],
6671
compilerOptions: {
72+
...getSampleProjectConfigWithNodeNext(withNodeNext),
6773
composite: true,
6874
declaration: true,
6975
forceConsistentCasingInFileNames: true,
@@ -93,9 +99,9 @@ export function getFsForSampleProjectReferences() {
9399
);
94100
}
95101

96-
export function getSysForSampleProjectReferences() {
102+
export function getSysForSampleProjectReferences(withNodeNext?: boolean) {
97103
return createWatchedSystem(
98-
getFsContentsForSampleProjectReferences(),
104+
getFsContentsForSampleProjectReferences(withNodeNext),
99105
{
100106
currentDirectory: "/user/username/projects/sample1",
101107
},

src/testRunner/unittests/tscWatch/projectsWithReferences.ts

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
jsonToReadableText,
66
} from "../helpers";
77
import {
8+
getSampleProjectConfigWithNodeNext,
89
getSysForSampleProjectReferences,
910
} from "../helpers/sampleProjectReferences";
1011
import {
@@ -27,54 +28,63 @@ import {
2728
} from "../helpers/virtualFileSystemWithWatch";
2829

2930
describe("unittests:: tsc-watch:: projects with references: invoking when references are already built", () => {
30-
verifyTscWatch({
31-
scenario: "projectsWithReferences",
32-
subScenario: "on sample project",
33-
sys: () =>
34-
solutionBuildWithBaseline(
35-
getSysForSampleProjectReferences(),
36-
["tests"],
37-
),
38-
commandLineArgs: ["-w", "-p", "tests", "--traceResolution", "--explainFiles"],
39-
edits: [
40-
{
41-
caption: "local edit in logic ts, and build logic",
42-
edit: sys => {
43-
sys.appendFile("/user/username/projects/sample1/logic/index.ts", `function foo() { }`);
44-
const solutionBuilder = createSolutionBuilder(sys, ["logic"]);
45-
solutionBuilder.build();
31+
function verify(withNodeNext: boolean) {
32+
verifyTscWatch({
33+
scenario: "projectsWithReferences",
34+
subScenario: `on sample project${withNodeNext ? " with nodenext" : ""}`,
35+
sys: () =>
36+
solutionBuildWithBaseline(
37+
getSysForSampleProjectReferences(withNodeNext),
38+
["tests"],
39+
),
40+
commandLineArgs: ["-w", "-p", "tests", "--traceResolution", "--explainFiles"],
41+
edits: [
42+
{
43+
caption: "local edit in logic ts, and build logic",
44+
edit: sys => {
45+
sys.appendFile("/user/username/projects/sample1/logic/index.ts", `function foo() { }`);
46+
const solutionBuilder = createSolutionBuilder(sys, ["logic"]);
47+
solutionBuilder.build();
48+
},
49+
// not ideal, but currently because of d.ts but no new file is written
50+
// There will be timeout queued even though file contents are same
51+
timeouts: noop,
4652
},
47-
// not ideal, but currently because of d.ts but no new file is written
48-
// There will be timeout queued even though file contents are same
49-
timeouts: noop,
50-
},
51-
{
52-
caption: "non local edit in logic ts, and build logic",
53-
edit: sys => {
54-
sys.appendFile("/user/username/projects/sample1/logic/index.ts", `export function gfoo() { }`);
55-
const solutionBuilder = createSolutionBuilder(sys, ["logic"]);
56-
solutionBuilder.build();
53+
{
54+
caption: "non local edit in logic ts, and build logic",
55+
edit: sys => {
56+
sys.appendFile("/user/username/projects/sample1/logic/index.ts", `export function gfoo() { }`);
57+
const solutionBuilder = createSolutionBuilder(sys, ["logic"]);
58+
solutionBuilder.build();
59+
},
60+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
5761
},
58-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
59-
},
60-
{
61-
caption: "change in project reference config file builds correctly",
62-
edit: sys => {
63-
sys.writeFile(
64-
"/user/username/projects/sample1/logic/tsconfig.json",
65-
jsonToReadableText({
66-
compilerOptions: { composite: true, declaration: true, declarationDir: "decls" },
67-
references: [{ path: "../core" }],
68-
}),
69-
);
70-
const solutionBuilder = createSolutionBuilder(sys, ["logic"]);
71-
solutionBuilder.build();
62+
{
63+
caption: "change in project reference config file builds correctly",
64+
edit: sys => {
65+
sys.writeFile(
66+
"/user/username/projects/sample1/logic/tsconfig.json",
67+
jsonToReadableText({
68+
compilerOptions: {
69+
...getSampleProjectConfigWithNodeNext(withNodeNext),
70+
composite: true,
71+
declaration: true,
72+
declarationDir: "decls",
73+
},
74+
references: [{ path: "../core" }],
75+
}),
76+
);
77+
const solutionBuilder = createSolutionBuilder(sys, ["logic"]);
78+
solutionBuilder.build();
79+
},
80+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
7281
},
73-
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
74-
},
75-
],
76-
baselineDependencies: true,
77-
});
82+
],
83+
baselineDependencies: true,
84+
});
85+
}
86+
verify(/*withNodeNext*/ false);
87+
verify(/*withNodeNext*/ true);
7888

7989
function changeCompilerOpitonsPaths(sys: TestServerHost, config: string, newPaths: object) {
8090
const configJson = JSON.parse(sys.readFile(config)!);

0 commit comments

Comments
 (0)