Skip to content

Commit 0b77163

Browse files
committed
fix: Wait for tasks depending on sourcemaps before deleting
1 parent 62de37e commit 0b77163

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

packages/bundler-plugin-core/src/debug-id-upload.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ interface DebugIdUploadPluginOptions {
2424
handleRecoverableError: (error: unknown) => void;
2525
sentryHub: Hub;
2626
sentryClient: NodeClient;
27-
deleteFilesUpForDeletion: () => Promise<void>;
2827
sentryCliOptions: {
2928
url: string;
3029
authToken: string;
@@ -34,6 +33,7 @@ interface DebugIdUploadPluginOptions {
3433
silent: boolean;
3534
headers?: Record<string, string>;
3635
};
36+
completeTaskDependingOnSourcemaps: () => void;
3737
}
3838

3939
export function createDebugIdUploadFunction({
@@ -47,7 +47,7 @@ export function createDebugIdUploadFunction({
4747
sentryClient,
4848
sentryCliOptions,
4949
rewriteSourcesHook,
50-
deleteFilesUpForDeletion,
50+
completeTaskDependingOnSourcemaps,
5151
}: DebugIdUploadPluginOptions) {
5252
return async (buildArtifactPaths: string[]) => {
5353
const artifactBundleUploadTransaction = sentryHub.startTransaction({
@@ -179,8 +179,6 @@ export function createDebugIdUploadFunction({
179179
uploadSpan.finish();
180180
logger.info("Successfully uploaded source maps to Sentry");
181181
}
182-
183-
await deleteFilesUpForDeletion();
184182
} catch (e) {
185183
sentryHub.withScope((scope) => {
186184
scope.setSpan(artifactBundleUploadTransaction);
@@ -196,6 +194,7 @@ export function createDebugIdUploadFunction({
196194
cleanupSpan.finish();
197195
}
198196
artifactBundleUploadTransaction.finish();
197+
completeTaskDependingOnSourcemaps();
199198
await safeFlushTelemetry(sentryClient);
200199
}
201200
};

packages/bundler-plugin-core/src/index.ts

+41-11
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,37 @@ export function sentryUnpluginFactory({
185185
})
186186
);
187187

188+
const tasks = new Set<symbol>();
189+
const subscribers: (() => void)[] = [];
190+
191+
function notifySubscribers() {
192+
subscribers.forEach((subscriber) => {
193+
subscriber();
194+
});
195+
}
196+
197+
function createTaskDependingOnSourcemaps() {
198+
const taskIdentifier = Symbol();
199+
tasks.add(taskIdentifier);
200+
return function completeTaskDependingOnSourcemaps() {
201+
tasks.delete(taskIdentifier);
202+
notifySubscribers();
203+
};
204+
}
205+
188206
async function deleteFilesUpForDeletion() {
207+
await new Promise<void>((resolve) => {
208+
subscribers.push(() => {
209+
if (tasks.size === 0) {
210+
resolve();
211+
}
212+
});
213+
214+
if (tasks.size === 0) {
215+
resolve();
216+
}
217+
});
218+
189219
const filesToDeleteAfterUpload =
190220
options.sourcemaps?.filesToDeleteAfterUpload ?? options.sourcemaps?.deleteFilesAfterUpload;
191221

@@ -326,22 +356,13 @@ export function sentryUnpluginFactory({
326356
vcsRemote: options.release.vcsRemote,
327357
headers: options.headers,
328358
},
329-
deleteFilesUpForDeletion,
359+
completeTaskDependingOnSourcemaps: createTaskDependingOnSourcemaps(),
330360
})
331361
);
332362
}
333363

334364
plugins.push(debugIdInjectionPlugin(logger));
335365

336-
plugins.push(
337-
fileDeletionPlugin({
338-
deleteFilesUpForDeletion,
339-
handleRecoverableError,
340-
sentryHub,
341-
sentryClient,
342-
})
343-
);
344-
345366
if (!options.authToken) {
346367
logger.warn(
347368
"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/"
@@ -360,7 +381,7 @@ export function sentryUnpluginFactory({
360381
createDebugIdUploadFunction({
361382
assets: options.sourcemaps?.assets,
362383
ignore: options.sourcemaps?.ignore,
363-
deleteFilesUpForDeletion,
384+
completeTaskDependingOnSourcemaps: createTaskDependingOnSourcemaps(),
364385
dist: options.release.dist,
365386
releaseName: options.release.name,
366387
logger: logger,
@@ -396,6 +417,15 @@ export function sentryUnpluginFactory({
396417
}
397418
}
398419

420+
plugins.push(
421+
fileDeletionPlugin({
422+
deleteFilesUpForDeletion,
423+
handleRecoverableError,
424+
sentryHub,
425+
sentryClient,
426+
})
427+
);
428+
399429
return plugins;
400430
});
401431
}

packages/bundler-plugin-core/src/plugins/release-management.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface ReleaseManagementPluginOptions {
2727
silent: boolean;
2828
headers?: Record<string, string>;
2929
};
30-
deleteFilesUpForDeletion: () => Promise<void>;
30+
completeTaskDependingOnSourcemaps: () => void;
3131
}
3232

3333
export function releaseManagementPlugin({
@@ -42,7 +42,7 @@ export function releaseManagementPlugin({
4242
sentryHub,
4343
sentryClient,
4444
sentryCliOptions,
45-
deleteFilesUpForDeletion,
45+
completeTaskDependingOnSourcemaps,
4646
}: ReleaseManagementPluginOptions): UnpluginOptions {
4747
return {
4848
name: "sentry-debug-id-upload-plugin",
@@ -85,12 +85,12 @@ export function releaseManagementPlugin({
8585
if (deployOptions) {
8686
await cliInstance.releases.newDeploy(releaseName, deployOptions);
8787
}
88-
89-
await deleteFilesUpForDeletion();
9088
} catch (e) {
9189
sentryHub.captureException('Error in "releaseManagementPlugin" writeBundle hook');
9290
await safeFlushTelemetry(sentryClient);
9391
handleRecoverableError(e);
92+
} finally {
93+
completeTaskDependingOnSourcemaps();
9494
}
9595
},
9696
};

0 commit comments

Comments
 (0)