Skip to content
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
38 changes: 24 additions & 14 deletions src/utils/sourcemaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::utils::file_upload::{
};
use crate::utils::logging::is_quiet_mode;
use crate::utils::progress::ProgressBar;
use crate::utils::sourcemaps::inject::InjectReport;
use crate::utils::sourcemaps::inject::{InjectReportBuilder, ReportItem};

pub mod inject;

Expand Down Expand Up @@ -801,7 +801,7 @@ impl SourceMapProcessor {
self.collect_sourcemap_references();
println!("{} Injecting debug ids", style(">").dim());

let mut report = InjectReport::default();
let mut report_builder = InjectReportBuilder::default();

let mut sourcemaps = self
.sources
Expand All @@ -821,9 +821,11 @@ impl SourceMapProcessor {
}

if let Some(debug_id) = self.debug_ids.get(source_url) {
report
.previously_injected
.push((source_url.into(), *debug_id));
report_builder.previously_injected.push(ReportItem::new(
source_url.into(),
source_url.clone(),
*debug_id,
));
continue;
}

Expand Down Expand Up @@ -1017,13 +1019,17 @@ impl SourceMapProcessor {
}

if debug_id_fresh {
report
.sourcemaps
.push((sourcemap_file.path.clone(), debug_id));
report_builder.sourcemaps.push(ReportItem::new(
sourcemap_file.path.clone(),
sourcemap_file.url.clone(),
debug_id,
));
} else {
report
.skipped_sourcemaps
.push((sourcemap_file.path.clone(), debug_id));
report_builder.skipped_sourcemaps.push(ReportItem::new(
sourcemap_file.path.clone(),
sourcemap_file.url.clone(),
debug_id,
));
}

debug_id
Expand Down Expand Up @@ -1065,11 +1071,15 @@ impl SourceMapProcessor {
))?;
}

report.injected.push((source_file.path.clone(), debug_id));
report_builder.injected.push(ReportItem::new(
source_file.path.clone(),
source_file.url.clone(),
debug_id,
));
}

