next.js: SWC plugin runner no longer provides full file path to metadata

Link to the code that reproduces this issue

https://github.com/serg-and/nextjs-minimal-swc-plugin-issue

To Reproduce

npm i
cd swc_plugin
npm run prepublish
cd ..
npm run dev

Logs will show that the provided filename in the metadata no longer includes the file path, only the filename.

Current vs. Expected behavior

Next’s pluggin runner no longer provides the known file path to the metadata for a SWC plugin, only providing the file name.

This is conflict with SWC, saying: swc docs

/// Host side metadata context plugin may need to access.
/// This is a global context - any plugin in single transform will have same
/// values.
pub struct TransformPluginMetadataContext {
    /// The path of the file being processed. This includes all of the path as
    /// much as possible.
    pub filename: Option<String>,

    ...
}

This breaks several packages such as next-superjson-plugin which rely on the path to determine whether some files are page or app router based.

Provide environment information

Operating System:
  Platform: linux
  Arch: arm64
  Version: #1 SMP Wed Jul 17 10:51:09 UTC 2024
  Available memory (MB): 7185
  Available CPU cores: 8
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 15.0.1 // Latest available version is detected (15.0.1).
  eslint-config-next: 15.0.1
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.0.4
Next.js Config:
  output: N/A

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

Pages Router, SWC

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

next dev (local)

Additional context

I believe the issue originates from here https://github.com/vercel/next.js/blob/v15.0.1/turbopack/crates/turbopack-ecmascript-plugins/src/transform/swc_ecma_transform_plugins.rs#L189

Some(ctx.file_name_str.to_string()) could simply be changed to Some(ctx.file_path_str.to_string())

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 15
  • Comments: 15 (8 by maintainers)

Most upvoted comments

I believe the issue originates from here https://github.com/vercel/next.js/blob/v15.0.1/turbopack/crates/turbopack-ecmascript-plugins/src/transform/swc_ecma_transform_plugins.rs#L189 Some(ctx.file_name_str.to_string()) could simply be changed to Some(ctx.file_path_str.to_string())

This PR (https://github.com/vercel/next.js/pull/78637) feels like it might be related, as it seems to implement that exact change for Turbopack. I’m really hoping this helps with the issue!

@awkaiser-tr I’m pretty sure i had to throw away the entire .next cache or maybe even the entire node_modules, because it otherwise wouldn’t update the plugin.

@kdy1 why cant this line https://github.com/vercel/next.js/blob/v15.0.1/turbopack/crates/turbopack-ecmascript-plugins/src/transform/swc_ecma_transform_plugins.rs#L189 be changed to Some(ctx.file_path_str.to_string()) . It seems that this is what is passed to the plugins and should be changed to keep backwards compatibility.

@awkaiser-tr It’s failing with something completely unrelated.

Giving this a New Years bump! I see @kdy1 self-assigned, identified root cause, then recently unassigned? 😞

If the Next.js team has reasons this won’t be patched, that would be great to know so projects such as next-superjson-plugin can be prompted to adapt. In the meantime, folks impacted by this have been unable to upgrade their Next.js projects beyond v14 for 109 days now and it feels bad to be held back by something so small. The issue can be resolved a few different ways[^1][^2] but, if it makes no difference to Next.js itself, here at the root cause would be the best patch location.

[^1]: Suggestion to change next-superjson-plugin behavior [^2]: Suggestion to use next-superjson instead

Path from the project root was what was provided by previous versions of next, so that’s what is need for backwards compatibility.