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

Async function breaks assigning to new objects in for loops #40535

Closed
1 task done
tobysmith568 opened this issue Sep 14, 2022 · 5 comments · Fixed by swc-project/swc#5861 or #40520
Closed
1 task done

Async function breaks assigning to new objects in for loops #40535

tobysmith568 opened this issue Sep 14, 2022 · 5 comments · Fixed by swc-project/swc#5861 or #40520
Labels
kind: bug Confirmed bug that is on the backlog SWC Related to minification/transpilation in Next.js.

Comments

@tobysmith568
Copy link

tobysmith568 commented Sep 14, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Home
Binaries:
  Node: 16.16.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 12.3.1-canary.1
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Not relevant

How are you deploying your application? (if relevant)

Not relevant

Describe the Bug

Assigning to an object in a for loop inside an async function seems to not work properly. It seems to always assign the final value to be iterated over.

image

The works array only has string literals pushed to it, so it works as expect and ends up with ["one", "two", "three"].

The broken array has objects assigned to it, so it is broken and gets [{ input: "three }, { input: "three }, { input: "three }] even though it's assigned to with the exact same variable in the exact same for loop.

Everything works as expected when it's not an async function.

This cannot be reproed on next@12.2.6-canary.8
But can be reproed on next@12.2.6-canary.9 and it remains present in next@canary as of 14/09/22.

See the README in the attached repro for more details.

Expected Behavior

Both the works and broken arrays should get "one", "two", and "three".

Link to reproduction

https://github.com/tobysmith568/nextjs-repro

To Reproduce

Push objects to an array using a for loop in an async function.

image

@tobysmith568 tobysmith568 added the bug Issue was opened via the bug report template. label Sep 14, 2022
@tobysmith568 tobysmith568 changed the title Async function breaks assigning to new objects Async function breaks assigning to new objects in for loops Sep 14, 2022
@discretegames
Copy link

I think I have the same issue from pushing object literals in an async function, though my code isn't in a for loop. I had a line like

myarray.push({ a: await myfunc(), b: 123 });

in an async function and it works in next@12.2.6-canary.8 but fails in next@12.2.6-canary.9 saying TypeError: Cannot convert undefined or null to object. However, these (should-be) equivalent snippets did both work in next@12.2.6-canary.9:

const a = await myfunc();
myarray.push({ a, b: 123 });
const obj = { a: await myfunc(), b: 123 };
myarray.push(obj);

@balazsorban44 balazsorban44 added kind: bug Confirmed bug that is on the backlog SWC Related to minification/transpilation in Next.js. and removed bug Issue was opened via the bug report template. labels Sep 14, 2022
@tobysmith568
Copy link
Author

@magic-akari thanks for the quick fix in swc-project/swc#5861.
However, I wonder if this should this have been auto-closed by the completion of swc-project/swc#5861 ?

I would have assumed this should stay open until there's a new patch release of @swc/helpers and next.js is updated to use it. In the meantime other people might come and open more issues here while the bug is still reproducible in next@canary.

@kdy1
Copy link
Member

kdy1 commented Sep 15, 2022

Oh I missed it.
It will be fixed by #40520

@kdy1 kdy1 reopened this Sep 15, 2022
kodiakhq bot pushed a commit to shuding/next.js that referenced this issue Sep 15, 2022
This updates SWC crates to
swc-project/swc@69bf533

---

 - Closes vercel#40535

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. 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 Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: bug Confirmed bug that is on the backlog SWC Related to minification/transpilation in Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants