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

Server actions regression in 14.2.8: “Failed to find Server Action” #69756

Closed
controversial opened this issue Sep 5, 2024 · 19 comments · Fixed by #69788
Closed

Server actions regression in 14.2.8: “Failed to find Server Action” #69756

controversial opened this issue Sep 5, 2024 · 19 comments · Fixed by #69788
Assignees
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@controversial
Copy link
Contributor

controversial commented Sep 5, 2024

When multiple components import from the same actions.ts file, server actions are missing/broken in the final build.

As seen in the attached repository, the structure I found which reliably reproduces this issue is as follows:

  • actions.ts (marked with 'use server') exports two server actions.
  • a page.tsx imports action1
  • this page also renders a child component, which imports action2 from the same file.

If page.tsx calls action1, the action will return undefined (instead of its real return value) and a “failed to find Server Action” error will appear in the server console:

Screenshot 2024-09-05 at 6 35 14 PM

Link to the code that reproduces this issue

https://github.com/controversial/next-server-actions-regression-repro

To Reproduce

Clone the included repository, then

  1. bun install
  2. bunx next build && bunx next start
  3. visit localhost:3000/my-page

Current vs. Expected behavior

Current behavior (next@canary and next@14.2.8)

  • Server console shows:
    Error: Failed to find Server Action "84eb9c473d392e3d69c580b53ac4d200bdc426c8". This request might be from an older or newer deployment. Original error: Cannot read properties of undefined (reading 'workers')
        ...
    
  • browser console shows: action1 returned: undefined

i.e. the server action is missing / broken

Expected behavior (next <= 14.2.7)

  • Server console shows no error when calling action1
  • Browser console shows action1 returned: {hello: 'world'}

i.e., all of the server actions that are imported from actions.ts get hooked up properly.

you can see this behavior by downgrading next to 14.2.7 within the included repro repository.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.1.0: Fri Aug 16 19:18:30 PDT 2024; root:xnu-11215.40.42~4/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 22.7.0
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: N/A
Relevant Packages:
  next: 14.2.8 // Latest available version is detected (14.2.8).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.4
Next.js Config:
  output: N/A

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

Server actions, runtime, vercel deployments, app router

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

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

Additional context

  • This error also exists with the current next@canary (141)
    • I made the repro using 14.2.8 to show that the current “stable” version includes this regression, but you can change the package versions to observe that canary is broken but 14.2.7 is not.
  • This regression in server actions is affecting our production app on Vercel.
  • There are no issues in the dev server, only after a production build.
  • This regression was introduced in Apply optimization for unused actions #69178
    • Based on debugging the webpack plugin, it seems like webpack’s own moduleGraph.getOutgoingConnections() may be returning incomplete/incorrect information inside connection.dependency.ids
@controversial controversial added the bug Issue was opened via the bug report template. label Sep 5, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Sep 5, 2024
@controversial
Copy link
Contributor Author

Anecdotally affecting a few other people at the bottom of #58431

@mikestonecodes
Copy link

I just ran into this issue as well.

@davidaragundy
Copy link

Same issue here, was working well with 14.2.7.

I have an actions/index.ts file where I export all my actions.

@alexOS-dev
Copy link

I encountered the same issue, which was working fine with 14.2.7. After some investigation, I found a solution by changing the way I export server actions in my index.ts file.

Before:

index.ts

export * from './user/create-user';
export * from './auth/login';

After:

index.ts

export { createUser } from './user/create-user';
export { authenticate, login } from './auth/login';

The wildcard export (export *) seemed to cause problems with server actions, but explicitly naming the exports resolved the issue. This way, I was able to maintain the imports like this:

import { authenticate, login } from '@actions';

without having to reference the full path to the server action.

I hope this helps anyone facing a similar problem!

Note. My directory structure looks something like this:

.
└── src/
    ├── actions/
    │   ├── user/
    │   │   └── create-user.ts
    │   ├── auth/
    │   │   └── login.ts
    │   └── index.ts
    └── app/
        └── auth/
            └── login/page.tsx

@controversial
Copy link
Contributor Author

Unfortunately, the reproduction I linked doesn't involve wildcard exports, so I don't think every case can be worked around by just avoiding wildcard exports.

@huozhi huozhi self-assigned this Sep 6, 2024
@adelrodriguez
Copy link

I also ran into this issue, and I could reproduce it consistently by exporting server actions from a barrel file in the same way that @alexOS-dev mentioned. I'm thinking this might be related to this fix? #69178

