Thanks to visit codestin.com
Credit goes to github.com

Skip to content

False positive for the "Server Actions must be async functions." diagnostic when using builder pattern with arrow function #77864

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

Open
ChristianIvicevic opened this issue Apr 5, 2025 · 2 comments
Labels
linear: turbopack Confirmed issue that is tracked by the Turbopack team. Server Actions Related to Server Actions. Turbopack Related to Turbopack with Next.js.

Comments

@ChristianIvicevic
Copy link
Contributor

Link to the code that reproduces this issue

https://github.com/ChristianIvicevic/debug/tree/turbopack-async-arrow-function

To Reproduce

  1. pnpm install
  2. pnpm dev
  3. Attempt to open the root page and see the false positive

Current vs. Expected behavior

Current behavior

Image

Extracting that inlined schema into a separate object does work and won't trigger the false positive.

Expected behavior

Being able to inline the schema here which is the cause of the false positive:

export const $serverAction = actionClient
	.schema(
		z.object({
			things: z.enum(Object.entries(things).map(([kind]) => kind) as [string, ...string[]]),
		}),
	)
	.action(async () => {})

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6031
  Available memory (MB): 49152
  Available CPU cores: 16
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 10.7.1
Relevant Packages:
  next: 15.2.4 // Latest available version is detected (15.2.4).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

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

Turbopack, Server Actions

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

next dev (local)

Additional context

No response

@github-actions github-actions bot added Server Actions Related to Server Actions. Turbopack Related to Turbopack with Next.js. labels Apr 5, 2025
@mischnic mischnic added the linear: turbopack Confirmed issue that is tracked by the Turbopack team. label Apr 6, 2025
@lubieowoce
Copy link
Member

lubieowoce commented Apr 29, 2025

With wrappers like these, It's hard to know which part is the actual action, so the transform defensively assumes that all functions present in the export might be. i don't think we can really improve this without unintentionally skipping transforming things that are supposed to be actions, and that'd be a pretty big issue, because we might leak server code to the client.

I'd recommend removing the top level directive and using an inline one instead:

export const $serverAction = actionClient
  .schema(
    z.object({
      things: z.enum(Object.entries(things).map(([kind]) => kind) as [string, ...string[]]),
    }),
  )
  .action(async () => {
    'use server'
  })

that way, the transform doesn't have to guess which functions need transforming.

@ChristianIvicevic
Copy link
Contributor Author

While I do understand why this false positive is occurring and the difficulties of distinguishing function calls I don't like the suggestion at all since it goes against the common convention to use the directive at the top of the file and it stifles DX a bit.

In my particular use case I can easily move the Object.entries().map() expression into a separate file for the time being.

Thanks for following up though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear: turbopack Confirmed issue that is tracked by the Turbopack team. Server Actions Related to Server Actions. Turbopack Related to Turbopack with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants