Skip to content

feat(sourcemaps): Support injecting indexed sourcemaps #2470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 44 additions & 102 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ sentry = { version = "0.34.0", default-features = false, features = [
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
sha1_smol = { version = "1.0.0", features = ["serde"] }
sourcemap = { version = "9.1.2", features = ["ram_bundle"] }
sourcemap = { version = "9.2.0", features = ["ram_bundle"] }
symbolic = { version = "12.13.3", features = ["debuginfo-serde", "il2cpp"] }
thiserror = "1.0.38"
url = "2.3.1"
Expand Down
124 changes: 104 additions & 20 deletions src/utils/sourcemaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use indicatif::ProgressStyle;
use log::{debug, info, warn};
use sentry::types::DebugId;
use sha1_smol::Digest;
use sourcemap::SourceMap;
use sourcemap::{DecodedMap, SourceMap};
use symbolic::debuginfo::js::{
discover_debug_id, discover_sourcemap_embedded_debug_id, discover_sourcemaps_location,
};
Expand Down Expand Up @@ -947,20 +947,54 @@ impl SourceMapProcessor {
bail!("Invalid embedded sourcemap in source file {source_url}");
};

let mut sourcemap = SourceMap::from_slice(&decoded).with_context(|| {
format!("Invalid embedded sourcemap in source file {source_url}")
})?;
let mut sourcemap =
sourcemap::decode_slice(&decoded).with_context(|| {
format!("Invalid embedded sourcemap in source file {source_url}")
})?;

let debug_id = sourcemap
.get_debug_id()
.debug_id()
.unwrap_or_else(|| inject::debug_id_from_bytes_hashed(&decoded));

let source_file = self.sources.get_mut(source_url).unwrap();
let source_file_contents = Arc::make_mut(&mut source_file.contents);
let adjustment_map = inject::fixup_js_file(source_file_contents, debug_id)
.context(format!("Failed to process {}", source_file.path.display()))?;

sourcemap.adjust_mappings(&adjustment_map);
match &mut sourcemap {
DecodedMap::Regular(sourcemap) => {
adjust_regular_sourcemap(sourcemap, source_file, debug_id)?;
}
DecodedMap::Hermes(sourcemap) => {
adjust_regular_sourcemap(sourcemap, source_file, debug_id)?;
}
DecodedMap::Index(sourcemap) => {
let source_file_contents = Arc::make_mut(&mut source_file.contents);
if sourcemap.adjust_sections_offset_rows(1) {
let injected = inject::inject_at_start(
str::from_utf8(source_file_contents).with_context(
|| {
format!(
"{} is not UTF-8",
source_file.path.display()
)
},
)?,
debug_id,
);

source_file_contents.clear();
source_file_contents.extend(injected.as_bytes());
} else {
// We can't adjust the section offset rows, so we have to inject at the end
inject::fixup_js_file_end(source_file_contents, debug_id)
.with_context(|| {
format!(
"Failed to inject debug id into {}",
source_file.path.display()
)
})?;
}
}
}

sourcemap.set_debug_id(Some(debug_id));

decoded.clear();
Expand All @@ -969,7 +1003,10 @@ impl SourceMapProcessor {
let encoded = data_encoding::BASE64.encode(&decoded);
let new_sourcemap_url = format!("{DATA_PREAMBLE}{encoded}");

inject::replace_sourcemap_url(source_file_contents, &new_sourcemap_url)?;
inject::replace_sourcemap_url(
Arc::make_mut(&mut source_file.contents),
&new_sourcemap_url,
)?;
*sourcemap_url = Some(SourceMapReference::from_url(new_sourcemap_url));

debug_id
Expand Down Expand Up @@ -1000,11 +1037,12 @@ impl SourceMapProcessor {
let (mut sourcemap, debug_id, debug_id_fresh) = {
let sourcemap_file = &self.sources[&sourcemap_url];

let sm = SourceMap::from_slice(&sourcemap_file.contents).context(
format!("Invalid sourcemap at {}", sourcemap_file.url),
)?;
let sm =
sourcemap::decode_slice(&sourcemap_file.contents).context(
format!("Invalid sourcemap at {}", sourcemap_file.url),
)?;

match sm.get_debug_id() {
match sm.debug_id() {
Some(debug_id) => (sm, debug_id, false),
None => {
let debug_id = inject::debug_id_from_bytes_hashed(
Expand All @@ -1016,13 +1054,45 @@ impl SourceMapProcessor {
};

let source_file = self.sources.get_mut(source_url).unwrap();
let adjustment_map = inject::fixup_js_file(
Arc::make_mut(&mut source_file.contents),
debug_id,
)
.context(format!("Failed to process {}", source_file.path.display()))?;

sourcemap.adjust_mappings(&adjustment_map);
match &mut sourcemap {
DecodedMap::Regular(sourcemap) => {
adjust_regular_sourcemap(sourcemap, source_file, debug_id)?;
}
DecodedMap::Hermes(sourcemap) => {
adjust_regular_sourcemap(sourcemap, source_file, debug_id)?;
}
DecodedMap::Index(sourcemap) => {
let source_file_contents =
Arc::make_mut(&mut source_file.contents);
if sourcemap.adjust_sections_offset_rows(1) {
let injected = inject::inject_at_start(
str::from_utf8(source_file_contents).with_context(
|| {
format!(
"{} is not UTF-8",
source_file.path.display()
)
},
)?,
debug_id,
);

source_file_contents.clear();
source_file_contents.extend(injected.as_bytes());
} else {
// We can't adjust the section offset rows, so we have to inject at the end
inject::fixup_js_file_end(source_file_contents, debug_id)
.with_context(|| {
format!(
"Failed to inject debug id into {}",
source_file.path.display()
)
})?;
}
}
}

sourcemap.set_debug_id(Some(debug_id));

let sourcemap_file = self.sources.get_mut(&sourcemap_url).unwrap();
Expand Down Expand Up @@ -1103,6 +1173,20 @@ impl SourceMapProcessor {
}
}

fn adjust_regular_sourcemap(
sourcemap: &mut SourceMap,
source_file: &mut SourceFile,
debug_id: DebugId,
) -> Result<()> {
#[expect(deprecated)]
let adjustment_map = inject::fixup_js_file(Arc::make_mut(&mut source_file.contents), debug_id)
.context(format!("Failed to process {}", source_file.path.display()))?;

sourcemap.adjust_mappings(&adjustment_map);

Ok(())
}

fn validate_script(source: &mut SourceFile) -> Result<()> {
if let Some(sm_ref) = get_sourcemap_ref(source) {
if let sourcemap::SourceMapRef::LegacyRef(_) = sm_ref {
Expand Down
Loading
Loading