-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathbuild-plugin-manager.ts
More file actions
849 lines (770 loc) · 32.4 KB
/
build-plugin-manager.ts
File metadata and controls
849 lines (770 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
import SentryCli from "@sentry/cli";
import {
closeSession,
DEFAULT_ENVIRONMENT,
getTraceData,
makeSession,
setMeasurement,
startSpan,
} from "@sentry/core";
import * as dotenv from "dotenv";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { NormalizedOptions, normalizeUserOptions, validateOptions } from "./options-mapping";
import { createLogger, Logger } from "./logger";
import {
allowedToSendTelemetry,
createSentryInstance,
safeFlushTelemetry,
} from "./sentry/telemetry";
import { Options, SentrySDKBuildFlags } from "./types";
import { arrayify, getTurborepoEnvPassthroughWarning, stripQueryAndHashFromPath } from "./utils";
import { glob } from "glob";
import { defaultRewriteSourcesHook, prepareBundleForDebugIdUpload } from "./debug-id-upload";
export type SentryBuildPluginManager = {
/**
* A logger instance that takes the options passed to the build plugin manager into account. (for silencing and log level etc.)
*/
logger: Logger;
/**
* Options after normalization. Includes things like the inferred release name.
*/
normalizedOptions: NormalizedOptions;
/**
* Magic strings and their replacement values that can be used for bundle size optimizations. This already takes
* into account the options passed to the build plugin manager.
*/
bundleSizeOptimizationReplacementValues: SentrySDKBuildFlags;
/**
* Metadata that should be injected into bundles if possible. Takes into account options passed to the build plugin manager.
*/
// See `generateModuleMetadataInjectorCode` for how this should be used exactly
bundleMetadata: Record<string, unknown>;
/**
* Contains utility functions for emitting telemetry via the build plugin manager.
*/
telemetry: {
/**
* Emits a `Sentry Bundler Plugin execution` signal.
*/
emitBundlerPluginExecutionSignal(): Promise<void>;
};
/**
* Will potentially create a release based on the build plugin manager options.
*
* Also
* - finalizes the release
* - sets commits
* - uploads legacy sourcemaps
* - adds deploy information
*/
createRelease(): Promise<void>;
/**
* Injects debug IDs into the build artifacts.
*
* This is a separate function from `uploadSourcemaps` because that needs to run before the sourcemaps are uploaded.
* Usually the respective bundler-plugin will take care of this before the sourcemaps are uploaded.
* Only use this if you need to manually inject debug IDs into the build artifacts.
*/
injectDebugIds(buildArtifactPaths: string[]): Promise<void>;
/**
* Uploads sourcemaps using the "Debug ID" method. This function takes a list of build artifact paths that will be uploaded
*/
uploadSourcemaps(
buildArtifactPaths: string[],
opts?: { prepareArtifacts?: boolean }
): Promise<void>;
/**
* Will delete artifacts based on the passed `sourcemaps.filesToDeleteAfterUpload` option.
*/
deleteArtifacts(): Promise<void>;
createDependencyOnBuildArtifacts: () => () => void;
};
function createCliInstance(options: NormalizedOptions): SentryCli {
return new SentryCli(null, {
authToken: options.authToken,
org: options.org,
project: options.project,
silent: options.silent,
url: options.url,
vcsRemote: options.release.vcsRemote,
headers: {
...getTraceData(),
...options.headers,
},
});
}
/**
* Creates a build plugin manager that exposes primitives for everything that a Sentry JavaScript SDK or build tooling may do during a build.
*
* The build plugin manager's behavior strongly depends on the options that are passed in.
*/
export function createSentryBuildPluginManager(
userOptions: Options,
bundlerPluginMetaContext: {
/**
* E.g. `webpack` or `nextjs` or `turbopack`
*/
buildTool: string;
/**
* E.g. `[sentry-webpack-plugin]` or `[@sentry/nextjs]`
*/
loggerPrefix: string;
}
): SentryBuildPluginManager {
const logger = createLogger({
prefix: bundlerPluginMetaContext.loggerPrefix,
silent: userOptions.silent ?? false,
debug: userOptions.debug ?? false,
});
try {
const dotenvFile = fs.readFileSync(
path.join(process.cwd(), ".env.sentry-build-plugin"),
"utf-8"
);
// NOTE: Do not use the dotenv.config API directly to read the dotenv file! For some ungodly reason, it falls back to reading `${process.cwd()}/.env` which is absolutely not what we want.
const dotenvResult = dotenv.parse(dotenvFile);
// Vite has a bug/behaviour where spreading into process.env will cause it to crash
// https://github.com/vitest-dev/vitest/issues/1870#issuecomment-1501140251
Object.assign(process.env, dotenvResult);
logger.info('Using environment variables configured in ".env.sentry-build-plugin".');
} catch (e: unknown) {
// Ignore "file not found" errors but throw all others
if (typeof e === "object" && e && "code" in e && e.code !== "ENOENT") {
throw e;
}
}
const options = normalizeUserOptions(userOptions);
if (options.disable) {
// Early-return a noop build plugin manager instance so that we
// don't continue validating options, setting up Sentry, etc.
// Otherwise we might create side-effects or log messages that
// users don't expect from a disabled plugin.
return {
normalizedOptions: options,
logger,
bundleSizeOptimizationReplacementValues: {},
telemetry: {
emitBundlerPluginExecutionSignal: async () => {
/* noop */
},
},
bundleMetadata: {},
createRelease: async () => {
/* noop */
},
uploadSourcemaps: async () => {
/* noop */
},
deleteArtifacts: async () => {
/* noop */
},
createDependencyOnBuildArtifacts: () => () => {
/* noop */
},
injectDebugIds: async () => {
/* noop */
},
};
}
const shouldSendTelemetry = allowedToSendTelemetry(options);
const { sentryScope, sentryClient } = createSentryInstance(
options,
shouldSendTelemetry,
bundlerPluginMetaContext.buildTool
);
const { release, environment = DEFAULT_ENVIRONMENT } = sentryClient.getOptions();
const sentrySession = makeSession({ release, environment });
sentryScope.setSession(sentrySession);
// Send the start of the session
sentryClient.captureSession(sentrySession);
let sessionHasEnded = false; // Just to prevent infinite loops with beforeExit, which is called whenever the event loop empties out
function endSession(): void {
if (sessionHasEnded) {
return;
}
closeSession(sentrySession);
sentryClient.captureSession(sentrySession);
sessionHasEnded = true;
}
// We also need to manually end sessions on errors because beforeExit is not called on crashes
process.on("beforeExit", () => {
endSession();
});
// Set the User-Agent that Sentry CLI will use when interacting with Sentry
process.env[
"SENTRY_PIPELINE"
] = `${bundlerPluginMetaContext.buildTool}-plugin/${__PACKAGE_VERSION__}`;
// Propagate debug flag to Sentry CLI via environment variable
if (options.debug) {
process.env["SENTRY_LOG_LEVEL"] = "debug";
}
// Not a bulletproof check but should be good enough to at least sometimes determine
// if the plugin is called in dev/watch mode or for a prod build. The important part
// here is to avoid a false positive. False negatives are okay.
const isDevMode = process.env["NODE_ENV"] === "development";
/**
* Handles errors caught and emitted in various areas of the plugin.
*
* Also sets the sentry session status according to the error handling.
*
* If users specify their custom `errorHandler` we'll leave the decision to throw
* or continue up to them. By default, @param throwByDefault controls if the plugin
* should throw an error (which causes a build fail in most bundlers) or continue.
*/
function handleRecoverableError(unknownError: unknown, throwByDefault: boolean): void {
sentrySession.status = "abnormal";
try {
if (options.errorHandler) {
try {
if (unknownError instanceof Error) {
options.errorHandler(unknownError);
} else {
options.errorHandler(new Error("An unknown error occurred"));
}
} catch (e) {
sentrySession.status = "crashed";
throw e;
}
} else {
// setting the session to "crashed" b/c from a plugin perspective this run failed.
// However, we're intentionally not rethrowing the error to avoid breaking the user build.
sentrySession.status = "crashed";
if (throwByDefault) {
throw unknownError;
}
logger.error("An error occurred. Couldn't finish all operations:", unknownError);
}
} finally {
endSession();
}
}
if (!validateOptions(options, logger)) {
// Throwing by default to avoid a misconfigured plugin going unnoticed.
handleRecoverableError(
new Error("Options were not set correctly. See output above for more details."),
true
);
}
// We have multiple plugins depending on generated source map files. (debug ID upload, legacy upload)
// Additionally, we also want to have the functionality to delete files after uploading sourcemaps.
// All of these plugins and the delete functionality need to run in the same hook (`writeBundle`).
// Since the plugins among themselves are not aware of when they run and finish, we need a system to
// track their dependencies on the generated files, so that we can initiate the file deletion only after
// nothing depends on the files anymore.
const dependenciesOnBuildArtifacts = new Set<symbol>();
const buildArtifactsDependencySubscribers: (() => void)[] = [];
function notifyBuildArtifactDependencySubscribers(): void {
buildArtifactsDependencySubscribers.forEach((subscriber) => {
subscriber();
});
}
function createDependencyOnBuildArtifacts(): () => void {
const dependencyIdentifier = Symbol();
dependenciesOnBuildArtifacts.add(dependencyIdentifier);
return function freeDependencyOnBuildArtifacts() {
dependenciesOnBuildArtifacts.delete(dependencyIdentifier);
notifyBuildArtifactDependencySubscribers();
};
}
/**
* Returns a Promise that resolves when all the currently active dependencies are freed again.
*
* It is very important that this function is called as late as possible before wanting to await the Promise to give
* the dependency producers as much time as possible to register themselves.
*/
function waitUntilBuildArtifactDependenciesAreFreed(): Promise<void> {
return new Promise<void>((resolve) => {
buildArtifactsDependencySubscribers.push(() => {
if (dependenciesOnBuildArtifacts.size === 0) {
resolve();
}
});
if (dependenciesOnBuildArtifacts.size === 0) {
resolve();
}
});
}
const bundleSizeOptimizationReplacementValues: SentrySDKBuildFlags = {};
if (options.bundleSizeOptimizations) {
const { bundleSizeOptimizations } = options;
if (bundleSizeOptimizations.excludeDebugStatements) {
bundleSizeOptimizationReplacementValues["__SENTRY_DEBUG__"] = false;
}
if (bundleSizeOptimizations.excludeTracing) {
bundleSizeOptimizationReplacementValues["__SENTRY_TRACING__"] = false;
}
if (bundleSizeOptimizations.excludeReplayCanvas) {
bundleSizeOptimizationReplacementValues["__RRWEB_EXCLUDE_CANVAS__"] = true;
}
if (bundleSizeOptimizations.excludeReplayIframe) {
bundleSizeOptimizationReplacementValues["__RRWEB_EXCLUDE_IFRAME__"] = true;
}
if (bundleSizeOptimizations.excludeReplayShadowDom) {
bundleSizeOptimizationReplacementValues["__RRWEB_EXCLUDE_SHADOW_DOM__"] = true;
}
if (bundleSizeOptimizations.excludeReplayWorker) {
bundleSizeOptimizationReplacementValues["__SENTRY_EXCLUDE_REPLAY_WORKER__"] = true;
}
}
let bundleMetadata: Record<string, unknown> = {};
if (options.moduleMetadata || options.applicationKey) {
if (options.applicationKey) {
// We use different keys so that if user-code receives multiple bundling passes, we will store the application keys of all the passes.
// It is a bit unfortunate that we have to inject the metadata snippet at the top, because after multiple
// injections, the first injection will always "win" because it comes last in the code. We would generally be
// fine with making the last bundling pass win. But because it cannot win, we have to use a workaround of storing
// the app keys in different object keys.
// We can simply use the `_sentryBundlerPluginAppKey:` to filter for app keys in the SDK.
bundleMetadata[`_sentryBundlerPluginAppKey:${options.applicationKey}`] = true;
}
if (typeof options.moduleMetadata === "function") {
const args = {
org: options.org,
project: options.project,
release: options.release.name,
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
bundleMetadata = { ...bundleMetadata, ...options.moduleMetadata(args) };
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
bundleMetadata = { ...bundleMetadata, ...options.moduleMetadata };
}
}
return {
/**
* A logger instance that takes the options passed to the build plugin manager into account. (for silencing and log level etc.)
*/
logger,
/**
* Options after normalization. Includes things like the inferred release name.
*/
normalizedOptions: options,
/**
* Magic strings and their replacement values that can be used for bundle size optimizations. This already takes
* into account the options passed to the build plugin manager.
*/
bundleSizeOptimizationReplacementValues,
/**
* Metadata that should be injected into bundles if possible. Takes into account options passed to the build plugin manager.
*/
// See `generateModuleMetadataInjectorCode` for how this should be used exactly
bundleMetadata,
/**
* Contains utility functions for emitting telemetry via the build plugin manager.
*/
telemetry: {
/**
* Emits a `Sentry Bundler Plugin execution` signal.
*/
async emitBundlerPluginExecutionSignal() {
if (await shouldSendTelemetry) {
logger.info(
"Sending telemetry data on issues and performance to Sentry. To disable telemetry, set `options.telemetry` to `false`."
);
startSpan({ name: "Sentry Bundler Plugin execution", scope: sentryScope }, () => {
//
});
await safeFlushTelemetry(sentryClient);
}
},
},
/**
* Will potentially create a release based on the build plugin manager options.
*
* Also
* - finalizes the release
* - sets commits
* - uploads legacy sourcemaps
* - adds deploy information
*/
async createRelease() {
if (!options.release.name) {
logger.debug(
"No release name provided. Will not create release. Please set the `release.name` option to identify your release."
);
return;
} else if (isDevMode) {
logger.debug("Running in development mode. Will not create release.");
return;
} else if (!options.authToken) {
logger.warn(
"No auth token provided. Will not create release. Please set the `authToken` option. You can find information on how to generate a Sentry auth token here: https://docs.sentry.io/api/auth/" +
getTurborepoEnvPassthroughWarning("SENTRY_AUTH_TOKEN")
);
return;
} else if (!options.org && !options.authToken.startsWith("sntrys_")) {
logger.warn(
"No organization slug provided. Will not create release. Please set the `org` option to your Sentry organization slug." +
getTurborepoEnvPassthroughWarning("SENTRY_ORG")
);
return;
} else if (!options.project) {
logger.warn(
"No project provided. Will not create release. Please set the `project` option to your Sentry project slug." +
getTurborepoEnvPassthroughWarning("SENTRY_PROJECT")
);
return;
}
// It is possible that this writeBundle hook is called multiple times in one build (for example when reusing the plugin, or when using build tooling like `@vitejs/plugin-legacy`)
// Therefore we need to actually register the execution of this hook as dependency on the sourcemap files.
const freeWriteBundleInvocationDependencyOnSourcemapFiles =
createDependencyOnBuildArtifacts();
try {
const cliInstance = createCliInstance(options);
if (options.release.create) {
await cliInstance.releases.new(options.release.name);
}
if (options.release.uploadLegacySourcemaps) {
const normalizedInclude = arrayify(options.release.uploadLegacySourcemaps)
.map((includeItem) =>
typeof includeItem === "string" ? { paths: [includeItem] } : includeItem
)
.map((includeEntry) => ({
...includeEntry,
validate: includeEntry.validate ?? false,
ext: includeEntry.ext
? includeEntry.ext.map((extension) => `.${extension.replace(/^\./, "")}`)
: [".js", ".map", ".jsbundle", ".bundle"],
ignore: includeEntry.ignore ? arrayify(includeEntry.ignore) : undefined,
}));
await cliInstance.releases.uploadSourceMaps(options.release.name, {
include: normalizedInclude,
dist: options.release.dist,
// We want this promise to throw if the sourcemaps fail to upload so that we know about it.
// see: https://github.com/getsentry/sentry-cli/pull/2605
live: "rejectOnError",
});
}
if (options.release.setCommits !== false) {
try {
await cliInstance.releases.setCommits(
options.release.name,
// set commits always exists due to the normalize function
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
options.release.setCommits!
);
} catch (e) {
// shouldNotThrowOnFailure being present means that the plugin defaulted to `{ auto: true }` for the setCommitsOptions, meaning that wee should not throw when CLI throws because there is no repo
if (
options.release.setCommits &&
"shouldNotThrowOnFailure" in options.release.setCommits &&
options.release.setCommits.shouldNotThrowOnFailure
) {
logger.debug(
"An error occurred setting commits on release (this message can be ignored unless you commits on release are desired):",
e
);
} else {
throw e;
}
}
}
if (options.release.finalize) {
await cliInstance.releases.finalize(options.release.name);
}
if (options.release.deploy) {
await cliInstance.releases.newDeploy(options.release.name, options.release.deploy);
}
} catch (e) {
sentryScope.captureException('Error in "releaseManagementPlugin" writeBundle hook');
await safeFlushTelemetry(sentryClient);
handleRecoverableError(e, false);
} finally {
freeWriteBundleInvocationDependencyOnSourcemapFiles();
}
},
/*
Injects debug IDs into the build artifacts.
This is a separate function from `uploadSourcemaps` because that needs to run before the sourcemaps are uploaded.
Usually the respective bundler-plugin will take care of this before the sourcemaps are uploaded.
Only use this if you need to manually inject debug IDs into the build artifacts.
*/
async injectDebugIds(buildArtifactPaths: string[]) {
await startSpan(
{ name: "inject-debug-ids", scope: sentryScope, forceTransaction: true },
async () => {
try {
const cliInstance = createCliInstance(options);
await cliInstance.execute(
["sourcemaps", "inject", ...buildArtifactPaths],
options.debug ?? false
);
} catch (e) {
sentryScope.captureException('Error in "debugIdInjectionPlugin" writeBundle hook');
handleRecoverableError(e, false);
} finally {
await safeFlushTelemetry(sentryClient);
}
}
);
},
/**
* Uploads sourcemaps using the "Debug ID" method.
*
* By default, this prepares bundles in a temporary folder before uploading. You can opt into an
* in-place, direct upload path by setting `prepareArtifacts` to `false`. If `prepareArtifacts` is set to
* `false`, no preparation (e.g. adding `//# debugId=...` and writing adjusted source maps) is performed and no temp folder is used.
*
* @param buildArtifactPaths - The paths of the build artifacts to upload
* @param opts - Optional flags to control temp folder usage and preparation
*/
async uploadSourcemaps(buildArtifactPaths: string[], opts?: { prepareArtifacts?: boolean }) {
if (!canUploadSourceMaps(options, logger, isDevMode)) {
return;
}
// Early exit if assets is explicitly set to an empty array
const assets = options.sourcemaps?.assets;
if (Array.isArray(assets) && assets.length === 0) {
logger.debug(
"Empty `sourcemaps.assets` option provided. Will not upload sourcemaps with debug ID."
);
return;
}
await startSpan(
// This is `forceTransaction`ed because this span is used in dashboards in the form of indexed transactions.
{ name: "debug-id-sourcemap-upload", scope: sentryScope, forceTransaction: true },
async () => {
// If we're not using a temp folder, we must not prepare artifacts in-place (to avoid mutating user files)
const shouldPrepare = opts?.prepareArtifacts ?? true;
let folderToCleanUp: string | undefined;
// It is possible that this writeBundle hook (which calls this function) is called multiple times in one build (for example when reusing the plugin, or when using build tooling like `@vitejs/plugin-legacy`)
// Therefore we need to actually register the execution of this hook as dependency on the sourcemap files.
const freeUploadDependencyOnBuildArtifacts = createDependencyOnBuildArtifacts();
try {
if (!shouldPrepare) {
// Direct CLI upload from existing artifact paths (no globbing, no preparation)
let pathsToUpload: string[];
if (assets) {
pathsToUpload = Array.isArray(assets) ? assets : [assets];
logger.debug(
`Direct upload mode: passing user-provided assets directly to CLI: ${pathsToUpload.join(
", "
)}`
);
} else {
// Use original paths e.g. like ['.next/server'] directly –> preferred way when no globbing is done
pathsToUpload = buildArtifactPaths;
}
const ignorePaths = options.sourcemaps?.ignore
? Array.isArray(options.sourcemaps?.ignore)
? options.sourcemaps?.ignore
: [options.sourcemaps?.ignore]
: [];
await startSpan({ name: "upload", scope: sentryScope }, async () => {
const cliInstance = createCliInstance(options);
await cliInstance.releases.uploadSourceMaps(options.release.name ?? "undefined", {
include: [
{
paths: pathsToUpload,
rewrite: true,
dist: options.release.dist,
},
],
ignore: ignorePaths,
live: "rejectOnError",
});
});
logger.info("Successfully uploaded source maps to Sentry");
} else {
// Prepare artifacts in temp folder before uploading
let globAssets: string | string[];
if (assets) {
globAssets = assets;
} else {
logger.debug(
"No `sourcemaps.assets` option provided, falling back to uploading detected build artifacts."
);
globAssets = buildArtifactPaths;
}
const globResult = await startSpan(
{ name: "glob", scope: sentryScope },
async () =>
await glob(globAssets, {
absolute: true,
nodir: true, // We need individual files for preparation
ignore: options.sourcemaps?.ignore,
})
);
const debugIdChunkFilePaths = globResult.filter((debugIdChunkFilePath) => {
return !!stripQueryAndHashFromPath(debugIdChunkFilePath).match(/\.(js|mjs|cjs)$/);
});
// The order of the files output by glob() is not deterministic
// Ensure order within the files so that {debug-id}-{chunkIndex} coupling is consistent
debugIdChunkFilePaths.sort();
if (debugIdChunkFilePaths.length === 0) {
logger.warn(
"Didn't find any matching sources for debug ID upload. Please check the `sourcemaps.assets` option."
);
} else {
const tmpUploadFolder = await startSpan(
{ name: "mkdtemp", scope: sentryScope },
async () => {
return (
process.env?.["SENTRY_TEST_OVERRIDE_TEMP_DIR"] ||
(await fs.promises.mkdtemp(
path.join(os.tmpdir(), "sentry-bundler-plugin-upload-")
))
);
}
);
folderToCleanUp = tmpUploadFolder;
// Prepare into temp folder, then upload
await startSpan(
{ name: "prepare-bundles", scope: sentryScope },
async (prepBundlesSpan) => {
// Preparing the bundles can be a lot of work and doing it all at once has the potential of nuking the heap so
// instead we do it with a maximum of 16 concurrent workers
const preparationTasks = debugIdChunkFilePaths.map(
(chunkFilePath, chunkIndex) => async () => {
await prepareBundleForDebugIdUpload(
chunkFilePath,
tmpUploadFolder,
chunkIndex,
logger,
options.sourcemaps?.rewriteSources ?? defaultRewriteSourcesHook,
options.sourcemaps?.resolveSourceMap
);
}
);
const workers: Promise<void>[] = [];
const worker = async (): Promise<void> => {
while (preparationTasks.length > 0) {
const task = preparationTasks.shift();
if (task) {
await task();
}
}
};
for (let workerIndex = 0; workerIndex < 16; workerIndex++) {
workers.push(worker());
}
await Promise.all(workers);
const files = await fs.promises.readdir(tmpUploadFolder);
const stats = files.map((file) =>
fs.promises.stat(path.join(tmpUploadFolder, file))
);
const uploadSize = (await Promise.all(stats)).reduce(
(accumulator, { size }) => accumulator + size,
0
);
setMeasurement("files", files.length, "none", prepBundlesSpan);
setMeasurement("upload_size", uploadSize, "byte", prepBundlesSpan);
await startSpan({ name: "upload", scope: sentryScope }, async () => {
const cliInstance = createCliInstance(options);
await cliInstance.releases.uploadSourceMaps(
options.release.name ?? "undefined",
{
include: [
{
paths: [tmpUploadFolder],
rewrite: false,
dist: options.release.dist,
},
],
live: "rejectOnError",
}
);
});
}
);
logger.info("Successfully uploaded source maps to Sentry");
}
}
} catch (e) {
sentryScope.captureException('Error in "debugIdUploadPlugin" writeBundle hook');
handleRecoverableError(e, false);
} finally {
if (folderToCleanUp && !process.env?.["SENTRY_TEST_OVERRIDE_TEMP_DIR"]) {
void startSpan({ name: "cleanup", scope: sentryScope }, async () => {
if (folderToCleanUp) {
await fs.promises.rm(folderToCleanUp, { recursive: true, force: true });
}
});
}
freeUploadDependencyOnBuildArtifacts();
await safeFlushTelemetry(sentryClient);
}
}
);
},
/**
* Will delete artifacts based on the passed `sourcemaps.filesToDeleteAfterUpload` option.
*/
async deleteArtifacts() {
try {
const filesToDelete = await options.sourcemaps?.filesToDeleteAfterUpload;
if (filesToDelete !== undefined) {
const filePathsToDelete = await glob(filesToDelete, {
absolute: true,
nodir: true,
});
logger.debug(
"Waiting for dependencies on generated files to be freed before deleting..."
);
await waitUntilBuildArtifactDependenciesAreFreed();
filePathsToDelete.forEach((filePathToDelete) => {
logger.debug(`Deleting asset after upload: ${filePathToDelete}`);
});
await Promise.all(
filePathsToDelete.map((filePathToDelete) =>
fs.promises.rm(filePathToDelete, { force: true }).catch((e) => {
// This is allowed to fail - we just don't do anything
logger.debug(
`An error occurred while attempting to delete asset: ${filePathToDelete}`,
e
);
})
)
);
}
} catch (e) {
sentryScope.captureException('Error in "sentry-file-deletion-plugin" buildEnd hook');
await safeFlushTelemetry(sentryClient);
// We throw by default if we get here b/c not being able to delete
// source maps could leak them to production
handleRecoverableError(e, true);
}
},
createDependencyOnBuildArtifacts,
};
}
function canUploadSourceMaps(
options: NormalizedOptions,
logger: Logger,
isDevMode: boolean
): boolean {
if (options.sourcemaps?.disable) {
logger.debug(
"Source map upload was disabled. Will not upload sourcemaps using debug ID process."
);
return false;
}
if (isDevMode) {
logger.debug("Running in development mode. Will not upload sourcemaps.");
return false;
}
if (!options.authToken) {
logger.warn(
"No auth token provided. Will not upload source maps. Please set the `authToken` option. You can find information on how to generate a Sentry auth token here: https://docs.sentry.io/api/auth/" +
getTurborepoEnvPassthroughWarning("SENTRY_AUTH_TOKEN")
);
return false;
}
if (!options.org && !options.authToken.startsWith("sntrys_")) {
logger.warn(
"No org provided. Will not upload source maps. Please set the `org` option to your Sentry organization slug." +
getTurborepoEnvPassthroughWarning("SENTRY_ORG")
);
return false;
}
if (!options.project) {
logger.warn(
"No project provided. Will not upload source maps. Please set the `project` option to your Sentry project slug." +
getTurborepoEnvPassthroughWarning("SENTRY_PROJECT")
);
return false;
}
return true;
}