Skip to content

Commit

Permalink
fix: Resolve the deletion task order
Browse files Browse the repository at this point in the history
  • Loading branch information
tyouzu1 committed Jul 11, 2024
1 parent c68361a commit 3e76c60
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
5 changes: 3 additions & 2 deletions packages/bundler-plugin-core/src/debug-id-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface DebugIdUploadPluginOptions {
silent: boolean;
headers?: Record<string, string>;
};
freeDependencyOnSourcemapFiles: () => void;
createDependencyOnSourcemapFiles: () => () => void;
}

export function createDebugIdUploadFunction({
Expand All @@ -47,7 +47,7 @@ export function createDebugIdUploadFunction({
sentryClient,
sentryCliOptions,
rewriteSourcesHook,
freeDependencyOnSourcemapFiles,
createDependencyOnSourcemapFiles,
}: DebugIdUploadPluginOptions) {
return async (buildArtifactPaths: string[]) => {
const artifactBundleUploadTransaction = sentryHub.startTransaction({
Expand All @@ -56,6 +56,7 @@ export function createDebugIdUploadFunction({

let folderToCleanUp: string | undefined;

const freeDependencyOnSourcemapFiles = createDependencyOnSourcemapFiles();
try {
const mkdtempSpan = artifactBundleUploadTransaction.startChild({ description: "mkdtemp" });
const tmpUploadFolder = await fs.promises.mkdtemp(
Expand Down
6 changes: 3 additions & 3 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export function sentryUnpluginFactory({
vcsRemote: options.release.vcsRemote,
headers: options.headers,
},
freeDependencyOnSourcemapFiles: createDependencyOnSourcemapFiles(),
createDependencyOnSourcemapFiles,
})
);
}
Expand All @@ -367,7 +367,7 @@ export function sentryUnpluginFactory({
createDebugIdUploadFunction({
assets: options.sourcemaps?.assets,
ignore: options.sourcemaps?.ignore,
freeDependencyOnSourcemapFiles: createDependencyOnSourcemapFiles(),
createDependencyOnSourcemapFiles,
dist: options.release.dist,
releaseName: options.release.name,
logger: logger,
Expand Down Expand Up @@ -407,7 +407,7 @@ export function sentryUnpluginFactory({
fileDeletionPlugin({
// It is very important that this is only called after all other dependencies have been created with `createDependencyOnSourcemapFiles`.
// Ideally, we always register this plugin last.
dependenciesAreFreedPromise: waitUntilSourcemapFileDependenciesAreFreed(),
dependenciesAreFreedPromise: () => waitUntilSourcemapFileDependenciesAreFreed(),
filesToDeleteAfterUpload:
options.sourcemaps?.filesToDeleteAfterUpload ??
options.sourcemaps?.deleteFilesAfterUpload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface ReleaseManagementPluginOptions {
silent: boolean;
headers?: Record<string, string>;
};
freeDependencyOnSourcemapFiles: () => void;
createDependencyOnSourcemapFiles: () => () => void;
}

export function releaseManagementPlugin({
Expand All @@ -42,11 +42,12 @@ export function releaseManagementPlugin({
sentryHub,
sentryClient,
sentryCliOptions,
freeDependencyOnSourcemapFiles,
createDependencyOnSourcemapFiles,
}: ReleaseManagementPluginOptions): UnpluginOptions {
return {
name: "sentry-debug-id-upload-plugin",
async writeBundle() {
const freeDependencyOnSourcemapFiles = createDependencyOnSourcemapFiles()
try {
const cliInstance = new SentryCli(null, sentryCliOptions);

Expand Down
74 changes: 34 additions & 40 deletions packages/bundler-plugin-core/src/plugins/sourcemap-deletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fs from "fs";

interface FileDeletionPlugin {
handleRecoverableError: (error: unknown) => void;
dependenciesAreFreedPromise: Promise<void>;
dependenciesAreFreedPromise: () => Promise<void>;
sentryHub: Hub;
sentryClient: NodeClient;
filesToDeleteAfterUpload: string | string[] | undefined;
Expand All @@ -22,49 +22,43 @@ export function fileDeletionPlugin({
dependenciesAreFreedPromise,
logger,
}: FileDeletionPlugin): UnpluginOptions {
const writeBundle = async () => {
try {
if (filesToDeleteAfterUpload !== undefined) {
const filePathsToDelete = await glob(filesToDeleteAfterUpload, {
absolute: true,
nodir: true,
});
return {
name: "sentry-file-deletion-plugin",
async writeBundle() {
try {
if (filesToDeleteAfterUpload !== undefined) {
const filePathsToDelete = await glob(filesToDeleteAfterUpload, {
absolute: true,
nodir: true,
});

logger.debug("Waiting for dependencies on generated files to be freed before deleting...");
logger.debug(
"Waiting for dependencies on generated files to be freed before deleting..."
);

await dependenciesAreFreedPromise;
await dependenciesAreFreedPromise();

filePathsToDelete.forEach((filePathToDelete) => {
logger.debug(`Deleting asset after upload: ${filePathToDelete}`);
});
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
);
})
)
);
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) {
sentryHub.captureException('Error in "sentry-file-deletion-plugin" buildEnd hook');
await safeFlushTelemetry(sentryClient);
handleRecoverableError(e);
}
} catch (e) {
sentryHub.captureException('Error in "sentry-file-deletion-plugin" buildEnd hook');
await safeFlushTelemetry(sentryClient);
handleRecoverableError(e);
}
};
return {
name: "sentry-file-deletion-plugin",
vite: {
writeBundle: {
sequential: true,
order: "post",
handler: writeBundle,
},
},
writeBundle,
};
}
}

0 comments on commit 3e76c60

Please sign in to comment.