Skip to content

Commit 8486a00

Browse files
authored
Sentry sourcemap 404 (#662)
1 parent c0b0a2e commit 8486a00

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

server/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,17 @@ const getHost = (req: { get: (key: string) => string | undefined }) =>
8787

8888
const MODE = process.env.NODE_ENV
8989

90-
if (MODE === 'production' && process.env.SENTRY_DSN) {
90+
const SHOULD_INIT_SENTRY =
91+
MODE === 'production' &&
92+
Boolean(process.env.SENTRY_DSN) &&
93+
// `start:mocks` (used in CI + local e2e) runs with `MOCKS=true`.
94+
process.env.MOCKS !== 'true'
95+
96+
if (SHOULD_INIT_SENTRY) {
9197
void import('./utils/monitoring.js').then(({ init }) => init())
9298
}
9399

94-
if (MODE === 'production') {
100+
if (SHOULD_INIT_SENTRY) {
95101
sentryInit({
96102
dsn: process.env.SENTRY_DSN,
97103
tracesSampleRate: 0.3,

vite.config.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,36 @@ import 'dotenv/config'
22
import { reactRouter } from '@react-router/dev/vite'
33
import { sentryVitePlugin } from '@sentry/vite-plugin'
44
import tailwindcss from '@tailwindcss/vite'
5-
import { glob } from 'glob'
65
import { defineConfig } from 'vite'
76
import { envOnlyMacros } from 'vite-env-only'
87
import { cjsInterop } from 'vite-plugin-cjs-interop'
98
import tsconfigPaths from 'vite-tsconfig-paths'
109

1110
const MODE = process.env.NODE_ENV
11+
const SENTRY_UPLOAD =
12+
process.env.SENTRY_UPLOAD === 'true' || process.env.SENTRY_UPLOAD === '1'
13+
14+
if (SENTRY_UPLOAD && MODE === 'production') {
15+
const authToken = process.env.SENTRY_AUTH_TOKEN
16+
const project = process.env.SENTRY_PROJECT
17+
const org = process.env.SENTRY_ORG
18+
// New-style Sentry auth tokens (prefix "sntrys_") embed the org, so SENTRY_ORG
19+
// is not required when using one.
20+
const tokenImpliesOrg = Boolean(authToken?.startsWith('sntrys_'))
21+
22+
// If upload is "on" but required settings are missing, the Sentry plugin will
23+
// just warn + skip the upload. Fail fast so we don't silently deploy without
24+
// sourcemaps in Sentry.
25+
if (!authToken) {
26+
throw new Error('SENTRY_UPLOAD is enabled, but SENTRY_AUTH_TOKEN is missing')
27+
}
28+
if (!project) {
29+
throw new Error('SENTRY_UPLOAD is enabled, but SENTRY_PROJECT is missing')
30+
}
31+
if (!org && !tokenImpliesOrg) {
32+
throw new Error('SENTRY_UPLOAD is enabled, but SENTRY_ORG is missing')
33+
}
34+
}
1235

1336
export default defineConfig(async () => {
1437
return {
@@ -24,28 +47,31 @@ export default defineConfig(async () => {
2447
tailwindcss(),
2548
reactRouter(),
2649
tsconfigPaths(),
27-
process.env.SENTRY_UPLOAD
50+
SENTRY_UPLOAD
2851
? sentryVitePlugin({
2952
disable: MODE !== 'production',
3053
authToken: process.env.SENTRY_AUTH_TOKEN,
3154
org: process.env.SENTRY_ORG,
3255
project: process.env.SENTRY_PROJECT,
56+
// By default the bundler plugin logs and continues on upload/release
57+
// errors. Fail the build so we don't deploy with broken symbolication.
58+
errorHandler: (err) => {
59+
throw err
60+
},
3361
release: {
3462
name: process.env.COMMIT_SHA,
3563
setCommits: {
3664
auto: true,
3765
},
3866
},
39-
sourcemaps: {
40-
filesToDeleteAfterUpload: await glob([
41-
'./build/**/*.map',
42-
'.server-build/**/*.map',
43-
]),
44-
},
4567
})
4668
: null,
4769
],
4870
build: {
71+
// This is an OSS project, so it's fine to generate "regular" sourcemaps.
72+
// If we ever want sourcemaps upload without exposing them publicly, switch
73+
// to `sourcemap: 'hidden'` (and keep uploading to Sentry).
74+
sourcemap: true,
4975
cssMinify: MODE === 'production',
5076
rollupOptions: {
5177
external: [/node:.*/, 'stream', 'crypto'],

0 commit comments

Comments
 (0)