Unverified Commit 5e236c99 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: ignore invalid sourcemaps for now (#76215)

https://github.com/vercel/next.js/pull/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
```
parent 1708497b
...@@ -41,6 +41,8 @@ struct SourceMapSectionItemJson { ...@@ -41,6 +41,8 @@ struct SourceMapSectionItemJson {
map: SourceMapJson, 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)] #[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct SourceMapJson { struct SourceMapJson {
...@@ -137,7 +139,10 @@ pub async fn resolve_source_map_sources( ...@@ -137,7 +139,10 @@ pub async fn resolve_source_map_sources(
return Ok(None); 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?; resolve_map(&mut map, origin).await?;
for section in map.sections.iter_mut().flatten() { for section in map.sections.iter_mut().flatten() {
...@@ -158,6 +163,11 @@ pub async fn fileify_source_map( ...@@ -158,6 +163,11 @@ pub async fn fileify_source_map(
return Ok(None); 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 = context_path.fs();
let context_fs = &*Vc::try_resolve_downcast_type::<DiskFileSystem>(context_fs) let context_fs = &*Vc::try_resolve_downcast_type::<DiskFileSystem>(context_fs)
.await? .await?
...@@ -165,10 +175,6 @@ pub async fn fileify_source_map( ...@@ -165,10 +175,6 @@ pub async fn fileify_source_map(
.await?; .await?;
let prefix = format!("{}///[{}]/", SOURCE_URL_PROTOCOL, context_fs.name()); 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>| { let transform_source = async |src: &mut Option<String>| {
if let Some(src) = src { if let Some(src) = src {
if let Some(src_rest) = src.strip_prefix(&prefix) { if let Some(src_rest) = src.strip_prefix(&prefix) {
......
(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
\ No newline at end of file
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/4e721_crates_turbopack-tests_tests_snapshot_source_maps_invalid_input_index_fc734e.js", {
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/source_maps/invalid/input/index.js [test] (ecmascript)": (function(__turbopack_context__) {
var { g: global, d: __dirname, m: module, e: exports } = __turbopack_context__;
{
console.log("test") //# sourceMappingURL=index.js.map
;
}}),
}]);
//# sourceMappingURL=4e721_crates_turbopack-tests_tests_snapshot_source_maps_invalid_input_index_fc734e.js.map
\ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 6, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/source_maps/invalid/input/index.js"],"sourcesContent":["console.log(\"test\")\n//# sourceMappingURL=index.js.map\n"],"names":[],"mappings":"AAAA,QAAQ,GAAG,CAAC,QACZ,iCAAiC"}},
{"offset": {"line": 8, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}]
}
\ No newline at end of file
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