Skip to content

[turbopack] dynamic imports fallback to single existing file when target file does not exist #74664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
qnten opened this issue Jan 8, 2025 · 1 comment · Fixed by #77986
Closed
Labels
linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked Turbopack Related to Turbopack with Next.js.

Comments

@qnten
Copy link

qnten commented Jan 8, 2025

Link to the code that reproduces this issue

https://github.com/qnten/reproduction-dynamic-import

To Reproduce

  1. Start the application in dev with turbo enabled npm run dev
  2. The page / should display the following:
1. first file exists so "first name" should be displayed: first file
2. second file does not exist so "no name found" should be displayed: first file
3. third file does not exist so "no name found" should be displayed: first file

Current vs. Expected behavior

Current Behavior

When using dynamic import to load files from a directory that contains only one file, the import incorrectly falls back to that existing file even if the requested file does not match the import path. Specifically, in the provided example:
• First File (first.ts): Exists and correctly returns "first name".
• Second File (second.ts): Does not exist, but getExportFromFile("second") still returns "first name" instead of "no name found".
• Third File (third.ts): Does not exist, but getExportFromFile("third") also returns "first name" instead of "no name found".

This behavior occurs only when the directory contains a single file. If a second file is added (second.ts) everything works as expected.

Expected Behavior

The dynamic import should strictly attempt to load the specified file based on the provided path. If the target file does not exist within the directory, the import should throw.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Fri Dec  6 19:01:59 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 22.4.1
  npm: 10.8.1
  Yarn: 1.22.22
  pnpm: 9.5.0
Relevant Packages:
  next: 15.2.0-canary.1 // Latest available version is detected (15.2.0-canary.1).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Turbopack

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

@qnten qnten added the bug Issue was opened via the bug report template. label Jan 8, 2025
@github-actions github-actions bot added the Turbopack Related to Turbopack with Next.js. label Jan 8, 2025
@samcx samcx removed the bug Issue was opened via the bug report template. label Jan 23, 2025
@mischnic mischnic added the linear: turbopack Confirmed issue that is tracked by the Turbopack team. label Apr 9, 2025
mischnic added a commit that referenced this issue Apr 10, 2025
Previously, if `require("./dynamic-files/"+name)` had only a single match, it would always return that one, regardless of whether `name` was equal to the matches' filename or not.

Closes #74664
Closes PACK-4309
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 24, 2025
feedthejim pushed a commit that referenced this issue May 14, 2025
Previously, if `require("./dynamic-files/"+name)` had only a single match, it would always return that one, regardless of whether `name` was equal to the matches' filename or not.

Closes #74664
Closes PACK-4309
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked Turbopack Related to Turbopack with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants