diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts
index a2ed0feb795a7..395afdadf096d 100644
--- a/src/services/findAllReferences.ts
+++ b/src/services/findAllReferences.ts
@@ -1687,13 +1687,24 @@ export namespace Core {
scope = next;
}
}
- }
-
- // If symbol.parent, this means we are in an export of an external module. (Otherwise we would have returned `undefined` above.)
- // For an export of a module, we may be in a declaration file, and it may be accessed elsewhere. E.g.:
- // declare module "a" { export type T = number; }
- // declare module "b" { import { T } from "a"; export const x: T; }
- // So we must search the whole source file. (Because we will mark the source file as seen, we we won't return to it when searching for imports.)
+ }
+
+ // If symbol.parent, this means we are in an export of an external module. (Otherwise we would have returned `undefined` above.)
+ // For an export of a module, we may be in a declaration file, and it may be accessed elsewhere. E.g.:
+ // declare module "a" { export type T = number; }
+ // declare module "b" { import { T } from "a"; export const x: T; }
+ // So we must search the whole source file. (Because we will mark the source file as seen, we we won't return to it when searching for imports.)
+ // However, for declare modules, the exports are globally accessible and can be used from any file, so we should do a global search.
+ if (exposedByParent && parent && isExternalModuleSymbol(parent)) {
+ // Check if this is a declare module by examining the declarations
+ const isInDeclareModule = parent.declarations?.some(decl =>
+ isModuleDeclaration(decl) && (decl.flags & NodeFlags.GlobalAugmentation) === 0
+ );
+ if (isInDeclareModule) {
+ // For declare modules, do a global search since their exports can be accessed from anywhere
+ return undefined;
+ }
+ }
return exposedByParent ? scope!.getSourceFile() : scope; // TODO: GH#18217
}
diff --git a/tests/cases/fourslash/findAllRefsWithDeclareModule.ts b/tests/cases/fourslash/findAllRefsWithDeclareModule.ts
new file mode 100644
index 0000000000000..6b7c9ede307f2
--- /dev/null
+++ b/tests/cases/fourslash/findAllRefsWithDeclareModule.ts
@@ -0,0 +1,15 @@
+///
+
+////declare module '@bug/api/index' {
+//// export * from "@bug/api/miscFunctions";
+////}
+////declare module '@bug/api/miscFunctions' {
+//// export function /*1*/myFunction(testParam: string): Promise;
+////}
+////
+////declare namespace bug.v0 {const api: typeof import('@bug/api/index')}
+////
+//////// test.ts
+////bug.v0.api./*2*/myFunction('test')
+
+verify.baselineFindAllReferences('1', '2');
\ No newline at end of file
diff --git a/tests/cases/fourslash/findAllRefsWithDeclareModuleDuplicate.ts b/tests/cases/fourslash/findAllRefsWithDeclareModuleDuplicate.ts
new file mode 100644
index 0000000000000..68edae355437e
--- /dev/null
+++ b/tests/cases/fourslash/findAllRefsWithDeclareModuleDuplicate.ts
@@ -0,0 +1,23 @@
+///
+
+////declare module '@bug/api/index' {
+//// export * from "@bug/api/miscFunctions";
+////}
+////declare module '@bug/api/miscFunctions' {
+//// export function /*1*/myFunction(testParam: string): Promise;
+////}
+////
+////declare namespace bug.v0 {const api: typeof import('@bug/api/index')}
+
+////declare module '@bug/api/index' {
+//// export * from "@bug/api/miscFunctions";
+////}
+////declare module '@bug/api/miscFunctions' {
+//// export function /*2*/myFunction(testParam: string): Promise;
+////}
+////
+////declare namespace bug.v0 {const api: typeof import('@bug/api/index')}
+
+////bug.v0.api./*3*/myFunction('test')
+
+verify.baselineFindAllReferences('1', '2', '3');
\ No newline at end of file