@@ -2,10 +2,9 @@ import SentryCli from "@sentry/cli";
2
2
import {
3
3
closeSession ,
4
4
DEFAULT_ENVIRONMENT ,
5
- getDynamicSamplingContextFromSpan ,
5
+ getTraceData ,
6
6
makeSession ,
7
7
setMeasurement ,
8
- spanToTraceHeader ,
9
8
startSpan ,
10
9
} from "@sentry/core" ;
11
10
import * as dotenv from "dotenv" ;
@@ -23,7 +22,6 @@ import { Options, SentrySDKBuildFlags } from "./types";
23
22
import { arrayify , getTurborepoEnvPassthroughWarning , stripQueryAndHashFromPath } from "./utils" ;
24
23
import { glob } from "glob" ;
25
24
import { defaultRewriteSourcesHook , prepareBundleForDebugIdUpload } from "./debug-id-upload" ;
26
- import { dynamicSamplingContextToSentryBaggageHeader } from "@sentry/utils" ;
27
25
28
26
export type SentryBuildPluginManager = {
29
27
/**
@@ -67,6 +65,15 @@ export type SentryBuildPluginManager = {
67
65
*/
68
66
createRelease ( ) : Promise < void > ;
69
67
68
+ /**
69
+ * Injects debug IDs into the build artifacts.
70
+ *
71
+ * This is a separate function from `uploadSourcemaps` because that needs to run before the sourcemaps are uploaded.
72
+ * Usually the respective bundler-plugin will take care of this before the sourcemaps are uploaded.
73
+ * Only use this if you need to manually inject debug IDs into the build artifacts.
74
+ */
75
+ injectDebugIds ( buildArtifactPaths : string [ ] ) : Promise < void > ;
76
+
70
77
/**
71
78
* Uploads sourcemaps using the "Debug ID" method. This function takes a list of build artifact paths that will be uploaded
72
79
*/
@@ -80,6 +87,20 @@ export type SentryBuildPluginManager = {
80
87
createDependencyOnBuildArtifacts : ( ) => ( ) => void ;
81
88
} ;
82
89
90
+ function createCliInstance ( options : NormalizedOptions ) : SentryCli {
91
+ return new SentryCli ( null , {
92
+ authToken : options . authToken ,
93
+ org : options . org ,
94
+ project : options . project ,
95
+ silent : options . silent ,
96
+ url : options . url ,
97
+ vcsRemote : options . release . vcsRemote ,
98
+ headers : {
99
+ ...getTraceData ( ) ,
100
+ } ,
101
+ } ) ;
102
+ }
103
+
83
104
/**
84
105
* Creates a build plugin manager that exposes primitives for everything that a Sentry JavaScript SDK or build tooling may do during a build.
85
106
*
@@ -153,6 +174,9 @@ export function createSentryBuildPluginManager(
153
174
createDependencyOnBuildArtifacts : ( ) => ( ) => {
154
175
/* noop */
155
176
} ,
177
+ injectDebugIds : async ( ) => {
178
+ /* noop */
179
+ } ,
156
180
} ;
157
181
}
158
182
@@ -424,15 +448,7 @@ export function createSentryBuildPluginManager(
424
448
createDependencyOnBuildArtifacts ( ) ;
425
449
426
450
try {
427
- const cliInstance = new SentryCli ( null , {
428
- authToken : options . authToken ,
429
- org : options . org ,
430
- project : options . project ,
431
- silent : options . silent ,
432
- url : options . url ,
433
- vcsRemote : options . release . vcsRemote ,
434
- headers : options . headers ,
435
- } ) ;
451
+ const cliInstance = createCliInstance ( options ) ;
436
452
437
453
if ( options . release . create ) {
438
454
await cliInstance . releases . new ( options . release . name ) ;
@@ -502,6 +518,33 @@ export function createSentryBuildPluginManager(
502
518
}
503
519
} ,
504
520
521
+ /*
522
+ Injects debug IDs into the build artifacts.
523
+
524
+ This is a separate function from `uploadSourcemaps` because that needs to run before the sourcemaps are uploaded.
525
+ Usually the respective bundler-plugin will take care of this before the sourcemaps are uploaded.
526
+ Only use this if you need to manually inject debug IDs into the build artifacts.
527
+ */
528
+ async injectDebugIds ( buildArtifactPaths : string [ ] ) {
529
+ await startSpan (
530
+ { name : "inject-debug-ids" , scope : sentryScope , forceTransaction : true } ,
531
+ async ( ) => {
532
+ try {
533
+ const cliInstance = createCliInstance ( options ) ;
534
+ await cliInstance . execute (
535
+ [ "sourcemaps" , "inject" , ...buildArtifactPaths ] ,
536
+ options . debug ?? false
537
+ ) ;
538
+ } catch ( e ) {
539
+ sentryScope . captureException ( 'Error in "debugIdInjectionPlugin" writeBundle hook' ) ;
540
+ handleRecoverableError ( e , false ) ;
541
+ } finally {
542
+ await safeFlushTelemetry ( sentryClient ) ;
543
+ }
544
+ }
545
+ ) ;
546
+ } ,
547
+
505
548
/**
506
549
* Uploads sourcemaps using the "Debug ID" method. This function takes a list of build artifact paths that will be uploaded
507
550
*/
@@ -617,23 +660,8 @@ export function createSentryBuildPluginManager(
617
660
setMeasurement ( "files" , files . length , "none" , prepBundlesSpan ) ;
618
661
setMeasurement ( "upload_size" , uploadSize , "byte" , prepBundlesSpan ) ;
619
662
620
- await startSpan ( { name : "upload" , scope : sentryScope } , async ( uploadSpan ) => {
621
- const cliInstance = new SentryCli ( null , {
622
- authToken : options . authToken ,
623
- org : options . org ,
624
- project : options . project ,
625
- silent : options . silent ,
626
- url : options . url ,
627
- vcsRemote : options . release . vcsRemote ,
628
- headers : {
629
- "sentry-trace" : spanToTraceHeader ( uploadSpan ) ,
630
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
631
- baggage : dynamicSamplingContextToSentryBaggageHeader (
632
- getDynamicSamplingContextFromSpan ( uploadSpan )
633
- ) ! ,
634
- ...options . headers ,
635
- } ,
636
- } ) ;
663
+ await startSpan ( { name : "upload" , scope : sentryScope } , async ( ) => {
664
+ const cliInstance = createCliInstance ( options ) ;
637
665
638
666
await cliInstance . releases . uploadSourceMaps (
639
667
options . release . name ?? "undefined" , // unfortunately this needs a value for now but it will not matter since debug IDs overpower releases anyhow
0 commit comments