Unverified Commit 2aa1e457 authored by Dmitrii's avatar Dmitrii Committed by GitHub

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
parent 3ee159a1
...@@ -3309,8 +3309,7 @@ async fn resolve_package_internal_with_imports_field( ...@@ -3309,8 +3309,7 @@ async fn resolve_package_internal_with_imports_field(
let Pattern::Constant(specifier) = pattern else { let Pattern::Constant(specifier) = pattern else {
bail!("PackageInternal requests can only be Constant strings"); bail!("PackageInternal requests can only be Constant strings");
}; };
// https://github.com/nodejs/node/blob/1b177932/lib/internal/modules/esm/resolve.js#L615-L619 if specifier == "#" || specifier.ends_with('/') {
if specifier == "#" || specifier.starts_with("#/") || specifier.ends_with('/') {
ResolvingIssue { ResolvingIssue {
severity: resolve_error_severity(resolve_options).await?, severity: resolve_error_severity(resolve_options).await?,
file_path: file_path.clone(), file_path: file_path.clone(),
......
import greeting from '#/greeting.js'
it('should resolve #/* subpath imports', () => {
expect(greeting).toBe('hello')
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment