Unverified Commit 7e5e69dd authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: deterministic server action order (#76905)

`HashMap` strikes again, causing the server action manifest order to be non-deterministic.

Less test flakeyness for `app-dir - server-action-period-hash-custom-key should have different manifest if the encryption key from process env is same`
parent 7d62789c
...@@ -213,7 +213,7 @@ impl ServerActionsGraph { ...@@ -213,7 +213,7 @@ impl ServerActionsGraph {
return Ok(Vc::cell(Default::default())); return Ok(Vc::cell(Default::default()));
} }
let mut result = FxHashMap::default(); let mut result = FxIndexMap::default();
graph.traverse_from_entry(entry, |node| { graph.traverse_from_entry(entry, |node| {
if let Some(node_data) = data.get(&node.module) { if let Some(node_data) = data.get(&node.module) {
result.insert(node.module, *node_data); result.insert(node.module, *node_data);
......
...@@ -7,7 +7,6 @@ use next_core::{ ...@@ -7,7 +7,6 @@ use next_core::{
}, },
util::NextRuntime, util::NextRuntime,
}; };
use rustc_hash::FxHashMap;
use swc_core::{ use swc_core::{
atoms::Atom, atoms::Atom,
common::comments::Comments, common::comments::Comments,
...@@ -406,7 +405,7 @@ struct OptionActionMap(Option<ResolvedVc<ActionMap>>); ...@@ -406,7 +405,7 @@ struct OptionActionMap(Option<ResolvedVc<ActionMap>>);
type LayerAndActions = (ActionLayer, ResolvedVc<ActionMap>); type LayerAndActions = (ActionLayer, ResolvedVc<ActionMap>);
/// A mapping of every module module containing Server Actions, mapping to its layer and actions. /// A mapping of every module module containing Server Actions, mapping to its layer and actions.
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct AllModuleActions(FxHashMap<ResolvedVc<Box<dyn Module>>, LayerAndActions>); pub struct AllModuleActions(FxIndexMap<ResolvedVc<Box<dyn Module>>, LayerAndActions>);
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn map_server_actions(graph: Vc<SingleModuleGraph>) -> Result<Vc<AllModuleActions>> { pub async fn map_server_actions(graph: Vc<SingleModuleGraph>) -> Result<Vc<AllModuleActions>> {
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup, type NextInstance } from 'e2e-utils'
async function getServerActionManifest(next) { async function getServerActionManifestNodeKeys(next: NextInstance) {
const content = await next.readFile( const manifest = await next.readJSON(
'.next/server/server-reference-manifest.json' '.next/server/server-reference-manifest.json'
) )
return JSON.parse(content) return Object.keys(manifest.node)
}
function compareServerActionManifestKeys(a, b, equal) {
a = a.node
b = b.node
const keysA = Object.keys(a)
const keysB = Object.keys(b)
if (equal) {
expect(keysA).toEqual(keysB)
} else {
expect(keysA).not.toEqual(keysB)
}
} }
describe('app-dir - server-action-period-hash-custom-key', () => { describe('app-dir - server-action-period-hash-custom-key', () => {
...@@ -27,32 +13,30 @@ describe('app-dir - server-action-period-hash-custom-key', () => { ...@@ -27,32 +13,30 @@ describe('app-dir - server-action-period-hash-custom-key', () => {
skipStart: true, skipStart: true,
}) })
it('should have different manifest if the encryption key from process env is changed', async () => { it('should have a different manifest if the encryption key from process env is changed', async () => {
process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY = 'my-secret-key1' process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY = 'my-secret-key1'
await next.build() await next.build()
delete process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY delete process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY
const firstManifest = await getServerActionManifest(next) const firstActionIds = await getServerActionManifestNodeKeys(next)
process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY = 'my-secret-key2' process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY = 'my-secret-key2'
await next.build() await next.build()
delete process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY delete process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY
const secondActionIds = await getServerActionManifestNodeKeys(next)
const secondManifest = await getServerActionManifest(next) expect(firstActionIds).not.toEqual(secondActionIds)
compareServerActionManifestKeys(firstManifest, secondManifest, false)
}) })
it('should have different manifest if the encryption key from process env is same', async () => { it('should have the same manifest if the encryption key from process env is same', async () => {
process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY = 'my-secret-key' process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY = 'my-secret-key'
await next.build() await next.build()
const firstManifest = await getServerActionManifest(next) const firstActionIds = await getServerActionManifestNodeKeys(next)
await next.remove('.next') // dismiss cache await next.remove('.next') // dismiss cache
await next.build() // build with the same secret key await next.build() // build with the same secret key
delete process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY delete process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY
const secondActionIds = await getServerActionManifestNodeKeys(next)
const secondManifest = await getServerActionManifest(next) expect(firstActionIds).toEqual(secondActionIds)
compareServerActionManifestKeys(firstManifest, secondManifest, true)
}) })
}) })
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup, type NextInstance } from 'e2e-utils'
async function getServerActionManifest(next) { async function getServerActionManifestNodeKeys(next: NextInstance) {
const content = await next.readFile( const manifest = await next.readJSON(
'.next/server/server-reference-manifest.json' '.next/server/server-reference-manifest.json'
) )
return JSON.parse(content) return Object.keys(manifest.node)
}
function compareServerActionManifestKeys(a, b, equal) {
a = a.node
b = b.node
const keysA = Object.keys(a)
const keysB = Object.keys(b)
if (equal) {
expect(keysA).toEqual(keysB)
} else {
expect(keysA).not.toEqual(keysB)
}
} }
describe('app-dir - server-action-period-hash', () => { describe('app-dir - server-action-period-hash', () => {
...@@ -29,23 +15,23 @@ describe('app-dir - server-action-period-hash', () => { ...@@ -29,23 +15,23 @@ describe('app-dir - server-action-period-hash', () => {
it('should have same manifest between continuous two builds', async () => { it('should have same manifest between continuous two builds', async () => {
await next.build() await next.build()
const firstManifest = await getServerActionManifest(next) const firstActionIds = await getServerActionManifestNodeKeys(next)
await next.build() await next.build()
const secondManifest = await getServerActionManifest(next) const secondActionIds = await getServerActionManifestNodeKeys(next)
compareServerActionManifestKeys(firstManifest, secondManifest, true) expect(firstActionIds).toEqual(secondActionIds)
}) })
it('should have different manifest between two builds with period hash', async () => { it('should have different manifest between two builds with period hash', async () => {
await next.build() await next.build()
const firstManifest = await getServerActionManifest(next) const firstActionIds = await getServerActionManifestNodeKeys(next)
await next.remove('.next') // dismiss cache await next.remove('.next') // dismiss cache
await next.build() await next.build()
const secondManifest = await getServerActionManifest(next) const secondActionIds = await getServerActionManifestNodeKeys(next)
compareServerActionManifestKeys(firstManifest, secondManifest, false) expect(firstActionIds).not.toEqual(secondActionIds)
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment