Skip to content

Commit 2d96a16

Browse files
authored
Merge pull request microsoft#33681 from microsoft/autoTypeReference
Fix issue when types installed after watch/editor is opened for auto type reference that is for global types not being detected
2 parents 7be7cba + 1a614a2 commit 2d96a16

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/compiler/program.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ namespace ts {
545545
return resolutions;
546546
}
547547

548+
/* @internal */
549+
export const inferredTypesContainingFile = "__inferred type names__.ts";
550+
548551
interface DiagnosticCache<T extends Diagnostic> {
549552
perFile?: Map<T[]>;
550553
allDiagnostics?: Diagnostic[];
@@ -875,7 +878,7 @@ namespace ts {
875878
if (typeReferences.length) {
876879
// This containingFilename needs to match with the one used in managed-side
877880
const containingDirectory = options.configFilePath ? getDirectoryPath(options.configFilePath) : host.getCurrentDirectory();
878-
const containingFilename = combinePaths(containingDirectory, "__inferred type names__.ts");
881+
const containingFilename = combinePaths(containingDirectory, inferredTypesContainingFile);
879882
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename);
880883
for (let i = 0; i < typeReferences.length; i++) {
881884
processTypeReferenceDirective(typeReferences[i], resolutions[i]);

src/compiler/resolutionCache.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,11 @@ namespace ts {
669669
// Mark the file as needing re-evaluation of module resolution instead of using it blindly.
670670
resolution.isInvalidated = true;
671671
(filesWithInvalidatedResolutions || (filesWithInvalidatedResolutions = createMap<true>())).set(containingFilePath, true);
672+
673+
// When its a file with inferred types resolution, invalidate type reference directive resolution
674+
if (containingFilePath.endsWith(inferredTypesContainingFile)) {
675+
resolutionHost.onChangedAutomaticTypeDirectiveNames();
676+
}
672677
}
673678
});
674679
});

src/testRunner/unittests/tscWatch/resolutionCache.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,47 @@ declare module "fs" {
402402
host.checkTimeoutQueueLength(0);
403403
});
404404
});
405+
406+
it("when types in compiler option are global and installed at later point", () => {
407+
const projectRoot = "/user/username/projects/myproject";
408+
const app: File = {
409+
path: `${projectRoot}/lib/app.ts`,
410+
content: `myapp.component("hello");`
411+
};
412+
const tsconfig: File = {
413+
path: `${projectRoot}/tsconfig.json`,
414+
content: JSON.stringify({
415+
compilerOptions: {
416+
module: "none",
417+
types: ["@myapp/ts-types"]
418+
}
419+
})
420+
};
421+
const host = createWatchedSystem([app, tsconfig, libFile]);
422+
const watch = createWatchOfConfigFile(tsconfig.path, host);
423+
checkProgramActualFiles(watch(), [app.path, libFile.path]);
424+
host.checkTimeoutQueueLength(0);
425+
checkOutputErrorsInitial(host, [
426+
createCompilerDiagnostic(Diagnostics.Cannot_find_type_definition_file_for_0, "@myapp/ts-types")
427+
]);
428+
429+
host.ensureFileOrFolder({
430+
path: `${projectRoot}/node_modules/@myapp/ts-types/package.json`,
431+
content: JSON.stringify({
432+
version: "1.65.1",
433+
types: "types/somefile.define.d.ts"
434+
})
435+
});
436+
host.ensureFileOrFolder({
437+
path: `${projectRoot}/node_modules/@myapp/ts-types/types/somefile.define.d.ts`,
438+
content: `
439+
declare namespace myapp {
440+
function component(str: string): number;
441+
}`
442+
});
443+
host.checkTimeoutQueueLengthAndRun(1);
444+
checkOutputErrorsIncremental(host, emptyArray);
445+
});
405446
});
406447

407448
describe("unittests:: tsc-watch:: resolutionCache:: tsc-watch with modules linked to sibling folder", () => {

0 commit comments

Comments
 (0)