Skip to content

Commit 1d76970

Browse files
authored
Match Strada and don't resolve imports of ambient modules declared in the same file (#64491)
1 parent c9b2c8e commit 1d76970

8 files changed

Lines changed: 133 additions & 28 deletions

‎tsc/internal/compiler/fileloader.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,18 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
877877
}
878878

879879
mode := getModeForUsageLocation(file.FileName(), meta, entry, optionsForFile)
880+
// We know moduleName resolves to an ambient module provided that moduleName:
881+
// - is in the list of ambient modules locally declared in the current source file.
882+
if slices.Contains(file.AmbientModuleNames, moduleName) {
883+
if optionsForFile.TraceResolution.IsTrue() {
884+
resolutionsTrace = append(resolutionsTrace, module.DiagAndArgs{
885+
Message: diagnostics.Module_0_was_resolved_as_locally_declared_ambient_module_in_file_1,
886+
Args: []any{moduleName, file.FileName()},
887+
})
888+
}
889+
resolutionsInFile[module.ModeAwareCacheKey{Name: moduleName, Mode: mode}] = &module.ResolvedModule{}
890+
continue
891+
}
880892
var resolvedModule *module.ResolvedModule
881893
var trace []module.DiagAndArgs
882894
var err error
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/a.ts(2,1): error TS2304: Cannot find name 'fooGlobal'.
2+
3+
4+
==== /a.ts (1 errors) ====
5+
/// <reference path="declarations.d.ts" />
6+
fooGlobal;
7+
~~~~~~~~~
8+
!!! error TS2304: Cannot find name 'fooGlobal'.
9+
10+
==== /node_modules/foo/package.json (0 errors) ====
11+
{ "name": "foo", "version": "1.0.0", "types": "index.d.ts" }
12+
13+
==== /node_modules/foo/index.d.ts (0 errors) ====
14+
declare global {
15+
var fooGlobal: string;
16+
}
17+
declare function foo(value: unknown): boolean;
18+
export default foo;
19+
20+
==== /declarations.d.ts (0 errors) ====
21+
declare module "foo" {
22+
import _foo from "foo";
23+
export function isFoo(value: unknown): boolean;
24+
}
25+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/ambientModuleImportingItselfNotResolved.ts] ////
2+
3+
//// [package.json]
4+
{ "name": "foo", "version": "1.0.0", "types": "index.d.ts" }
5+
6+
//// [index.d.ts]
7+
declare global {
8+
var fooGlobal: string;
9+
}
10+
declare function foo(value: unknown): boolean;
11+
export default foo;
12+
13+
//// [declarations.d.ts]
14+
declare module "foo" {
15+
import _foo from "foo";
16+
export function isFoo(value: unknown): boolean;
17+
}
18+
19+
//// [a.ts]
20+
/// <reference path="declarations.d.ts" />
21+
fooGlobal;
22+
23+
24+
//// [a.js]
25+
"use strict";
26+
/// <reference path="declarations.d.ts" />
27+
fooGlobal;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/ambientModuleImportingItselfNotResolved.ts] ////
2+
3+
=== /a.ts ===
4+
5+
/// <reference path="declarations.d.ts" />
6+
fooGlobal;
7+
8+
=== /declarations.d.ts ===
9+
declare module "foo" {
10+
>"foo" : Symbol(_foo, Decl(declarations.d.ts, 0, 0))
11+
12+
import _foo from "foo";
13+
>_foo : Symbol(_foo, Decl(declarations.d.ts, 1, 10))
14+
15+
export function isFoo(value: unknown): boolean;
16+
>isFoo : Symbol(isFoo, Decl(declarations.d.ts, 1, 27))
17+
>value : Symbol(value, Decl(declarations.d.ts, 2, 26))
18+
}
19+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Module 'foo' was resolved as locally declared ambient module in file '/declarations.d.ts'.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/ambientModuleImportingItselfNotResolved.ts] ////
2+
3+
=== /a.ts ===
4+
/// <reference path="declarations.d.ts" />
5+
fooGlobal;
6+
>fooGlobal : any
7+
8+
=== /declarations.d.ts ===
9+
declare module "foo" {
10+
>"foo" : typeof _foo
11+
12+
import _foo from "foo";
13+
>_foo : typeof _foo
14+
15+
export function isFoo(value: unknown): boolean;
16+
>isFoo : (value: unknown) => boolean
17+
>value : unknown
18+
}
19+
Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,9 @@
1-
======== Resolving module 'ph' from '/a/b/node_modules/@types/node/ph.d.ts'. ========
1+
Module 'ph' was resolved as locally declared ambient module in file '/a/b/node_modules/@types/node/ph.d.ts'.
2+
======== Resolving module 'node:ph' from '/a/b/main.ts'. ========
23
Explicitly specified module resolution kind: 'Bundler'.
34
Resolving in CJS mode with conditions 'import', 'types'.
4-
File '/a/b/node_modules/@types/node/package.json' does not exist.
5-
File '/a/b/node_modules/@types/package.json' does not exist.
6-
File '/a/b/node_modules/package.json' does not exist.
75
File '/a/b/package.json' does not exist.
86
File '/a/package.json' does not exist.
97
File '/package.json' does not exist.
10-
Loading module 'ph' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
11-
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
12-
Directory '/a/b/node_modules/@types/node/node_modules' does not exist, skipping all lookups in it.
13-
Directory '/a/b/node_modules/@types/node_modules' does not exist, skipping all lookups in it.
14-
File '/a/b/node_modules/ph.ts' does not exist.
15-
File '/a/b/node_modules/ph.tsx' does not exist.
16-
File '/a/b/node_modules/ph.d.ts' does not exist.
17-
File '/a/b/node_modules/@types/ph.d.ts' does not exist.
18-
Directory '/a/node_modules' does not exist, skipping all lookups in it.
19-
Directory '/node_modules' does not exist, skipping all lookups in it.
20-
Searching all ancestor node_modules directories for fallback extensions: JavaScript, JSON.
21-
Directory '/a/b/node_modules/@types/node/node_modules' does not exist, skipping all lookups in it.
22-
Directory '/a/b/node_modules/@types/node_modules' does not exist, skipping all lookups in it.
23-
File '/a/b/node_modules/ph.js' does not exist.
24-
File '/a/b/node_modules/ph.jsx' does not exist.
25-
Directory '/a/node_modules' does not exist, skipping all lookups in it.
26-
Directory '/node_modules' does not exist, skipping all lookups in it.
27-
======== Module name 'ph' was not resolved. ========
28-
======== Resolving module 'node:ph' from '/a/b/main.ts'. ========
29-
Explicitly specified module resolution kind: 'Bundler'.
30-
Resolving in CJS mode with conditions 'import', 'types'.
31-
File '/a/b/package.json' does not exist according to earlier cached lookups.
32-
File '/a/package.json' does not exist according to earlier cached lookups.
33-
File '/package.json' does not exist according to earlier cached lookups.
348
Skipping module 'node:ph' that looks like an absolute URI, target file types: TypeScript, JavaScript, Declaration, JSON.
359
======== Module name 'node:ph' was not resolved. ========
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @target: esnext
2+
// @module: preserve
3+
// @moduleResolution: bundler
4+
// @noImplicitReferences: true
5+
// @traceResolution: true
6+
7+
// An import inside an ambient module declaration that names the same module
8+
// resolves to that declaration, so the real package must not be pulled in.
9+
10+
// @filename: /node_modules/foo/package.json
11+
{ "name": "foo", "version": "1.0.0", "types": "index.d.ts" }
12+
13+
// @filename: /node_modules/foo/index.d.ts
14+
declare global {
15+
var fooGlobal: string;
16+
}
17+
declare function foo(value: unknown): boolean;
18+
export default foo;
19+
20+
// @filename: /declarations.d.ts
21+
declare module "foo" {
22+
import _foo from "foo";
23+
export function isFoo(value: unknown): boolean;
24+
}
25+
26+
// @filename: /a.ts
27+
/// <reference path="declarations.d.ts" />
28+
fooGlobal;

0 commit comments

Comments
 (0)