Skip to content

Commit 8684c3b

Browse files
authored
Merge pull request #30740 from Microsoft/moduleAndAmbientResolutionConflict
When old program resolved to module and that file is included, dont consider as ambient resolution.
2 parents 9e24460 + 7ccd86b commit 8684c3b

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ namespace ts {
11341134
function moduleNameResolvesToAmbientModuleInNonModifiedFile(moduleName: string): boolean {
11351135
const resolutionToFile = getResolvedModule(oldSourceFile!, moduleName);
11361136
const resolvedFile = resolutionToFile && oldProgram!.getSourceFile(resolutionToFile.resolvedFileName);
1137-
if (resolutionToFile && resolvedFile && !resolvedFile.externalModuleIndicator) {
1137+
if (resolutionToFile && resolvedFile) {
11381138
// In the old program, we resolved to an ambient module that was in the same
11391139
// place as we expected to find an actual module file.
11401140
// We actually need to return 'false' here even though this seems like a 'true' case

src/server/project.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,9 @@ namespace ts.server {
958958
);
959959
const elapsed = timestamp() - start;
960960
this.writeLog(`Finishing updateGraphWorker: Project: ${this.getProjectName()} Version: ${this.getProjectVersion()} structureChanged: ${hasNewProgram} Elapsed: ${elapsed}ms`);
961+
if (this.program !== oldProgram) {
962+
this.print();
963+
}
961964
return hasNewProgram;
962965
}
963966

src/testRunner/unittests/tsserver/projectErrors.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,90 @@ namespace ts.projectSystem {
479479
session.clearMessages();
480480
}
481481
});
482+
483+
it("Correct errors when resolution resolves to file that has same ambient module and is also module", () => {
484+
const projectRootPath = "/users/username/projects/myproject";
485+
const aFile: File = {
486+
path: `${projectRootPath}/src/a.ts`,
487+
content: `import * as myModule from "@custom/plugin";
488+
function foo() {
489+
// hello
490+
}`
491+
};
492+
const config: File = {
493+
path: `${projectRootPath}/tsconfig.json`,
494+
content: JSON.stringify({ include: ["src"] })
495+
};
496+
const plugin: File = {
497+
path: `${projectRootPath}/node_modules/@custom/plugin/index.d.ts`,
498+
content: `import './proposed';
499+
declare module '@custom/plugin' {
500+
export const version: string;
501+
}`
502+
};
503+
const pluginProposed: File = {
504+
path: `${projectRootPath}/node_modules/@custom/plugin/proposed.d.ts`,
505+
content: `declare module '@custom/plugin' {
506+
export const bar = 10;
507+
}`
508+
};
509+
const files = [libFile, aFile, config, plugin, pluginProposed];
510+
const host = createServerHost(files);
511+
const session = createSession(host, { canUseEvents: true });
512+
const service = session.getProjectService();
513+
openFilesForSession([aFile], session);
514+
515+
checkNumberOfProjects(service, { configuredProjects: 1 });
516+
session.clearMessages();
517+
checkErrors();
518+
519+
session.executeCommandSeq<protocol.ChangeRequest>({
520+
command: protocol.CommandTypes.Change,
521+
arguments: {
522+
file: aFile.path,
523+
line: 3,
524+
offset: 8,
525+
endLine: 3,
526+
endOffset: 8,
527+
insertString: "o"
528+
}
529+
});
530+
checkErrors();
531+
532+
function checkErrors() {
533+
host.checkTimeoutQueueLength(0);
534+
const expectedSequenceId = session.getNextSeq();
535+
session.executeCommandSeq<protocol.GeterrRequest>({
536+
command: server.CommandNames.Geterr,
537+
arguments: {
538+
delay: 0,
539+
files: [aFile.path],
540+
}
541+
});
542+
543+
host.checkTimeoutQueueLengthAndRun(1);
544+
545+
checkErrorMessage(session, "syntaxDiag", { file: aFile.path, diagnostics: [] }, /*isMostRecent*/ true);
546+
session.clearMessages();
547+
548+
host.runQueuedImmediateCallbacks(1);
549+
550+
checkErrorMessage(session, "semanticDiag", { file: aFile.path, diagnostics: [] });
551+
session.clearMessages();
552+
553+
host.runQueuedImmediateCallbacks(1);
554+
555+
checkErrorMessage(session, "suggestionDiag", {
556+
file: aFile.path,
557+
diagnostics: [
558+
createDiagnostic({ line: 1, offset: 1 }, { line: 1, offset: 44 }, Diagnostics._0_is_declared_but_its_value_is_never_read, ["myModule"], "suggestion", /*reportsUnnecessary*/ true),
559+
createDiagnostic({ line: 2, offset: 10 }, { line: 2, offset: 13 }, Diagnostics._0_is_declared_but_its_value_is_never_read, ["foo"], "suggestion", /*reportsUnnecessary*/ true)
560+
],
561+
});
562+
checkCompleteEvent(session, 2, expectedSequenceId);
563+
session.clearMessages();
564+
}
565+
});
482566
});
483567

484568
describe("unittests:: tsserver:: Project Errors for Configure file diagnostics events", () => {

0 commit comments

Comments
 (0)