@huozhi
Copy link
Member

huozhi commented Sep 9, 2024

Folks, we had a fix in patch release 14.2.9, please upgrade and let us know if it fixed for you 🙏

@WoetDev
Copy link

WoetDev commented Sep 9, 2024

@huozhi I've upgraded to version 14.2.9, but still receiving this error which seems related to this issue, the server action I'm using is structured as in https://sdk.vercel.ai/examples/next-app/assistants/stream-assistant-responses.

When downgrading to v14.2.7, I'm not receiving this error.

I'm not sure if this is related to the ai package, or a change in next, when I upgrade either one of those, the AI functionality in my production app breaks.

I've logged a ticket on the ai package: vercel/ai#2948

Reproduction can be found here: https://github.com/WoetDev/bug-next-ai-sdk

image

@janvennemann
Copy link

@huozhi @WoetDev Here is a CodeSandbox reproduction case based on the Save and Restore AI State example.

Just enter a message and hit "Send Message" and the error is shown. Downgrading to Nextjs v14.2.7 fixes the issue.

@huozhi
Copy link
Member

huozhi commented Sep 9, 2024

@WoetDev thanks fore reporting, we'll look into that specific case, as usually the function in "use server" module should be async actions, but AI is a component, not a valid server action. The analysis only found AI but not submitMessage.

A workaround would be having a new file for AI component itself, then import AI into your layout.

import { createAI, } from "ai/rsc";
import { submitMessage } from "./actions";
export const AI = createAI({
  actions: {
    submitMessage,
  },
});

This way make sure submitMessage is used.

@huozhi huozhi reopened this Sep 9, 2024
@ezeparziale
Copy link

I encountered the same issue, which was working fine with 14.2.7. After some investigation, I found a solution by changing the way I export server actions in my index.ts file.

Before:

index.ts

export * from './user/create-user';
export * from './auth/login';

After:

index.ts

export { createUser } from './user/create-user';
export { authenticate, login } from './auth/login';

The wildcard export (export *) seemed to cause problems with server actions, but explicitly naming the exports resolved the issue. This way, I was able to maintain the imports like this:

import { authenticate, login } from '@actions';

without having to reference the full path to the server action.

I hope this helps anyone facing a similar problem!

Note. My directory structure looks something like this:

.
└── src/
    ├── actions/
    │   ├── user/
    │   │   └── create-user.ts
    │   ├── auth/
    │   │   └── login.ts
    │   └── index.ts
    └── app/
        └── auth/
            └── login/page.tsx

I was having issues, but I tried this solution, and now all the server actions work with version 14.2.9. A bit strange behavior, as with version 14.2.7, everything was working fine with the export * from './file'.

Does anyone know the reason why? Or is it just an issue?

@WebDevTrop
Copy link

Just ran into this issue too. Had a server action exported (also with the 'use server' at the top of the function) with 'use server' at the top of the file. Reverting to 14.2.7 fixed this issue.

The server action was imported into a server component and then passed down to a client component via props.

@BlakeHastings
Copy link

I'm facing the same issue. Each of my server actions are their own file and I centralize them with a _module.ts which I import:
image
image

That way I can import them however needed from one file:
image

I'm getting this in the console:
Error: Failed to find Server Action "f64d873775c513ae3c50af1469c9c00d76b735cf". This request might be from an older or newer deployment. Original error: Cannot read properties of undefined (reading 'workers')

@huozhi
Copy link
Member

huozhi commented Sep 12, 2024

We released v14.2.10 with the fix, could you upgrade to the latest version and verify if that fixed for you? 🙏 Thanks

@huozhi huozhi closed this as completed Sep 12, 2024
@ezeparziale
Copy link

Server actions work well in 14.2.10 and 14.2.11.

@luskin
Copy link

luskin commented Sep 18, 2024

This error is still present in next@15.0.0-canary.158 in production builds only. No issues in dev server.

@huozhi
Copy link
Member

huozhi commented Sep 18, 2024

@luskin can you file a new issue with your reproduction?

@BleedingDev
Copy link

BleedingDev commented Sep 23, 2024

@luskin can you file a new issue with your reproduction?

Fixed in Next.js canary.163 with React 19.0.0-rc-5d19e1c8-20240923. Had this problem on canary.160.

Copy link
Contributor

github-actions bot commented Oct 8, 2024

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 Oct 8, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 8, 2024
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 Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.