Commit b1ab3408 authored by Janka Uryga's avatar Janka Uryga

abort and report on sync IO errors in dev

parent 31f18201
......@@ -17,6 +17,7 @@ import {
} from '../app-render/work-async-storage.external'
import type {
DevStoreModernPartial,
DevRequestStoreModern,
PrerenderStoreModernRuntime,
RequestStore,
} from '../app-render/work-unit-async-storage.external'
......@@ -2789,6 +2790,7 @@ async function renderWithRestartOnCacheMissInDev(
htmlRequestId,
renderOpts,
requestId,
workStore,
componentMod: {
routeModule: {
userland: { loaderTree },
......@@ -2803,6 +2805,13 @@ async function renderWithRestartOnCacheMissInDev(
} = renderOpts
assertClientReferenceManifest(clientReferenceManifest)
const captureOwnerStack = ComponentMod.captureOwnerStack
// Check if any segment of the current page has runtime prefetching enabled.
// Note that if we're in a client navigation, this config might come from
// a shared layout parent that won't actually be rendered here.
// However, if the parent is runtime prefetchable, then all of its children
// can potentially run as part of a runtime prefetch, so it makes sense to validate them.
const hasRuntimePrefetch =
await anySegmentHasRuntimePrefetchEnabled(loaderTree)
......@@ -2810,7 +2819,8 @@ async function renderWithRestartOnCacheMissInDev(
let requestStore: RequestStore = initialRequestStore
const environmentName = () => {
const currentStage = requestStore.stagedRendering!.currentStage
const { stagedRendering } = requestStore as DevRequestStoreModern
const currentStage = stagedRendering.currentStage
switch (currentStage) {
case RenderStage.Static:
return 'Prerender'
......@@ -2824,6 +2834,25 @@ async function renderWithRestartOnCacheMissInDev(
}
}
const throwIfInvalidDynamic = (expectedStage: RenderStage) => {
const { stagedRendering, dynamicTracking } =
requestStore as DevRequestStoreModern
if (
expectedStage !== RenderStage.Dynamic &&
// Sync IO errors advance us to the dynamic stage.
stagedRendering.currentStage === RenderStage.Dynamic
) {
// We should always have an error set, but be defensive
if (dynamicTracking.syncDynamicErrorWithStack) {
throw dynamicTracking.syncDynamicErrorWithStack
}
}
if (workStore.invalidDynamicUsageError) {
throw workStore.invalidDynamicUsageError
}
}
//===============================================
// Initial render
//===============================================
......@@ -2861,6 +2890,10 @@ async function renderWithRestartOnCacheMissInDev(
),
prerenderResumeDataCache,
cacheSignal,
dynamicTracking: createDynamicTrackingState(
false // isDebugDynamicAccesses
),
captureOwnerStack,
} satisfies DevStoreModernPartial)
let debugChannel = setReactDebugChannel && createDebugChannel()
......@@ -2899,6 +2932,8 @@ async function renderWithRestartOnCacheMissInDev(
return stream
},
(stream) => {
throwIfInvalidDynamic(/* expected stage */ RenderStage.Static)
// Runtime stage
initialStageController.advanceStage(RenderStage.Runtime)
......@@ -2912,7 +2947,9 @@ async function renderWithRestartOnCacheMissInDev(
// and see if there's any cache misses in the runtime stage.
return stream
},
async (maybeStream) => {
(maybeStream) => {
throwIfInvalidDynamic(/* expected stage */ RenderStage.Runtime)
// Dynamic stage
// If we had cache misses in either of the previous stages,
......@@ -2954,6 +2991,10 @@ async function renderWithRestartOnCacheMissInDev(
await cacheSignal.cacheReady()
initialReactController.abort()
throwIfInvalidDynamic(
// If we're warming caches, we shouldn't have advanced past the runtime stage.
RenderStage.Runtime
)
//===============================================
// Final render (restarted)
......@@ -2979,6 +3020,10 @@ async function renderWithRestartOnCacheMissInDev(
),
prerenderResumeDataCache: null,
cacheSignal: null,
dynamicTracking: createDynamicTrackingState(
false // isDebugDynamicAccesses
),
captureOwnerStack: ComponentMod.captureOwnerStack,
} satisfies DevStoreModernPartial)
// The initial render already wrote to its debug channel.
......@@ -3002,11 +3047,15 @@ async function renderWithRestartOnCacheMissInDev(
)
},
(stream) => {
throwIfInvalidDynamic(/* expected stage */ RenderStage.Static)
// Runtime stage
finalStageController.advanceStage(RenderStage.Runtime)
return stream
},
(stream) => {
throwIfInvalidDynamic(/* expected stage */ RenderStage.Runtime)
// Dynamic stage
finalStageController.advanceStage(RenderStage.Dynamic)
return stream
......
......@@ -23,10 +23,10 @@
import type { WorkStore } from '../app-render/work-async-storage.external'
import type {
WorkUnitStore,
RequestStore,
PrerenderStoreLegacy,
PrerenderStoreModern,
PrerenderStoreModernRuntime,
DevRequestStoreModern,
} from '../app-render/work-unit-async-storage.external'
// Once postpone is in stable we should switch to importing the postpone export directly
......@@ -296,14 +296,18 @@ export function abortOnSynchronousPlatformIOAccess(
}
export function trackSynchronousPlatformIOAccessInDev(
requestStore: RequestStore
requestStore: DevRequestStoreModern,
errorWithStack: Error
): void {
const { stagedRendering, dynamicTracking } = requestStore
// We don't actually have a controller to abort but we do the semantic equivalent by
// advancing the request store out of the prerender stage
if (requestStore.stagedRendering) {
// TODO: error for sync IO in the runtime stage
// (which is not currently covered by the validation render in `spawnDynamicValidationInDev`)
requestStore.stagedRendering.advanceStage(RenderStage.Dynamic)
// TODO: error for sync IO in the runtime stage
// (which is not currently covered by the validation render in `spawnDynamicValidationInDev`)
stagedRendering.advanceStage(RenderStage.Dynamic)
if (dynamicTracking.syncDynamicErrorWithStack === null) {
dynamicTracking.syncDynamicErrorWithStack = errorWithStack
}
}
......
......@@ -71,6 +71,7 @@ export type RequestStore = ProdRequestStore | DevRequestStore
export type ProdRequestStore = BaseRequestStore & AllMissing<DevStore>
export type DevRequestStore = BaseRequestStore & DevStore
export type DevRequestStoreModern = BaseRequestStore & DevStoreModern
// If `cacheComponents` is enabled, we add multiple extra properties on the store.
// We either want all of them to be present, or all of them to be undefined.
......@@ -90,6 +91,8 @@ type DevStoreCommon = {
export type DevStoreModernPartial = {
readonly stagedRendering: StagedRenderingController
readonly asyncApiPromises: DevAsyncApiPromises
readonly captureOwnerStack: () => string | null
readonly dynamicTracking: DynamicTrackingState
} & (
| {
// In the initial render, we track and fill caches
......
......@@ -263,7 +263,7 @@ function createRequestStoreImpl(
serverComponentsHmrCache:
serverComponentsHmrCache ||
(globalThis as any).__serverComponentsHmrCache,
devFallbackParams,
devFallbackParams: devFallbackParams ?? null,
}
}
......
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import {
workUnitAsyncStorage,
type DevRequestStoreModern,
type PrerenderStoreModern,
} from '../app-render/work-unit-async-storage.external'
import {
......@@ -87,7 +88,31 @@ export function io(expression: string, type: ApiType) {
}
case 'request':
if (process.env.NODE_ENV === 'development') {
trackSynchronousPlatformIOAccessInDev(workUnitStore)
if (workUnitStore.stagedRendering) {
// If the prerender signal is already aborted we don't need to construct
// any stacks because something else actually terminated the prerender.
let message: string
switch (type) {
case 'time':
message = `Route "${workStore.route}" used ${expression} before accessing either uncached data (e.g. \`fetch()\`) or \`connection()\`. Accessing the current time in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time`
break
case 'random':
message = `Route "${workStore.route}" used ${expression} before accessing either uncached data (e.g. \`fetch()\`) or \`connection()\`. Accessing random values synchronously in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-random`
break
case 'crypto':
message = `Route "${workStore.route}" used ${expression} before accessing either uncached data (e.g. \`fetch()\`) or \`connection()\`. Accessing random cryptographic values synchronously in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-crypto`
break
default:
throw new InvariantError(
'Unknown expression type in abortOnSynchronousPlatformIOAccess.'
)
}
trackSynchronousPlatformIOAccessInDev(
workUnitStore,
applyOwnerStack(new Error(message), workUnitStore)
)
}
}
break
case 'prerender-ppr':
......@@ -101,7 +126,10 @@ export function io(expression: string, type: ApiType) {
}
}
function applyOwnerStack(error: Error, workUnitStore: PrerenderStoreModern) {
function applyOwnerStack(
error: Error,
workUnitStore: PrerenderStoreModern | DevRequestStoreModern
) {
// TODO: Instead of stitching the stacks here, we should log the original
// error as-is when it occurs, and let `patchErrorInspect` handle adding the
// owner stack, instead of logging it deferred in the `LogSafely` component
......
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