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

Valid this in server actions #74181

Closed
maciej-ka opened this issue Dec 20, 2024 · 1 comment · Fixed by #74179
Closed

Valid this in server actions #74181

maciej-ka opened this issue Dec 20, 2024 · 1 comment · Fixed by #74179
Labels
bug Issue was opened via the bug report template. locked

Comments

@maciej-ka
Copy link
Contributor

maciej-ka commented Dec 20, 2024

Link to the code that reproduces this issue

https://github.com/maciej-ka/next-server-action-valid-this

To Reproduce

  1. npm run dev
  2. visit

Current vs. Expected behavior

Current

server-action-this-error

Expected

Screenshot 2024-12-20 at 09 10 34

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000
  Available memory (MB): 65536
  Available CPU cores: 10
Binaries:
  Node: 22.9.0
  npm: 10.8.3
  Yarn: 1.22.17
  pnpm: 9.15.0
Relevant Packages:
  next: 15.0.4 // There is a newer version (15.1.2) available, upgrade recommended!
  eslint-config-next: 15.1.2
  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)

Not sure

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

next dev (local), next build (local), next start (local), Vercel (Deployed), Other (Deployed)

Additional context

Debug

When debugging on Next.js version before forbidding this
(available on branch of reproduction repo)

export const createItem = async () => {
  return new Promise<number>((resolve, reject) => {
    console.dir(this);
    db.run('INSERT INTO items (title) VALUES (?)', ["foo"], function(err) {
      if (err) reject(err)
      console.dir(this);
      resolve(this.lastID)
    })
  });
};

Results are
Screenshot 2024-12-19 at 23 10 37

Motivation

While the first case could be forbidden,
second case, where this is a Statement should be allowed.

Links

Forbidden this introduced in: #73059
Ticket created from discussion: #74124
Solution PR by changing a function visitor: #74179

@maciej-ka maciej-ka added the bug Issue was opened via the bug report template. label Dec 20, 2024
unstubbable added a commit that referenced this issue Dec 20, 2024
…74179)

In #73059 we added build-time checks for the forbidden usage of `this`
and `arguments` in server functions. A nested function expression is
however allowed to use these expressions.

fixes #74181

To add a bit more context: `this` is forbidden in server actions because
the Next.js compiler hoists inline server actions out of their original
location into the module scope, so that they can be imported and invoked
by the action handler. Due to this hoisting, the `this` context gets
lost and is not available to the server action.

To prevent surprising runtime errors for such cases, we emit a build
error to provide feedback to developers as early as possible.

However, nested function declarations or function expressions create a
new `this` context, and in those it's allowed to access `this` and
`arguments`.

For consistency, and to prevent surprises when refactoring server
actions, we apply the same rules for module-level server actions.
Copy link
Contributor

github-actions bot commented Jan 4, 2025

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 added the locked label Jan 4, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 4, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant