Skip to content

Commit f07b24e

Browse files
fix(sourcemaps): Display injection errors (#2775)
We have been generating these error messages, but we never print them, even though it seems like we probably mean to be printing them. Fixes #2774 Fixes [CLI-170](https://linear.app/getsentry/issue/CLI-170/sourcemap-injection-errors-never-displayed)
1 parent 6889a79 commit f07b24e

File tree

5 files changed

+100
-32
lines changed

5 files changed

+100
-32
lines changed

src/utils/sourcemaps.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::utils::file_upload::{
2727
};
2828
use crate::utils::logging::is_quiet_mode;
2929
use crate::utils::progress::ProgressBar;
30-
use crate::utils::sourcemaps::inject::InjectReport;
30+
use crate::utils::sourcemaps::inject::{InjectReportBuilder, ReportItem};
3131

3232
pub mod inject;
3333

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

804-
let mut report = InjectReport::default();
804+
let mut report_builder = InjectReportBuilder::default();
805805

806806
let mut sourcemaps = self
807807
.sources
@@ -821,9 +821,11 @@ impl SourceMapProcessor {
821821
}
822822

823823
if let Some(debug_id) = self.debug_ids.get(source_url) {
824-
report
825-
.previously_injected
826-
.push((source_url.into(), *debug_id));
824+
report_builder.previously_injected.push(ReportItem::new(
825+
source_url.into(),
826+
source_url.clone(),
827+
*debug_id,
828+
));
827829
continue;
828830
}
829831

@@ -1017,13 +1019,17 @@ impl SourceMapProcessor {
10171019
}
10181020

10191021
if debug_id_fresh {
1020-
report
1021-
.sourcemaps
1022-
.push((sourcemap_file.path.clone(), debug_id));
1022+
report_builder.sourcemaps.push(ReportItem::new(
1023+
sourcemap_file.path.clone(),
1024+
sourcemap_file.url.clone(),
1025+
debug_id,
1026+
));
10231027
} else {
1024-
report
1025-
.skipped_sourcemaps
1026-
.push((sourcemap_file.path.clone(), debug_id));
1028+
report_builder.skipped_sourcemaps.push(ReportItem::new(
1029+
sourcemap_file.path.clone(),
1030+
sourcemap_file.url.clone(),
1031+
debug_id,
1032+
));
10271033
}
10281034

10291035
debug_id
@@ -1065,11 +1071,15 @@ impl SourceMapProcessor {
10651071
))?;
10661072
}
10671073

1068-
report.injected.push((source_file.path.clone(), debug_id));
1074+
report_builder.injected.push(ReportItem::new(
1075+
source_file.path.clone(),
1076+
source_file.url.clone(),
1077+
debug_id,
1078+
));
10691079
}
10701080