if !report.is_empty() {
println!("{report}");
if !report_builder.is_empty() {
println!("{}", report_builder.into_report(&self.sources));
} else {
println!("> Nothing to inject")
}
Expand Down
83 changes: 65 additions & 18 deletions src/utils/sourcemaps/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use magic_string::{GenerateDecodedMapOptions, MagicString};
use sentry::types::DebugId;
use sourcemap::SourceMap;

use crate::utils::file_upload::SourceFiles;

const CODE_SNIPPET_TEMPLATE: &str = r#"!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="__SENTRY_DEBUG_ID__")}catch(e){}}();"#;
const DEBUGID_PLACEHOLDER: &str = "__SENTRY_DEBUG_ID__";
const DEBUGID_COMMENT_PREFIX: &str = "//# debugId";
Expand All @@ -41,11 +43,19 @@ static USE_DIRECTIVE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
fn print_section_with_debugid(
f: &mut fmt::Formatter<'_>,
title: &str,
data: &[(PathBuf, DebugId)],
data: &[ReportItem],
sourcefiles: &SourceFiles,
) -> fmt::Result {
print_section_title(f, title)?;
for (path, debug_id) in data.iter().sorted_by_key(|x| &x.0) {
writeln!(f, " {debug_id} - {}", path.display())?;
for item in data.iter().sorted_by_key(|x| &x.path) {
writeln!(f, " {} - {}", item.debug_id, item.path.display())?;
for (level, message) in sourcefiles
.get(&item.url)
.iter()
.flat_map(|sourcefile| &sourcefile.messages)
{
writeln!(f, " - {}: {message}", style(&level).red())?;
}
}
Ok(())
}
Expand All @@ -54,60 +64,97 @@ fn print_section_title(f: &mut fmt::Formatter<'_>, title: &str) -> fmt::Result {
writeln!(f, " {}", style(title).yellow().bold())
}

#[derive(Debug, Clone)]
pub struct ReportItem {
path: PathBuf,
url: String,
debug_id: DebugId,
}

/// Report for the inject operation, including any warnings or errors from the sources.
pub struct InjectReport<'a> {
inner: InjectReportBuilder,
sourcefiles: &'a SourceFiles,
}

/// Builder for the inject report.
/// To print the report, we need to add the sources to the report,
/// as we pull any warnings or errors from there.
#[derive(Debug, Clone, Default)]
pub struct InjectReport {
pub injected: Vec<(PathBuf, DebugId)>,
pub previously_injected: Vec<(PathBuf, DebugId)>,
pub sourcemaps: Vec<(PathBuf, DebugId)>,
pub skipped_sourcemaps: Vec<(PathBuf, DebugId)>,
pub struct InjectReportBuilder {
pub injected: Vec<ReportItem>,
pub previously_injected: Vec<ReportItem>,
pub sourcemaps: Vec<ReportItem>,
pub skipped_sourcemaps: Vec<ReportItem>,
}

impl InjectReport {
impl ReportItem {
pub fn new(path: PathBuf, url: String, debug_id: DebugId) -> Self {
Self {
path,
url,
debug_id,
}
}
}

impl InjectReportBuilder {
pub fn is_empty(&self) -> bool {
self.injected.is_empty()
&& self.previously_injected.is_empty()
&& self.sourcemaps.is_empty()
&& self.skipped_sourcemaps.is_empty()
}

pub fn into_report<'a>(self, sourcefiles: &'a SourceFiles) -> InjectReport<'a> {
InjectReport {
inner: self,
sourcefiles,
}
}
}

impl fmt::Display for InjectReport {
impl fmt::Display for InjectReport<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"\n{}",
style("Source Map Debug ID Injection Report").dim().bold()
)?;

if !self.injected.is_empty() {
if !self.inner.injected.is_empty() {
print_section_with_debugid(
f,
"Modified: The following source files have been modified to have debug ids",
&self.injected,
&self.inner.injected,
self.sourcefiles,
)?;
}

if !self.sourcemaps.is_empty() {
if !self.inner.sourcemaps.is_empty() {
print_section_with_debugid(
f,
"Modified: The following sourcemap files have been modified to have debug ids",
&self.sourcemaps,
&self.inner.sourcemaps,
self.sourcefiles,
)?;
}

if !self.previously_injected.is_empty() {
if !self.inner.previously_injected.is_empty() {
print_section_with_debugid(
f,
"Ignored: The following source files already have debug ids",
&self.previously_injected,
&self.inner.previously_injected,
self.sourcefiles,
)?;
}

if !self.skipped_sourcemaps.is_empty() {
if !self.inner.skipped_sourcemaps.is_empty() {
print_section_with_debugid(
f,
"Ignored: The following sourcemap files already have debug ids",
&self.skipped_sourcemaps,
&self.inner.skipped_sourcemaps,
self.sourcefiles,
)?;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $ sentry-cli sourcemaps inject --ext="complex.js" --dry-run tests/integration/_f
Source Map Debug ID Injection Report
Modified: [..]
[..] - tests/integration/_fixtures/inject_complex_extension/hello.complex.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for tests/integration/_fixtures/inject_complex_extension/hello.complex.js)


```
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ $ sentry-cli sourcemaps inject ./server ./static
Source Map Debug ID Injection Report
Modified: The following source files have been modified to have debug ids
[..]-[..]-[..]-[..]-[..] - ./server/app/page.min.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/app/page.min.js)
[..]-[..]-[..]-[..]-[..] - ./server/chunks/1.min.js
[..]-[..]-[..]-[..]-[..] - ./server/chunks/flight-server-css-manifest.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/chunks/flight-server-css-manifest.js)
[..]-[..]-[..]-[..]-[..] - ./server/flight-manifest.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/flight-manifest.js)
[..]-[..]-[..]-[..]-[..] - ./server/pages/_document.min.js
[..]-[..]-[..]-[..]-[..] - ./server/pages/api/hello.min.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/pages/api/hello.min.js)
[..]-[..]-[..]-[..]-[..] - ./static/chunks/575-bb7d7e0e6de8d623.min.js
[..]-[..]-[..]-[..]-[..] - ./static/chunks/app/client/layout-ba9c3036fc0dba78.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./static/chunks/app/client/layout-ba9c3036fc0dba78.js)
[..]-[..]-[..]-[..]-[..] - ./static/chunks/app/head-172ad45600676c06.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./static/chunks/app/head-172ad45600676c06.js)
[..]-[..]-[..]-[..]-[..] - ./static/chunks/pages/asdf-05b39167abbe433b.min.js
Modified: The following sourcemap files have been modified to have debug ids
[..]-[..]-[..]-[..]-[..] - ./server/pages/_document.js.map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ Source Map Debug ID Injection Report
[..]-[..]-[..]-[..]-[..] - ./server/app/page.js
[..]-[..]-[..]-[..]-[..] - ./server/chunks/1.js
[..]-[..]-[..]-[..]-[..] - ./server/chunks/flight-server-css-manifest.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/chunks/flight-server-css-manifest.js)
[..]-[..]-[..]-[..]-[..] - ./server/dummy_embedded.js
[..]-[..]-[..]-[..]-[..] - ./server/dummy_hosted.js
[..]-[..]-[..]-[..]-[..] - ./server/dummy_hosted_full.js
[..]-[..]-[..]-[..]-[..] - ./server/flight-manifest.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/flight-manifest.js)
[..]-[..]-[..]-[..]-[..] - ./server/pages/_document.js
[..]-[..]-[..]-[..]-[..] - ./server/pages/api/hello.js
[..]-[..]-[..]-[..]-[..] - ./static/chunks/575-bb7d7e0e6de8d623.js
[..]-[..]-[..]-[..]-[..] - ./static/chunks/app/client/layout-ba9c3036fc0dba78.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./static/chunks/app/client/layout-ba9c3036fc0dba78.js)
[..]-[..]-[..]-[..]-[..] - ./static/chunks/app/head-172ad45600676c06.js
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./static/chunks/app/head-172ad45600676c06.js)
[..]-[..]-[..]-[..]-[..] - ./static/chunks/pages/asdf-05b39167abbe433b.js
Modified: The following sourcemap files have been modified to have debug ids
[..]-[..]-[..]-[..]-[..] - ./server/dummy_hosted.js.map
Expand Down