Skip to content

Commit de28d02

Browse files
committed
Add test case to verify correct resolution file is picked when current resolution file is not removed but higher precedence file is created
1 parent 67f9533 commit de28d02

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

src/compiler/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ namespace ts {
825825
const moduleName = moduleNames[i];
826826
// If we want to reuse resolutions more aggressively, we can refine this to check for whether the
827827
// text of the corresponding modulenames has changed.
828-
if (file === oldSourceFile) {
828+
if (file === oldSourceFile && !hasInvalidatedResolution(oldSourceFile.path)) {
829829
const oldResolvedModule = oldSourceFile && oldSourceFile.resolvedModules.get(moduleName);
830830
if (oldResolvedModule) {
831831
if (isTraceEnabled(options, host)) {
@@ -846,7 +846,7 @@ namespace ts {
846846
trace(host, Diagnostics.Module_0_was_resolved_as_locally_declared_ambient_module_in_file_1, moduleName, containingFile);
847847
}
848848
}
849-
else if (!hasInvalidatedResolution(oldProgramState.file.path)) {
849+
else {
850850
resolvesToAmbientModuleInNonModifiedFile = moduleNameResolvesToAmbientModuleInNonModifiedFile(moduleName, oldProgramState);
851851
}
852852

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,59 @@ namespace ts.projectSystem {
23062306
});
23072307
assert.isFalse(hasErrorMsg);
23082308
});
2309+
2310+
it("Changed module resolution reflected when specifying files list", () => {
2311+
const file1: FileOrFolder = {
2312+
path: "/a/b/file1.ts",
2313+
content: 'import classc from "file2"'
2314+
};
2315+
const file2a: FileOrFolder = {
2316+
path: "/a/file2.ts",
2317+
content: "export classc { method2a() { return 10; } }"
2318+
};
2319+
const file2: FileOrFolder = {
2320+
path: "/a/b/file2.ts",
2321+
content: "export classc { method2() { return 10; } }"
2322+
};
2323+
const configFile: FileOrFolder = {
2324+
path: "/a/b/tsconfig.json",
2325+
content: JSON.stringify({ files: [file1.path], compilerOptions: { module: "amd" } })
2326+
};
2327+
const files = [file1, file2a, configFile, libFile];
2328+
const host = createServerHost(files);
2329+
const projectService = createProjectService(host);
2330+
projectService.openClientFile(file1.path);
2331+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
2332+
const project = projectService.configuredProjects.get(configFile.path);
2333+
assert.isDefined(project);
2334+
checkProjectActualFiles(project, map(files, file => file.path));
2335+
checkWatchedFiles(host, mapDefined(files, file => file === file1 ? undefined : file.path));
2336+
checkWatchedDirectories(host, [], /*recursive*/ false);
2337+
const watchedRecursiveDirectories = getTypeRootsFromLocation("/a/b");
2338+
watchedRecursiveDirectories.push("/a/b");
2339+
checkWatchedDirectories(host, watchedRecursiveDirectories, /*recursive*/ true);
2340+
2341+
files.push(file2);
2342+
host.reloadFS(files);
2343+
host.runQueuedTimeoutCallbacks();
2344+
watchedRecursiveDirectories.pop();
2345+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
2346+
assert.strictEqual(projectService.configuredProjects.get(configFile.path), project);
2347+
checkProjectActualFiles(project, mapDefined(files, file => file === file2a ? undefined : file.path));
2348+
checkWatchedFiles(host, mapDefined(files, file => file === file1 ? undefined : file.path));
2349+
checkWatchedDirectories(host, [], /*recursive*/ false);
2350+
checkWatchedDirectories(host, watchedRecursiveDirectories, /*recursive*/ true);
2351+
2352+
// On next file open the files file2a should be closed and not watched any more
2353+
projectService.openClientFile(file2.path);
2354+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
2355+
assert.strictEqual(projectService.configuredProjects.get(configFile.path), project);
2356+
checkProjectActualFiles(project, mapDefined(files, file => file === file2a ? undefined : file.path));
2357+
checkWatchedFiles(host, [libFile.path, configFile.path]);
2358+
checkWatchedDirectories(host, [], /*recursive*/ false);
2359+
checkWatchedDirectories(host, watchedRecursiveDirectories, /*recursive*/ true);
2360+
2361+
});
23092362
});
23102363

23112364
describe("Proper errors", () => {

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ namespace ts.TestFSWithWatch {
130130
}
131131

132132
export function checkFileNames(caption: string, actualFileNames: string[], expectedFileNames: string[]) {
133-
assert.equal(actualFileNames.length, expectedFileNames.length, `${caption}: incorrect actual number of files, expected ${JSON.stringify(expectedFileNames)}, got ${actualFileNames}`);
133+
assert.equal(actualFileNames.length, expectedFileNames.length, `${caption}: incorrect actual number of files, expected ${expectedFileNames}, got ${actualFileNames}`);
134134
for (const f of expectedFileNames) {
135-
assert.isTrue(contains(actualFileNames, f), `${caption}: expected to find ${f} in ${JSON.stringify(actualFileNames)}`);
135+
assert.isTrue(contains(actualFileNames, f), `${caption}: expected to find ${f} in ${actualFileNames}`);
136136
}
137137
}
138138

0 commit comments

Comments
 (0)