1071-
if !report.is_empty() {
1072-
println!("{report}");
1081+
if !report_builder.is_empty() {
1082+
println!("{}", report_builder.into_report(&self.sources));
10731083
} else {
10741084
println!("> Nothing to inject")
10751085
}

src/utils/sourcemaps/inject.rs

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use magic_string::{GenerateDecodedMapOptions, MagicString};
1515
use sentry::types::DebugId;
1616
use sourcemap::SourceMap;
1717

18+
use crate::utils::file_upload::SourceFiles;
19+
1820
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){}}();"#;
1921
const DEBUGID_PLACEHOLDER: &str = "__SENTRY_DEBUG_ID__";
2022
const DEBUGID_COMMENT_PREFIX: &str = "//# debugId";
@@ -41,11 +43,19 @@ static USE_DIRECTIVE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
4143
fn print_section_with_debugid(
4244
f: &mut fmt::Formatter<'_>,
4345
title: &str,
44-
data: &[(PathBuf, DebugId)],
46+
data: &[ReportItem],
47+
sourcefiles: &SourceFiles,
4548
) -> fmt::Result {
4649
print_section_title(f, title)?;
47-
for (path, debug_id) in data.iter().sorted_by_key(|x| &x.0) {
48-
writeln!(f, " {debug_id} - {}", path.display())?;
50+
for item in data.iter().sorted_by_key(|x| &x.path) {
51+
writeln!(f, " {} - {}", item.debug_id, item.path.display())?;
52+
for (level, message) in sourcefiles
53+
.get(&item.url)
54+
.iter()
55+
.flat_map(|sourcefile| &sourcefile.messages)
56+
{
57+
writeln!(f, " - {}: {message}", style(&level).red())?;
58+
}
4959
}
5060
Ok(())
5161
}
@@ -54,60 +64,97 @@ fn print_section_title(f: &mut fmt::Formatter<'_>, title: &str) -> fmt::Result {
5464
writeln!(f, " {}", style(title).yellow().bold())
5565
}
5666

67+
#[derive(Debug, Clone)]
68+
pub struct ReportItem {
69+
path: PathBuf,
70+
url: String,
71+
debug_id: DebugId,
72+
}
73+
74+
/// Report for the inject operation, including any warnings or errors from the sources.
75+
pub struct InjectReport<'a> {
76+
inner: InjectReportBuilder,
77+
sourcefiles: &'a SourceFiles,
78+
}
79+
80+
/// Builder for the inject report.
81+
/// To print the report, we need to add the sources to the report,
82+
/// as we pull any warnings or errors from there.
5783
#[derive(Debug, Clone, Default)]
58-
pub struct InjectReport {
59-
pub injected: Vec<(PathBuf, DebugId)>,
60-
pub previously_injected: Vec<(PathBuf, DebugId)>,
61-
pub sourcemaps: Vec<(PathBuf, DebugId)>,
62-
pub skipped_sourcemaps: Vec<(PathBuf, DebugId)>,
84+
pub struct InjectReportBuilder {
85+
pub injected: Vec<ReportItem>,
86+
pub previously_injected: Vec<ReportItem>,
87+
pub sourcemaps: Vec<ReportItem>,
88+
pub skipped_sourcemaps: Vec<ReportItem>,
6389
}
6490

65-
impl InjectReport {
91+
impl ReportItem {
92+
pub fn new(path: PathBuf, url: String, debug_id: DebugId) -> Self {
93+
Self {
94+
path,
95+
url,
96+
debug_id,
97+
}
98+
}
99+
}
100+
101+
impl InjectReportBuilder {
66102
pub fn is_empty(&self) -> bool {
67103
self.injected.is_empty()
68104
&& self.previously_injected.is_empty()
69105
&& self.sourcemaps.is_empty()
70106
&& self.skipped_sourcemaps.is_empty()
71107
}
108+
109+
pub fn into_report<'a>(self, sourcefiles: &'a SourceFiles) -> InjectReport<'a> {
110+
InjectReport {
111+
inner: self,
112+
sourcefiles,
113+
}
114+
}
72115
}
73116

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

