Skip to content
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

In Next.js 15 with Turbopack mode, the IIFE is not executing as expected, resulting in a strange bug. #74190

Closed
listen936 opened this issue Dec 20, 2024 · 6 comments · Fixed by #74628
Assignees
Labels
bug Issue was opened via the bug report template. locked Turbopack Related to Turbopack with Next.js.

Comments

@listen936
Copy link

Link to the code that reproduces this issue

https://github.com/listen936/nextjs_15_turbopack_bug_demo

To Reproduce

  1. npm install
  2. npm run dev

Current vs. Expected behavior

(function _(namespace, path) {
  // The expected output is "", "wrapper", "wrapper.data", "wrapper.data.key"
  // The actual output is "", "wrapper", "data", "key"
  console.debug("####", path)

  for (var i = 0, keys = Object.keys(namespace); i < keys.length; i++) {
    var key = keys[i];
    // --------------------------------------------------------------------------
    console.debug("####", path ? true : false) // always true? why?
    // --------------------------------------------------------------------------
    parent[key] = _(namespace[key], path ? `${path}.${key}` : key, parent[key] || {});
    // --- If the line below is replaced, it will meet the expectations.
    // parent[key] = _(namespace[key], Boolean(path) ? `${path}.${key}` : key, parent[key] || {});
  }
  return parent;
})({
  wrapper: {
    data: {
      key: {}
    },
  }
}, '');

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:02:27 PDT 2024; root:xnu-11215.41.3~2/RELEASE_X86_64
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 18.20.1
  npm: 8.19.4
  Yarn: 1.22.10
  pnpm: 8.14.1
Relevant Packages:
  next: 15.1.2 // Latest available version is detected (15.1.2).
  eslint-config-next: 15.1.1
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.2
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

@listen936 listen936 added the bug Issue was opened via the bug report template. label Dec 20, 2024
@listen936
Copy link
Author

This code is from my actual development experience; I just removed the unnecessary parts.

@github-actions github-actions bot added the Turbopack Related to Turbopack with Next.js. label Dec 20, 2024
@listen936
Copy link
Author

update demo code

var root = {};
// namespace builder
(function _(namespace, path, parent) {
  // The expected output is "", "wrapper", "wrapper.data", "wrapper.data.key"
  // The actual output is "", "wrapper", "data", "key"
  console.debug("####", path)

  for (var i = 0, keys = Object.keys(namespace); i < keys.length; i++) {
    var key = keys[i];
    // --------------------------------------------------------------------------
    console.debug("####", path, path ? true : false) // always true? why?
    // --------------------------------------------------------------------------
    parent[key] = _(namespace[key], path ? `${path}.${key}` : key, parent[key] || {});
    // --- If the line below is replaced, it will meet the expectations.
    // parent[key] = _(namespace[key], Boolean(path) ? `${path}.${key}` : key, parent[key] || {});
  }
  return parent;
})({
  wrapper: {
    data: {
      key: {}
    },
  }
}, "", root);

@nnnnoel
Copy link
Contributor

nnnnoel commented Jan 6, 2025

I think it is bug of turbopack analyzer.
analyzer doesn't respect side effect for call iife recursively

@nnnnoel
Copy link
Contributor

nnnnoel commented Jan 7, 2025

I tested on my local machine, snapshot for arguments looks like invalid. @sokra can i know who can help turbopack analyzer issues?

Graph/Snapshot: nnnnoel@6304325

@kdy1 kdy1 self-assigned this Jan 7, 2025
@kdy1
Copy link
Member

kdy1 commented Jan 8, 2025

Investigation

input.js:

var root = {};
(function _(namespace, path, parent) {
  for (var i = 0, keys = Object.keys(namespace); i < keys.length; i++) {
    var key = keys[i];
    console.debug("####", path, path ? true : false)
    parent[key] = _(namespace[key], path ? `${path}.${key}` : key, parent[key] || {});
  }
  return parent;
})({
  wrapper: {
    data: {
      key: {}
    },
  }
}, "", root);

becomes

var root = {};
(function _(namespace, path, parent) {
    for(var i = 0, keys = Object.keys(namespace); i < keys.length; i++){
        var key = keys[i];
        console.debug("####", path, ("TURBOPACK compile-time falsy", 0) ? ("TURBOPACK unreachable", undefined) : false);
        parent[key] = _(namespace[key], ("TURBOPACK compile-time falsy", 0) ? ("TURBOPACK unreachable", undefined) : key, parent[key] || {});
    }
    return parent;
})({
    wrapper: {
        data: {
            key: {}
        }
    }
}, "", root);

kdy1 added a commit that referenced this issue Jan 9, 2025
### What?

Add a check for recursive IIFE.

### Why?

Turbopack miscompiles recursive IIFEs because it does not check if IIFE recurses.

### How?

Closes #74190
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 Jan 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Turbopack Related to Turbopack with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants