Skip to content

fix(turbopack): handle cyclic or rootless nodes in ModuleGraphImportTracer - #98204

Open
agentHits wants to merge 1 commit into
vercel:canaryfrom
agentHits:fix/turbopack-module-graph-root-path-panic
Open

fix(turbopack): handle cyclic or rootless nodes in ModuleGraphImportTracer#98204
agentHits wants to merge 1 commit into
vercel:canaryfrom
agentHits:fix/turbopack-module-graph-root-path-panic

Conversation

@agentHits

Copy link
Copy Markdown

What?

In turbopack/crates/turbopack-core/src/module_graph/mod.rs, replaces unreachable!("there must be a path to a root") with a graceful fallback to vec![module_idx] when astar cannot find a root in the reversed graph.

Why?

During standalone static prerendering with complex CSS / SCSS import trees, when Turbopack formats an issue diagnostic via ModuleGraphImportTracer::get_traces, cyclic imports or orphaned asset nodes can cause astar on reversed_graph to return None (since no node satisfies neighbors(n).next().is_none()).

Previously, this triggered an unexpected Rust panic that killed the entire build worker:

thread 'tokio-rt-worker' panicked at turbopack/crates/turbopack-core/src/module_graph/mod.rs:750:25:
internal error: entered unreachable code: there must be a path to a root

How?

Instead of panicking with unreachable!, Turbopack now falls back to returning the single module_idx so issue reporting can proceed without terminating the process:

let path = match petgraph::algo::astar(
    &reversed_graph,
    module_idx,
    |n| reversed_graph.neighbors(n).next().is_none(),
    |e| match e.weight().chunking_type {
        ChunkingType::Parallel { .. } => 0,
        _ => 1,
    },
    |_| 0,
) {
    Some((_, path)) => path,
    None => vec![module_idx],
};

…racer

Avoid unreachable!() panic when astar cannot find a path to a root in reversed_graph. Fallback gracefully to returning the current module index instead of crashing the entire build worker.
@agentHits

Copy link
Copy Markdown
Author

Fixes #98205

Linked Issue: #98205
Reproduction & Benchmark: https://github.com/agentHits/next-prerender-workstore-reproduction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant