Skip to content

Commit 2aa1e45

Browse files
authored
fix(turbopack): allow #/ prefixed subpath import specifiers (#94461)
## What? Fixes Turbopack failing to resolve imports using the common `"#/*": "./src/*"` pattern in `package.json` `imports` field. ## Why? The resolver had an early-rejection check that incorrectly blocked all specifiers starting with `#/`: ```rust if specifier == "#" || specifier.starts_with("#/") || specifier.ends_with(/) { // emit error, return unresolvable } ``` Node.js and webpack (enhanced-resolve) both support the widely-used TypeScript pattern: ```json "imports": { "#/*": "./src/*" } ``` When a user writes `import x from '#/greeting'`, the specifier `#/greeting` starts with `#/`, so Turbopack was erroneously rejecting it before attempting the `imports` field lookup. Only bare `#` and directory-trailing `/` specifiers should be rejected early — `#/something` is perfectly valid and should reach the `imports` field resolver. Since Turbopack is a drop-in webpack replacement in Next.js, it should support the same resolution patterns. The `AliasMap` already handles wildcard pattern matching correctly once the early rejection is bypassed — removing the `starts_with("#/")` guard is sufficient. ## How? - Remove `specifier.starts_with("#/")` from the early rejection condition in `resolve_package_internal_with_imports_field` - Add a test fixture (`subpath-imports-slash`) that validates `#/*` wildcard imports resolve correctly ## Testing New test case in `turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/subpath-imports-slash`: ```json { "imports": { "#/*": "./src/*" } } ``` ```js import greeting from '#/greeting.js' it('should resolve #/* subpath imports', () => { expect(greeting).toBe('hello') }) ``` Closes #94290
1 parent 3ee159a commit 2aa1e45

4 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎turbopack/crates/turbopack-core/src/resolve/mod.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3309,8 +3309,7 @@ async fn resolve_package_internal_with_imports_field(
33093309
let Pattern::Constant(specifier) = pattern else {
33103310
bail!("PackageInternal requests can only be Constant strings");
33113311
};
3312-
// https://github.com/nodejs/node/blob/1b177932/lib/internal/modules/esm/resolve.js#L615-L619
3313-
if specifier == "#" || specifier.starts_with("#/") || specifier.ends_with('/') {
3312+
if specifier == "#" || specifier.ends_with('/') {
33143313
ResolvingIssue {
33153314
severity: resolve_error_severity(resolve_options).await?,
33163315
file_path: file_path.clone(),
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import greeting from '#/greeting.js'
2+
3+
it('should resolve #/* subpath imports', () => {
4+
expect(greeting).toBe('hello')
5+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "subpath-imports-slash",
3+
"imports": {
4+
"#/*": "./src/*"
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'hello'

0 commit comments

Comments
 (0)