From 85a494ac794f6f5c35ff0c5196fa4beddb3026ef Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 28 Nov 2023 09:00:06 +0100 Subject: [PATCH] fix(astro): Configure sourcemap assets directory for Vercel adapter (#9665) This PR adds support for uploading sourcemaps when using the `@astrojs/vercel/*` adapters. It seems that the adapter updates the config.outdir value too late for our previous detection logic to correctly detect the .vercel output directory. Furthermore, the adapter for some reason first saves server files to /dist only to copy it later to /.vercel/functions. This copy command seems to happen too late for when our sourcemaps plugin runs. For these two reasons, we try to detect the used adapter (love that this is much simpler in Astro than Sveltekit) and adjust the assets glob to search for files in both, `dist` and `.vercel`. --- packages/astro/src/integration/index.ts | 10 +++++++ packages/astro/test/integration/index.test.ts | 27 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/packages/astro/src/integration/index.ts b/packages/astro/src/integration/index.ts index 5f5629697e3e..e844e383aaec 100644 --- a/packages/astro/src/integration/index.ts +++ b/packages/astro/src/integration/index.ts @@ -100,6 +100,16 @@ function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined { } function getSourcemapsAssetsGlob(config: AstroConfig): string { + // The vercel adapter puts the output into its .vercel directory + // However, the way this adapter is written, the config.outDir value is update too late for + // us to reliably detect it. Also, server files are first temporarily written to /dist and then + // only copied over to /.vercel. This seems to happen too late though. + // So we glob on both of these directories. + // Another case of "it ain't pretty but it works":( + if (config.adapter && config.adapter.name?.startsWith('@astrojs/vercel')) { + return '{.vercel,dist}/**/*'; + } + // paths are stored as "file://" URLs const outDirPathname = config.outDir && path.resolve(config.outDir.pathname); const rootDirName = path.resolve((config.root && config.root.pathname) || process.cwd()); diff --git a/packages/astro/test/integration/index.test.ts b/packages/astro/test/integration/index.test.ts index 51132971321d..fcf606c40e0b 100644 --- a/packages/astro/test/integration/index.test.ts +++ b/packages/astro/test/integration/index.test.ts @@ -83,6 +83,33 @@ describe('sentryAstro integration', () => { }); }); + it('sets the correct assets glob for vercel if the Vercel adapter is used', async () => { + const integration = sentryAstro({ + sourceMapsUploadOptions: { enabled: true, org: 'my-org', project: 'my-project', telemetry: false }, + }); + // @ts-expect-error - the hook exists and we only need to pass what we actually use + await integration.hooks['astro:config:setup']({ + updateConfig, + injectScript, + config: { + // @ts-expect-error - we only need to pass what we actually use + adapter: { name: '@astrojs/vercel/serverless' }, + }, + }); + + expect(sentryVitePluginSpy).toHaveBeenCalledTimes(1); + expect(sentryVitePluginSpy).toHaveBeenCalledWith({ + authToken: 'my-token', + org: 'my-org', + project: 'my-project', + telemetry: false, + debug: false, + sourcemaps: { + assets: ['{.vercel,dist}/**/*'], + }, + }); + }); + it("doesn't enable source maps if `sourceMapsUploadOptions.enabled` is `false`", async () => { const integration = sentryAstro({ sourceMapsUploadOptions: { enabled: false },