82-
if !self.injected.is_empty() {
125+
if !self.inner.injected.is_empty() {
83126
print_section_with_debugid(
84127
f,
85128
"Modified: The following source files have been modified to have debug ids",
86-
&self.injected,
129+
&self.inner.injected,
130+
self.sourcefiles,
87131
)?;
88132
}
89133

90-
if !self.sourcemaps.is_empty() {
134+
if !self.inner.sourcemaps.is_empty() {
91135
print_section_with_debugid(
92136
f,
93137
"Modified: The following sourcemap files have been modified to have debug ids",
94-
&self.sourcemaps,
138+
&self.inner.sourcemaps,
139+
self.sourcefiles,
95140
)?;
96141
}
97142

98-
if !self.previously_injected.is_empty() {
143+
if !self.inner.previously_injected.is_empty() {
99144
print_section_with_debugid(
100145
f,
101146
"Ignored: The following source files already have debug ids",
102-
&self.previously_injected,
147+
&self.inner.previously_injected,
148+
self.sourcefiles,
103149
)?;
104150
}
105151

106-
if !self.skipped_sourcemaps.is_empty() {
152+
if !self.inner.skipped_sourcemaps.is_empty() {
107153
print_section_with_debugid(
108154
f,
109155
"Ignored: The following sourcemap files already have debug ids",
110-
&self.skipped_sourcemaps,
156+
&self.inner.skipped_sourcemaps,
157+
self.sourcefiles,
111158
)?;
112159
}
113160

tests/integration/_cases/sourcemaps/sourcemaps-inject-complex-extension.trycmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ $ sentry-cli sourcemaps inject --ext="complex.js" --dry-run tests/integration/_f
55
Source Map Debug ID Injection Report
66
Modified: [..]
77
[..] - tests/integration/_fixtures/inject_complex_extension/hello.complex.js
8+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for tests/integration/_fixtures/inject_complex_extension/hello.complex.js)
89

910

1011
```

tests/integration/_cases/sourcemaps/sourcemaps-inject-nomappings.trycmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ $ sentry-cli sourcemaps inject ./server ./static
1111
Source Map Debug ID Injection Report
1212
Modified: The following source files have been modified to have debug ids
1313
[..]-[..]-[..]-[..]-[..] - ./server/app/page.min.js
14+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/app/page.min.js)
1415
[..]-[..]-[..]-[..]-[..] - ./server/chunks/1.min.js
1516
[..]-[..]-[..]-[..]-[..] - ./server/chunks/flight-server-css-manifest.js
17+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/chunks/flight-server-css-manifest.js)
1618
[..]-[..]-[..]-[..]-[..] - ./server/flight-manifest.js
19+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/flight-manifest.js)
1720
[..]-[..]-[..]-[..]-[..] - ./server/pages/_document.min.js
1821
[..]-[..]-[..]-[..]-[..] - ./server/pages/api/hello.min.js
22+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/pages/api/hello.min.js)
1923
[..]-[..]-[..]-[..]-[..] - ./static/chunks/575-bb7d7e0e6de8d623.min.js
2024
[..]-[..]-[..]-[..]-[..] - ./static/chunks/app/client/layout-ba9c3036fc0dba78.js
25+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./static/chunks/app/client/layout-ba9c3036fc0dba78.js)
2126
[..]-[..]-[..]-[..]-[..] - ./static/chunks/app/head-172ad45600676c06.js
27+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./static/chunks/app/head-172ad45600676c06.js)
2228
[..]-[..]-[..]-[..]-[..] - ./static/chunks/pages/asdf-05b39167abbe433b.min.js
2329
Modified: The following sourcemap files have been modified to have debug ids
2430
[..]-[..]-[..]-[..]-[..] - ./server/pages/_document.js.map

tests/integration/_cases/sourcemaps/sourcemaps-inject.trycmd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ Source Map Debug ID Injection Report
1313
[..]-[..]-[..]-[..]-[..] - ./server/app/page.js
1414
[..]-[..]-[..]-[..]-[..] - ./server/chunks/1.js
1515
[..]-[..]-[..]-[..]-[..] - ./server/chunks/flight-server-css-manifest.js
16+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/chunks/flight-server-css-manifest.js)
1617
[..]-[..]-[..]-[..]-[..] - ./server/dummy_embedded.js
1718
[..]-[..]-[..]-[..]-[..] - ./server/dummy_hosted.js
1819
[..]-[..]-[..]-[..]-[..] - ./server/dummy_hosted_full.js
1920
[..]-[..]-[..]-[..]-[..] - ./server/flight-manifest.js
21+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./server/flight-manifest.js)
2022
[..]-[..]-[..]-[..]-[..] - ./server/pages/_document.js
2123
[..]-[..]-[..]-[..]-[..] - ./server/pages/api/hello.js
2224
[..]-[..]-[..]-[..]-[..] - ./static/chunks/575-bb7d7e0e6de8d623.js
2325
[..]-[..]-[..]-[..]-[..] - ./static/chunks/app/client/layout-ba9c3036fc0dba78.js
26+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./static/chunks/app/client/layout-ba9c3036fc0dba78.js)
2427
[..]-[..]-[..]-[..]-[..] - ./static/chunks/app/head-172ad45600676c06.js
28+
- warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ./static/chunks/app/head-172ad45600676c06.js)
2529
[..]-[..]-[..]-[..]-[..] - ./static/chunks/pages/asdf-05b39167abbe433b.js
2630
Modified: The following sourcemap files have been modified to have debug ids
2731
[..]-[..]-[..]-[..]-[..] - ./server/dummy_hosted.js.map

0 commit comments

Comments
 (0)