diff --git a/src/node.cc b/src/node.cc index 89e0e5524c2102..14e27806a5d3b2 100644 --- a/src/node.cc +++ b/src/node.cc @@ -838,6 +838,12 @@ static ExitCode InitializeNodeWithArgsInternal( V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1); #endif + if (!!(flags & ProcessInitializationFlags::kGeneratePredictableSnapshot) || + per_process::cli_options->per_isolate->build_snapshot) { + v8::V8::SetFlagsFromString("--predictable"); + v8::V8::SetFlagsFromString("--random_seed=42"); + } + // Specify this explicitly to avoid being affected by V8 changes to the // default value. V8::SetFlagsFromString("--rehash-snapshot"); diff --git a/src/node.h b/src/node.h index ca01c42e8af484..f2740116a4710b 100644 --- a/src/node.h +++ b/src/node.h @@ -265,6 +265,8 @@ enum Flags : uint32_t { // cppgc::InitializeProcess() before creating a Node.js environment // and call cppgc::ShutdownProcess() before process shutdown. kNoInitializeCppgc = 1 << 13, + // Initialize the process for predictable snapshot generation. + kGeneratePredictableSnapshot = 1 << 14, // Emulate the behavior of InitializeNodeWithArgs() when passing // a flags argument to the InitializeOncePerProcess() replacement diff --git a/tools/snapshot/node_mksnapshot.cc b/tools/snapshot/node_mksnapshot.cc index a9df251700ea06..841a8ca743bcaa 100644 --- a/tools/snapshot/node_mksnapshot.cc +++ b/tools/snapshot/node_mksnapshot.cc @@ -50,8 +50,6 @@ int main(int argc, char* argv[]) { setvbuf(stderr, nullptr, _IONBF, 0); #endif // _WIN32 - v8::V8::SetFlagsFromString("--random_seed=42"); - v8::V8::SetFlagsFromString("--harmony-import-assertions"); return BuildSnapshot(argc, argv); } @@ -65,7 +63,8 @@ int BuildSnapshot(int argc, char* argv[]) { std::unique_ptr result = node::InitializeOncePerProcess( - std::vector(argv, argv + argc)); + std::vector(argv, argv + argc), + node::ProcessInitializationFlags::kGeneratePredictableSnapshot); CHECK(!result->early_return()); CHECK_EQ(result->exit_code(), 0);