Skip to content

Commit

Permalink
Turbopack: ignore invalid sourcemaps for now (#76215)
Browse files Browse the repository at this point in the history
#75791 made the sourcemap handling stricter, it caused this when encountering invalid input source maps (i.e. the ones that are published to npm).
Let's keep the old behavior for now (which is also what Webpack does). 
```
Caused by:
    0: Execution of run_inner_operation failed
    1: Execution of run_test_operation failed
    2: Execution of ModuleGraph::from_modules failed
    3: Execution of SingleModuleGraph::new_with_entries failed
    4: [project]/turbopack/crates/turbopack-tests/tests/snapshot/source_maps/invalid/input/index.js [test] (ecmascript)
    5: Execution of primary_chunkable_referenced_modules failed
    6: Execution of <EcmascriptModuleAsset as Module>::references failed
    7: Execution of analyse_ecmascript_module failed
    8: failed to analyse ecmascript module '[project]/turbopack/crates/turbopack-tests/tests/snapshot/source_maps/invalid/input/index.js [test] (ecmascript)'
    9: Execution of <SourceMapReference as GenerateSourceMap>::generate_source_map failed
   10: invalid type: string "use client", expected struct SourceMapJson at line 1 column 12
```
  • Loading branch information
mischnic authored Feb 21, 2025
1 parent 1708497 commit 5e236c9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
16 changes: 11 additions & 5 deletions turbopack/crates/turbopack-core/src/source_map/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct SourceMapSectionItemJson {
map: SourceMapJson,
}

// TODO this could be made (much) more efficient by not even de- and serializing other fields
// (apart from `sources`) and just keep storing them as strings.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SourceMapJson {
Expand Down Expand Up @@ -137,7 +139,10 @@ pub async fn resolve_source_map_sources(
return Ok(None);
};

let mut map: SourceMapJson = serde_json::from_reader(map.read())?;
let Ok(mut map): serde_json::Result<SourceMapJson> = serde_json::from_reader(map.read()) else {
// Silently ignore invalid sourcemaps
return Ok(None);
};

resolve_map(&mut map, origin).await?;
for section in map.sections.iter_mut().flatten() {
Expand All @@ -158,17 +163,18 @@ pub async fn fileify_source_map(
return Ok(None);
};

let Ok(mut map): serde_json::Result<SourceMapJson> = serde_json::from_reader(map.read()) else {
// Silently ignore invalid sourcemaps
return Ok(None);
};

let context_fs = context_path.fs();
let context_fs = &*Vc::try_resolve_downcast_type::<DiskFileSystem>(context_fs)
.await?
.context("Expected the chunking context to have a DiskFileSystem")?
.await?;
let prefix = format!("{}///[{}]/", SOURCE_URL_PROTOCOL, context_fs.name());

// TODO this could be made (much) more efficient by not even de- and serializing other fields
// (apart from `sources`) and just keep storing them as strings.
let mut map: SourceMapJson = serde_json::from_reader(map.read())?;

let transform_source = async |src: &mut Option<String>| {
if let Some(src) = src {
if let Some(src_rest) = src.strip_prefix(&prefix) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
"output/4e721_crates_turbopack-tests_tests_snapshot_source_maps_invalid_input_index_66486b.js",
{},
{"otherChunks":["output/4e721_crates_turbopack-tests_tests_snapshot_source_maps_invalid_input_index_fc734e.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/source_maps/invalid/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e236c9

Please sign in to comment.