Skip to content

fix(sveltekit): Detect sentry release before creating the Vite plugin #7902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/sveltekit/src/vite/sourceMaps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { getSentryRelease } from '@sentry/node';
import { uuid4 } from '@sentry/utils';
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
import { sentryVitePlugin } from '@sentry/vite-plugin';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
// @ts-ignore -sorcery has no types :(
Expand All @@ -22,6 +25,10 @@ type SentryVitePluginOptionsOptionalInclude = Omit<SentryVitePluginOptions, 'inc
include?: SentryVitePluginOptions['include'];
};

// storing this in the module scope because `makeCustomSentryVitePlugin` is called multiple times
// and we only want to generate a uuid once in case we have to fall back to it.
const release = detectSentryRelease();

/**
* Creates a new Vite plugin that uses the unplugin-based Sentry Vite plugin to create
* releases and upload source maps to Sentry.
Expand Down Expand Up @@ -51,6 +58,7 @@ export async function makeCustomSentryVitePlugin(options?: SentryVitePluginOptio
{ paths: [`${outputDir}/server`], ignore: ['chunks/**'] },
],
configFile: hasSentryProperties ? 'sentry.properties' : undefined,
release,
};

const mergedOptions = {
Expand Down Expand Up @@ -174,3 +182,19 @@ function getFiles(dir: string): string[] {

return Array.prototype.concat(...files);
}

function detectSentryRelease(): string {
let releaseFallback: string;
try {
releaseFallback = child_process.execSync('git rev-parse HEAD', { stdio: 'ignore' }).toString().trim();
} catch (_) {
// the command can throw for various reasons. Most importantly:
// - git is not installed
// - there is no git repo or no commit yet
// regardless of the case we just fall back to assigning a random uuid.
releaseFallback = uuid4();
}
const release = getSentryRelease() || releaseFallback;

return release;
}