Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that we are checking if correct file with resolved path is present in the new program when removing the existing packageJson watching #57988

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/resolutionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
else impliedFormatPackageJsons.delete(newFile.resolvedPath);
});
impliedFormatPackageJsons.forEach((existing, path) => {
if (!newProgram?.getSourceFileByPath(path)) {
const newFile = newProgram?.getSourceFileByPath(path);
if (!newFile || newFile.resolvedPath !== path) {
existing.forEach(location => fileWatchesOfAffectingLocations.get(location)!.files--);
impliedFormatPackageJsons.delete(path);
}
Expand Down
4 changes: 4 additions & 0 deletions src/testRunner/unittests/helpers/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ export interface FsContents {
export function libPath(forLib: string) {
return `${ts.getDirectoryPath(libFile.path)}/lib.${forLib}.d.ts`;
}

export function getProjectConfigWithNodeNext(withNodeNext: boolean | undefined) {
return withNodeNext ? { module: "nodenext", target: "es5" } : undefined;
}
10 changes: 4 additions & 6 deletions src/testRunner/unittests/helpers/sampleProjectReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "../helpers";
import {
FsContents,
getProjectConfigWithNodeNext,
} from "./contents";
import {
loadProjectFromFiles,
Expand All @@ -16,13 +17,10 @@ import {
libFile,
} from "./virtualFileSystemWithWatch";

export function getSampleProjectConfigWithNodeNext(withNodeNext: boolean | undefined) {
return withNodeNext ? { module: "nodenext", target: "es5" } : undefined;
}
export function getFsContentsForSampleProjectReferencesLogicConfig(withNodeNext?: boolean) {
return jsonToReadableText({
compilerOptions: {
...getSampleProjectConfigWithNodeNext(withNodeNext),
...getProjectConfigWithNodeNext(withNodeNext),
composite: true,
declaration: true,
sourceMap: true,
Expand All @@ -39,7 +37,7 @@ export function getFsContentsForSampleProjectReferences(withNodeNext?: boolean):
[libFile.path]: libFile.content,
"/user/username/projects/sample1/core/tsconfig.json": jsonToReadableText({
compilerOptions: {
...getSampleProjectConfigWithNodeNext(withNodeNext),
...getProjectConfigWithNodeNext(withNodeNext),
composite: true,
declaration: true,
declarationMap: true,
Expand Down Expand Up @@ -69,7 +67,7 @@ export function getFsContentsForSampleProjectReferences(withNodeNext?: boolean):
],
files: ["index.ts"],
compilerOptions: {
...getSampleProjectConfigWithNodeNext(withNodeNext),
...getProjectConfigWithNodeNext(withNodeNext),
composite: true,
declaration: true,
forceConsistentCasingInFileNames: true,
Expand Down
18 changes: 12 additions & 6 deletions src/testRunner/unittests/helpers/transitiveReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "../helpers";
import {
FsContents,
getProjectConfigWithNodeNext,
libContent,
} from "./contents";
import {
Expand All @@ -19,9 +20,10 @@ export function getFsContentsForTransitiveReferencesRefsAdts() {
`;
}

export function getFsContentsForTransitiveReferencesBConfig() {
export function getFsContentsForTransitiveReferencesBConfig(withNodeNext: boolean) {
return jsonToReadableText({
compilerOptions: {
...getProjectConfigWithNodeNext(withNodeNext),
composite: true,
baseUrl: "./",
paths: {
Expand All @@ -33,14 +35,17 @@ export function getFsContentsForTransitiveReferencesBConfig() {
});
}

export function getFsContentsForTransitiveReferencesAConfig() {
export function getFsContentsForTransitiveReferencesAConfig(withNodeNext: boolean) {
return jsonToReadableText({
compilerOptions: { composite: true },
compilerOptions: {
...getProjectConfigWithNodeNext(withNodeNext),
composite: true,
},
files: ["a.ts"],
});
}

export function getFsContentsForTransitiveReferences(): FsContents {
export function getFsContentsForTransitiveReferences(withNodeNext?: boolean): FsContents {
return {
"/user/username/projects/transitiveReferences/refs/a.d.ts": getFsContentsForTransitiveReferencesRefsAdts(),
"/user/username/projects/transitiveReferences/a.ts": dedent`
Expand All @@ -56,11 +61,12 @@ export function getFsContentsForTransitiveReferences(): FsContents {
b;
X;
`,
"/user/username/projects/transitiveReferences/tsconfig.a.json": getFsContentsForTransitiveReferencesAConfig(),
"/user/username/projects/transitiveReferences/tsconfig.b.json": getFsContentsForTransitiveReferencesBConfig(),
"/user/username/projects/transitiveReferences/tsconfig.a.json": getFsContentsForTransitiveReferencesAConfig(!!withNodeNext),
"/user/username/projects/transitiveReferences/tsconfig.b.json": getFsContentsForTransitiveReferencesBConfig(!!withNodeNext),
"/user/username/projects/transitiveReferences/tsconfig.c.json": jsonToReadableText({
files: ["c.ts"],
compilerOptions: {
...getProjectConfigWithNodeNext(withNodeNext),
baseUrl: "./",
paths: {
"@ref/*": ["./refs/*"],
Expand Down
152 changes: 79 additions & 73 deletions src/testRunner/unittests/tscWatch/projectsWithReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
jsonToReadableText,
} from "../helpers";
import {
getSampleProjectConfigWithNodeNext,
getProjectConfigWithNodeNext,
} from "../helpers/contents";
import {
getSysForSampleProjectReferences,
} from "../helpers/sampleProjectReferences";
import {
Expand All @@ -28,7 +30,7 @@ import {
} from "../helpers/virtualFileSystemWithWatch";

describe("unittests:: tsc-watch:: projects with references: invoking when references are already built", () => {
function verify(withNodeNext: boolean) {
function verifySampleProject(withNodeNext: boolean) {
verifyTscWatch({
scenario: "projectsWithReferences",
subScenario: `on sample project${withNodeNext ? " with nodenext" : ""}`,
Expand Down Expand Up @@ -66,7 +68,7 @@ describe("unittests:: tsc-watch:: projects with references: invoking when refere
"/user/username/projects/sample1/logic/tsconfig.json",
jsonToReadableText({
compilerOptions: {
...getSampleProjectConfigWithNodeNext(withNodeNext),
...getProjectConfigWithNodeNext(withNodeNext),
composite: true,
declaration: true,
declarationDir: "decls",
Expand All @@ -83,86 +85,90 @@ describe("unittests:: tsc-watch:: projects with references: invoking when refere
baselineDependencies: true,
});
}
verify(/*withNodeNext*/ false);
verify(/*withNodeNext*/ true);
verifySampleProject(/*withNodeNext*/ false);
verifySampleProject(/*withNodeNext*/ true);

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

verifyTscWatch({
scenario: "projectsWithReferences",
subScenario: "on transitive references",
sys: () =>
solutionBuildWithBaseline(
createWatchedSystem(
getFsContentsForTransitiveReferences(),
{ currentDirectory: `/user/username/projects/transitiveReferences` },
function verifyTransitiveReferences(withNodeNext: boolean) {
verifyTscWatch({
scenario: "projectsWithReferences",
subScenario: `on transitive references${withNodeNext ? " with nodenext" : ""}`,
sys: () =>
solutionBuildWithBaseline(
createWatchedSystem(
getFsContentsForTransitiveReferences(withNodeNext),
{ currentDirectory: `/user/username/projects/transitiveReferences` },
),
["tsconfig.c.json"],
),
["tsconfig.c.json"],
),
commandLineArgs: ["-w", "-p", "tsconfig.c.json", "--traceResolution", "--explainFiles"],
edits: [
{
caption: "non local edit b ts, and build b",
edit: sys => {
sys.appendFile("b.ts", `export function gfoo() { }`);
const solutionBuilder = createSolutionBuilder(sys, ["tsconfig.b.json"]);
solutionBuilder.build();
commandLineArgs: ["-w", "-p", "tsconfig.c.json", "--traceResolution", "--explainFiles"],
edits: [
{
caption: "non local edit b ts, and build b",
edit: sys => {
sys.appendFile("b.ts", `export function gfoo() { }`);
const solutionBuilder = createSolutionBuilder(sys, ["tsconfig.b.json"]);
solutionBuilder.build();
},
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "edit on config file",
edit: sys => {
sys.ensureFileOrFolder({
path: "/user/username/projects/transitiveReferences/nrefs/a.d.ts",
content: sys.readFile("/user/username/projects/transitiveReferences/refs/a.d.ts")!,
});
changeCompilerOpitonsPaths(sys, "tsconfig.c.json", { "@ref/*": ["./nrefs/*"] });
{
caption: "edit on config file",
edit: sys => {
sys.ensureFileOrFolder({
path: "/user/username/projects/transitiveReferences/nrefs/a.d.ts",
content: sys.readFile("/user/username/projects/transitiveReferences/refs/a.d.ts")!,
});
changeCompilerOpitonsPaths(sys, "tsconfig.c.json", { "@ref/*": ["./nrefs/*"] });
},
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "Revert config file edit",
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.c.json", { "@ref/*": ["./refs/*"] }),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "edit in referenced config file",
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.b.json", { "@ref/*": ["./nrefs/*"] }),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "Revert referenced config file edit",
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.b.json", { "@ref/*": ["./refs/*"] }),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "deleting referenced config file",
edit: sys => sys.deleteFile("tsconfig.b.json"),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "Revert deleting referenced config file",
edit: sys => sys.writeFile("tsconfig.b.json", getFsContentsForTransitiveReferencesBConfig()),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "deleting transitively referenced config file",
edit: sys => sys.deleteFile("tsconfig.a.json"),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "Revert deleting transitively referenced config file",
edit: sys => sys.writeFile("tsconfig.a.json", getFsContentsForTransitiveReferencesAConfig()),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
],
baselineDependencies: true,
});
{
caption: "Revert config file edit",
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.c.json", { "@ref/*": ["./refs/*"] }),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "edit in referenced config file",
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.b.json", { "@ref/*": ["./nrefs/*"] }),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "Revert referenced config file edit",
edit: sys => changeCompilerOpitonsPaths(sys, "tsconfig.b.json", { "@ref/*": ["./refs/*"] }),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "deleting referenced config file",
edit: sys => sys.deleteFile("tsconfig.b.json"),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "Revert deleting referenced config file",
edit: sys => sys.writeFile("tsconfig.b.json", getFsContentsForTransitiveReferencesBConfig(withNodeNext)),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "deleting transitively referenced config file",
edit: sys => sys.deleteFile("tsconfig.a.json"),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "Revert deleting transitively referenced config file",
edit: sys => sys.writeFile("tsconfig.a.json", getFsContentsForTransitiveReferencesAConfig(withNodeNext)),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
],
baselineDependencies: true,
});
}
verifyTransitiveReferences(/*withNodeNext*/ false);
verifyTransitiveReferences(/*withNodeNext*/ true);

verifyTscWatch({
scenario: "projectsWithReferences",
Expand Down
Loading