Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,26 +1,101 @@
import * as assert from 'node:assert/strict';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { assertSourceMapUpload } from '@sentry-internal/test-utils';
import {
assertSourceMapUpload,
findInjectedDebugIds,
findSourceMapFiles,
findSourceMappingUrlComments,
getChunkUploadPosts,
loadMockServerResults,
} from '@sentry-internal/test-utils';

const isStatic = process.env.E2E_ASTRO_OUTPUT === 'static';
const noUpload = !!process.env.E2E_NO_UPLOAD;

const BUILD_OUTPUT = 'dist';
const OUTPUT_DIRS = isStatic ? [BUILD_OUTPUT] : [path.join(BUILD_OUTPUT, 'client'), path.join(BUILD_OUTPUT, 'server')];

assertSourceMapUpload({
outputDirs: OUTPUT_DIRS,
// The app does not set `build.sourcemap`, so the SDK enables hidden maps and must delete them after upload.
strippedDirs: [BUILD_OUTPUT],
markers: {
'client source': 'SOURCEMAP_MARKER_CLIENT',
...(isStatic ? {} : { 'server source': 'SOURCEMAP_MARKER_SERVER' }),
// Astro 7 writes the prerender build to a hidden `.prerender` folder, which a plain `dist/**/*` glob skips
'prerendered page source': 'SOURCEMAP_MARKER_PRERENDER',
},
// Written by the build, see `recordChunksWithoutSourceMap` in `astro.config.mjs`.
debugIdsWithoutSourceMap: '.tmp_debug_ids_without_source_map',
});
// Without an auth token no upload can run, so the SDK turns on no source maps. The bundler plugin
// still injects debug IDs.
if (noUpload) {
// Written by the build, see `recordSourceMaps` in `astro.config.mjs`.
const mapsMade = new Map<string, number>();
for (const line of fs.readFileSync('.tmp_source_maps_made', 'utf8').split('\n').filter(Boolean)) {
const [environment, count] = line.split(' ');
mapsMade.set(environment as string, (mapsMade.get(environment as string) ?? 0) + Number(count));
}
for (const environment of ['client', 'ssr', 'prerender']) {
assert.equal(mapsMade.get(environment), 0, `Expected the ${environment} build to make no source maps`);
}
assert.notDeepEqual(findInjectedDebugIds({ outputDir: BUILD_OUTPUT }), [], `Expected debug IDs in ${BUILD_OUTPUT}`);

assert.deepEqual(findSourceMapFiles({ outputDir: BUILD_OUTPUT }), [], `Expected no source maps in ${BUILD_OUTPUT}`);
assert.deepEqual(
findSourceMappingUrlComments({ outputDir: BUILD_OUTPUT }),
[],
`Expected no sourceMappingURL comments in ${BUILD_OUTPUT}`,
);

const buildLog = fs.readFileSync('.tmp_build_stdout', 'utf8') + fs.readFileSync('.tmp_build_stderr', 'utf8');
assert.ok(
!buildLog.includes('deleteSourcemapsAfterUpload'),
'Expected no message about `sourcemaps.deleteSourcemapsAfterUpload`, which the app does not set',
);
} else if (process.env.E2E_VARIANT === 'manual-upload') {
// `upload: false` with `delete: false`: nothing reaches Sentry, and the hidden maps Sentry turned on
// stay with the debug IDs of their chunks, ready for an upload with the CLI.
assert.deepEqual(getChunkUploadPosts(loadMockServerResults()), [], 'Expected no source map upload');
assert.deepEqual(
findSourceMappingUrlComments({ outputDir: BUILD_OUTPUT }),
[],
`Expected no sourceMappingURL comments in ${BUILD_OUTPUT}`,
);

// Written by the build, see `recordSourceMaps` in `astro.config.mjs`.
const debugIdsWithoutSourceMap = new Set(
fs.existsSync('.tmp_debug_ids_without_source_map')
? fs.readFileSync('.tmp_debug_ids_without_source_map', 'utf8').split('\n').filter(Boolean)
: [],
);

for (const outputDir of OUTPUT_DIRS) {
const mapDebugIds = new Set(
findSourceMapFiles({ outputDir }).map(file => {
const map = JSON.parse(fs.readFileSync(file, 'utf8')) as { debug_id?: string; debugId?: string };
return (map.debug_id ?? map.debugId)?.toLowerCase();
}),
);
const injectedDebugIds = findInjectedDebugIds({ outputDir });

assert.ok(injectedDebugIds.length > 0, `Expected debug IDs to be injected into ${outputDir}`);
assert.deepEqual(
injectedDebugIds.filter(debugId => !mapDebugIds.has(debugId) && !debugIdsWithoutSourceMap.has(debugId)),
[],
`Expected a source map with the same debug ID for every chunk in ${outputDir}`,
);
assert.ok(
injectedDebugIds.some(debugId => mapDebugIds.has(debugId)),
`Expected at least one chunk in ${outputDir} to have a kept source map`,
);
console.log(` ${outputDir}: ${injectedDebugIds.length} debug ID(s), ${mapDebugIds.size} kept source map(s)`);
}
} else {
// Also covers the `delete-all` variant: the maps the app turned on are uploaded and removed.
assertSourceMapUpload({
outputDirs: OUTPUT_DIRS,
// The app does not set `build.sourcemap`, so the SDK enables hidden maps and must delete them after upload.
strippedDirs: [BUILD_OUTPUT],
markers: {
'client source': 'SOURCEMAP_MARKER_CLIENT',
...(isStatic ? {} : { 'server source': 'SOURCEMAP_MARKER_SERVER' }),
// Astro 7 writes the prerender build to a hidden `.prerender` folder, which a plain `dist/**/*` glob skips
'prerendered page source': 'SOURCEMAP_MARKER_PRERENDER',
},
// Written by the build, see `recordChunksWithoutSourceMap` in `astro.config.mjs`.
debugIdsWithoutSourceMap: '.tmp_debug_ids_without_source_map',
});
}

