Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (r *resolutionState) loadModuleFromSelfNameReference() *resolved {
}

func (r *resolutionState) loadModuleFromImports() *resolved {
if r.name == "#" || strings.HasPrefix(r.name, "#/") {
if r.name == "#" || (strings.HasPrefix(r.name, "#/") && (r.features&NodeResolutionFeaturesImportsPatternRoot) == 0) {
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change enables #/ root wildcard imports for NodeNext and Bundler modes but lacks test coverage. According to the project guidelines, at least one minimal test case should be added to verify the new behavior works correctly in each mode (Node16 rejects, NodeNext/Bundler accepts). Consider adding a test in testdata/tests/cases/compiler/ that demonstrates:

  1. #/ pattern being rejected in Node16 mode
  2. #/ pattern being accepted in NodeNext mode
  3. #/ pattern being accepted in Bundler mode

The test should include a package.json with an imports field like "#/*": "./src/*" to match the example from the PR description.

Copilot uses AI. Check for mistakes.
if r.tracer != nil {
r.tracer.write(diagnostics.Invalid_import_specifier_0_has_no_possible_resolutions, r.name)
}
Expand Down
7 changes: 5 additions & 2 deletions internal/module/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ const (
NodeResolutionFeaturesSelfName
NodeResolutionFeaturesExports
NodeResolutionFeaturesExportsPatternTrailers
// allowing `#/` root imports in package.json imports field
// not supported until mass adoption - https://github.com/nodejs/node/pull/60864
NodeResolutionFeaturesImportsPatternRoot

NodeResolutionFeaturesNone NodeResolutionFeatures = 0
NodeResolutionFeaturesAll = NodeResolutionFeaturesImports | NodeResolutionFeaturesSelfName | NodeResolutionFeaturesExports | NodeResolutionFeaturesExportsPatternTrailers
NodeResolutionFeaturesAll = NodeResolutionFeaturesImports | NodeResolutionFeaturesSelfName | NodeResolutionFeaturesExports | NodeResolutionFeaturesExportsPatternTrailers | NodeResolutionFeaturesImportsPatternRoot
NodeResolutionFeaturesNode16Default = NodeResolutionFeaturesImports | NodeResolutionFeaturesSelfName | NodeResolutionFeaturesExports | NodeResolutionFeaturesExportsPatternTrailers
NodeResolutionFeaturesNodeNextDefault = NodeResolutionFeaturesAll
NodeResolutionFeaturesBundlerDefault = NodeResolutionFeaturesImports | NodeResolutionFeaturesSelfName | NodeResolutionFeaturesExports | NodeResolutionFeaturesExportsPatternTrailers
NodeResolutionFeaturesBundlerDefault = NodeResolutionFeaturesImports | NodeResolutionFeaturesSelfName | NodeResolutionFeaturesExports | NodeResolutionFeaturesExportsPatternTrailers | NodeResolutionFeaturesImportsPatternRoot
)

type PackageId struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//// [tests/cases/compiler/nodeModulesPackageImportsRootWildcard.ts] ////

//// [package.json]
{
"name": "package",
"private": true,
"type": "module",
"imports": {
"#/*": "./src/*"
}
}
//// [foo.ts]
export const foo = "foo";
//// [bar.ts]
export const bar = "bar";
//// [baz.ts]
export const baz = "baz";
//// [index.ts]
// esm format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;
//// [index.mts]
// esm format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;
//// [index.cts]
// cjs format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;


//// [foo.js]
export const foo = "foo";
//// [bar.js]
export const bar = "bar";
//// [baz.js]
export const baz = "baz";
//// [index.js]
// esm format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;
//// [index.mjs]
// esm format file
import { foo } from "#/foo.js";
import { bar } from "#/features/bar.js";
import { baz } from "#/nested/deep/baz.js";
foo;
bar;
baz;
//// [index.cjs]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// cjs format file
const foo_js_1 = require("#/foo.js");
const bar_js_1 = require("#/features/bar.js");
const baz_js_1 = require("#/nested/deep/baz.js");
foo_js_1.foo;
bar_js_1.bar;
baz_js_1.baz;


//// [foo.d.ts]
export declare const foo = "foo";
//// [bar.d.ts]
export declare const bar = "bar";
//// [baz.d.ts]
export declare const baz = "baz";
//// [index.d.ts]
export {};
//// [index.d.mts]
export {};
//// [index.d.cts]
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//// [tests/cases/compiler/nodeModulesPackageImportsRootWildcard.ts] ////

=== src/foo.ts ===
export const foo = "foo";
>foo : Symbol(foo, Decl(foo.ts, 0, 12))

=== src/features/bar.ts ===
export const bar = "bar";
>bar : Symbol(bar, Decl(bar.ts, 0, 12))

=== src/nested/deep/baz.ts ===
export const baz = "baz";
>baz : Symbol(baz, Decl(baz.ts, 0, 12))

=== index.ts ===
// esm format file
import { foo } from "#/foo.js";
>foo : Symbol(foo, Decl(index.ts, 1, 8))

import { bar } from "#/features/bar.js";
>bar : Symbol(bar, Decl(index.ts, 2, 8))

import { baz } from "#/nested/deep/baz.js";
>baz : Symbol(baz, Decl(index.ts, 3, 8))

foo;
>foo : Symbol(foo, Decl(index.ts, 1, 8))

bar;
>bar : Symbol(bar, Decl(index.ts, 2, 8))

baz;
>baz : Symbol(baz, Decl(index.ts, 3, 8))

=== index.mts ===
// esm format file
import { foo } from "#/foo.js";
>foo : Symbol(foo, Decl(index.mts, 1, 8))

import { bar } from "#/features/bar.js";
>bar : Symbol(bar, Decl(index.mts, 2, 8))

import { baz } from "#/nested/deep/baz.js";
>baz : Symbol(baz, Decl(index.mts, 3, 8))

foo;
>foo : Symbol(foo, Decl(index.mts, 1, 8))

bar;
>bar : Symbol(bar, Decl(index.mts, 2, 8))

baz;
>baz : Symbol(baz, Decl(index.mts, 3, 8))

=== index.cts ===
// cjs format file
import { foo } from "#/foo.js";
>foo : Symbol(foo, Decl(index.cts, 1, 8))

import { bar } from "#/features/bar.js";
>bar : Symbol(bar, Decl(index.cts, 2, 8))

import { baz } from "#/nested/deep/baz.js";
>baz : Symbol(baz, Decl(index.cts, 3, 8))

foo;
>foo : Symbol(foo, Decl(index.cts, 1, 8))

bar;
>bar : Symbol(bar, Decl(index.cts, 2, 8))

baz;
>baz : Symbol(baz, Decl(index.cts, 3, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
======== Resolving module '#/foo.js' from '/.src/index.ts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
Found 'package.json' at '/.src/package.json'.
Using 'imports' subpath '#/*' with target './src/foo.js'.
File name '/.src/src/foo.js' has a '.js' extension - stripping it.
File '/.src/src/foo.ts' exists - use it as a name resolution result.
======== Module name '#/foo.js' was successfully resolved to '/.src/src/foo.ts'. ========
======== Resolving module '#/features/bar.js' from '/.src/index.ts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
File '/.src/package.json' exists according to earlier cached lookups.
Using 'imports' subpath '#/*' with target './src/features/bar.js'.
File name '/.src/src/features/bar.js' has a '.js' extension - stripping it.
File '/.src/src/features/bar.ts' exists - use it as a name resolution result.
======== Module name '#/features/bar.js' was successfully resolved to '/.src/src/features/bar.ts'. ========
======== Resolving module '#/nested/deep/baz.js' from '/.src/index.ts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
File '/.src/package.json' exists according to earlier cached lookups.
Using 'imports' subpath '#/*' with target './src/nested/deep/baz.js'.
File name '/.src/src/nested/deep/baz.js' has a '.js' extension - stripping it.
File '/.src/src/nested/deep/baz.ts' exists - use it as a name resolution result.
======== Module name '#/nested/deep/baz.js' was successfully resolved to '/.src/src/nested/deep/baz.ts'. ========
======== Resolving module '#/foo.js' from '/.src/index.mts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
File '/.src/package.json' exists according to earlier cached lookups.
Using 'imports' subpath '#/*' with target './src/foo.js'.
File name '/.src/src/foo.js' has a '.js' extension - stripping it.
File '/.src/src/foo.ts' exists - use it as a name resolution result.
======== Module name '#/foo.js' was successfully resolved to '/.src/src/foo.ts'. ========
======== Resolving module '#/features/bar.js' from '/.src/index.mts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
File '/.src/package.json' exists according to earlier cached lookups.
Using 'imports' subpath '#/*' with target './src/features/bar.js'.
File name '/.src/src/features/bar.js' has a '.js' extension - stripping it.
File '/.src/src/features/bar.ts' exists - use it as a name resolution result.
======== Module name '#/features/bar.js' was successfully resolved to '/.src/src/features/bar.ts'. ========
======== Resolving module '#/nested/deep/baz.js' from '/.src/index.mts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
File '/.src/package.json' exists according to earlier cached lookups.
Using 'imports' subpath '#/*' with target './src/nested/deep/baz.js'.
File name '/.src/src/nested/deep/baz.js' has a '.js' extension - stripping it.
File '/.src/src/nested/deep/baz.ts' exists - use it as a name resolution result.
======== Module name '#/nested/deep/baz.js' was successfully resolved to '/.src/src/nested/deep/baz.ts'. ========
======== Resolving module '#/foo.js' from '/.src/index.cts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in CJS mode with conditions 'require', 'types', 'node'.
File '/.src/package.json' exists according to earlier cached lookups.
Using 'imports' subpath '#/*' with target './src/foo.js'.
File name '/.src/src/foo.js' has a '.js' extension - stripping it.
File '/.src/src/foo.ts' exists - use it as a name resolution result.
======== Module name '#/foo.js' was successfully resolved to '/.src/src/foo.ts'. ========
======== Resolving module '#/features/bar.js' from '/.src/index.cts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in CJS mode with conditions 'require', 'types', 'node'.
File '/.src/package.json' exists according to earlier cached lookups.
Using 'imports' subpath '#/*' with target './src/features/bar.js'.
File name '/.src/src/features/bar.js' has a '.js' extension - stripping it.
File '/.src/src/features/bar.ts' exists - use it as a name resolution result.
======== Module name '#/features/bar.js' was successfully resolved to '/.src/src/features/bar.ts'. ========
======== Resolving module '#/nested/deep/baz.js' from '/.src/index.cts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in CJS mode with conditions 'require', 'types', 'node'.
File '/.src/package.json' exists according to earlier cached lookups.
Using 'imports' subpath '#/*' with target './src/nested/deep/baz.js'.
File name '/.src/src/nested/deep/baz.js' has a '.js' extension - stripping it.
File '/.src/src/nested/deep/baz.ts' exists - use it as a name resolution result.
======== Module name '#/nested/deep/baz.js' was successfully resolved to '/.src/src/nested/deep/baz.ts'. ========
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//// [tests/cases/compiler/nodeModulesPackageImportsRootWildcard.ts] ////

=== src/foo.ts ===
export const foo = "foo";
>foo : "foo"
>"foo" : "foo"

=== src/features/bar.ts ===
export const bar = "bar";
>bar : "bar"
>"bar" : "bar"

=== src/nested/deep/baz.ts ===
export const baz = "baz";
>baz : "baz"
>"baz" : "baz"

=== index.ts ===
// esm format file
import { foo } from "#/foo.js";
>foo : "foo"

import { bar } from "#/features/bar.js";
>bar : "bar"

import { baz } from "#/nested/deep/baz.js";
>baz : "baz"

foo;
>foo : "foo"

bar;
>bar : "bar"

baz;
>baz : "baz"

=== index.mts ===
// esm format file
import { foo } from "#/foo.js";
>foo : "foo"

import { bar } from "#/features/bar.js";
>bar : "bar"

import { baz } from "#/nested/deep/baz.js";
>baz : "baz"

foo;
>foo : "foo"

bar;
>bar : "bar"

baz;
>baz : "baz"

=== index.cts ===
// cjs format file
import { foo } from "#/foo.js";
>foo : "foo"

import { bar } from "#/features/bar.js";
>bar : "bar"

import { baz } from "#/nested/deep/baz.js";
>baz : "baz"

foo;
>foo : "foo"

bar;
>bar : "bar"

baz;
>baz : "baz"

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
index.ts(2,21): error TS2307: Cannot find module '#/foo.js' or its corresponding type declarations.


==== package.json (0 errors) ====
{
"name": "package",
"private": true,
"type": "module",
"imports": {
"#/*": "./src/*"
}
}
==== src/foo.ts (0 errors) ====
export const foo = "foo";
==== index.ts (1 errors) ====
// esm format file
import { foo } from "#/foo.js";
~~~~~~~~~~
!!! error TS2307: Cannot find module '#/foo.js' or its corresponding type declarations.
foo;

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/nodeModulesPackageImportsRootWildcardNode16.ts] ////

//// [package.json]
{
"name": "package",
"private": true,
"type": "module",
"imports": {
"#/*": "./src/*"
}
}
//// [foo.ts]
export const foo = "foo";
//// [index.ts]
// esm format file
import { foo } from "#/foo.js";
foo;


//// [foo.js]
export const foo = "foo";
//// [index.js]
// esm format file
import { foo } from "#/foo.js";
foo;


//// [foo.d.ts]
export declare const foo = "foo";
//// [index.d.ts]
export {};
Loading