// The release must stay out of the content-hashed client chunks, or every release renames them and busts their cache.
const CLIENT_DIR = isStatic ? BUILD_OUTPUT : path.join(BUILD_OUTPUT, 'client');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import { defineConfig } from 'astro/config';
import * as fs from 'node:fs';

const isStatic = process.env.E2E_ASTRO_OUTPUT === 'static';
const noUpload = !!process.env.E2E_NO_UPLOAD;
// `manual-upload`: Sentry uploads nothing and keeps the maps it turned on, for an upload with the CLI.
// `delete-all`: the app turns source maps on itself, and Sentry removes them.
const variant = process.env.E2E_VARIANT;

// Rolldown emits no map for chunks without mappable source (e.g. virtual modules or its runtime), so their debug
// IDs can't be uploaded. Recorded here because the SDK deletes all maps after the upload.
function recordChunksWithoutSourceMap() {
// Records the source maps each environment made, before the Sentry plugin removes them in its
// `generateBundle` with `order: 'post'`. Rolldown emits no map for chunks without mappable source
// (e.g. virtual modules or its runtime), so their debug IDs can't be uploaded.
function recordSourceMaps() {
return {
name: 'record-chunks-without-source-map',
name: 'record-source-maps',
apply: 'build',
enforce: 'post',
writeBundle(_options, bundle) {
generateBundle(_options, bundle) {
const maps = Object.keys(bundle).filter(fileName => fileName.endsWith('.map'));
fs.appendFileSync('.tmp_source_maps_made', `${this.environment.name} ${maps.length}\n`);

for (const output of Object.values(bundle)) {
if (output.type !== 'chunk' || bundle[`${output.fileName}.map`]) {
continue;
Expand All @@ -39,16 +46,25 @@ export default defineConfig({
integrations: [
sentry({
sentryUrl: 'http://localhost:3032',
authToken: 'fake-auth-token',
authToken: noUpload ? undefined : 'fake-auth-token',
org: 'test-org',
project: 'test-project',
release: { name: 'test-release' },
debug: true,
sourcemaps:
variant === 'manual-upload'
? { upload: false, delete: false }
: variant === 'delete-all'
? { delete: true }
: undefined,
}),
...(isStatic ? [] : [ssrRoute]),
],
// Keeps Astro from inlining the small client script into the HTML, where it would get no source map
vite: { build: { assetsInlineLimit: 0 }, plugins: [recordChunksWithoutSourceMap()] },
vite: {
build: { assetsInlineLimit: 0, ...(variant === 'delete-all' && { sourcemap: true }) },
plugins: [recordSourceMaps()],
},
output: isStatic ? 'static' : 'server',
adapter: isStatic ? undefined : node({ mode: 'standalone' }),
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
"test:build": "pnpm install && pnpm build",
"test:assert": "node assert-build.ts",
"test:build:static": "E2E_ASTRO_OUTPUT=static pnpm test:build",
"test:assert:static": "E2E_ASTRO_OUTPUT=static pnpm test:assert"
"test:assert:static": "E2E_ASTRO_OUTPUT=static pnpm test:assert",
"test:build:no-upload": "E2E_NO_UPLOAD=1 pnpm test:build",
"test:assert:no-upload": "E2E_NO_UPLOAD=1 pnpm test:assert",
"test:build:manual-upload": "E2E_VARIANT=manual-upload pnpm test:build",
"test:assert:manual-upload": "E2E_VARIANT=manual-upload pnpm test:assert",
"test:build:delete-all": "E2E_VARIANT=delete-all pnpm test:build",
"test:assert:delete-all": "E2E_VARIANT=delete-all pnpm test:assert"
},
"dependencies": {
"@astrojs/node": "^11.1.4",
Expand All @@ -26,6 +32,21 @@
"build-command": "pnpm test:build:static",
"assert-command": "pnpm test:assert:static",
"label": "astro-7-sourcemaps (static output)"
},
{
"build-command": "pnpm test:build:no-upload",
"assert-command": "pnpm test:assert:no-upload",
"label": "astro-7-sourcemaps (no upload)"
},
{
"build-command": "pnpm test:build:manual-upload",
"assert-command": "pnpm test:assert:manual-upload",
"label": "astro-7-sourcemaps (manual upload)"
},
{
"build-command": "pnpm test:build:delete-all",
"assert-command": "pnpm test:assert:delete-all",
"label": "astro-7-sourcemaps (delete all)"
}
]
}
Expand Down
Loading
Loading