Skip to content

Commit 70d1cbb

Browse files
authored
fix(astro): Fix import path when using external init files with default path (#10214)
Fix a bug in the Astro SDK where specifying an external `sentry.(client|server).config.js` in the default location file would cause a build error. Previously, we checked for a supported file extension and instead of adding an import statement for the path to the file, we'd only add the file extension. So ```js import 'mjs'; // instead of import 'path/to/sentry.client.config.mjs' ``` Fix the filename lookup function and adds a test to cover regressions.
1 parent fb7e516 commit 70d1cbb

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

dev-packages/e2e-tests/test-applications/cloudflare-astro/astro.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export default defineConfig({
1717
sourceMapsUploadOptions: {
1818
enabled: false,
1919
},
20-
clientInitPath: 'sentry.client.mjs',
20+
// purposefully setting the default name for client
21+
// and a custom name for server to test both cases
2122
serverInitPath: 'sentry.server.mjs',
2223
}),
2324
],

packages/astro/src/integration/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ const possibleFileExtensions = ['ts', 'js', 'tsx', 'jsx', 'mjs', 'cjs', 'mts'];
120120

121121
function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined {
122122
const cwd = process.cwd();
123-
return possibleFileExtensions.find(extension => {
124-
const filename = path.resolve(path.join(cwd, `sentry.${type}.config.${extension}`));
125-
return fs.existsSync(filename);
126-
});
123+
return possibleFileExtensions
124+
.map(e => path.resolve(path.join(cwd, `sentry.${type}.config.${e}`)))
125+
.find(filename => fs.existsSync(filename));
127126
}
128127

129128
function getSourcemapsAssetsGlob(config: AstroConfig): string {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { vi } from 'vitest';
2+
3+
import { sentryAstro } from '../../src/integration';
4+
5+
vi.mock('fs', async () => {
6+
const actual = await vi.importActual('fs');
7+
return {
8+
// @ts-expect-error - just mocking around
9+
...actual,
10+
existsSync: vi.fn(p => p.endsWith('js')),
11+
};
12+
});
13+
14+
const updateConfig = vi.fn();
15+
const injectScript = vi.fn();
16+
const config = {
17+
root: new URL('file://path/to/project'),
18+
outDir: new URL('file://path/to/project/out'),
19+
};
20+
21+
describe('sentryAstro integration', () => {
22+
afterEach(() => {
23+
vi.clearAllMocks();
24+
});
25+
26+
it('injects client and server init scripts from default paths if they exist', () => {
27+
const integration = sentryAstro({});
28+
29+
expect(integration.hooks['astro:config:setup']).toBeDefined();
30+
31+
// @ts-expect-error - the hook exists and we only need to pass what we actually use
32+
integration.hooks['astro:config:setup']({ updateConfig, injectScript, config });
33+
34+
expect(injectScript).toHaveBeenCalledTimes(2);
35+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringMatching(/^import ".*\/sentry.client.config.js"/));
36+
expect(injectScript).toHaveBeenCalledWith(
37+
'page-ssr',
38+
expect.stringMatching(/^import ".*\/sentry.server.config.js"/),
39+
);
40+
});
41+
});

packages/astro/test/integration/index.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,11 @@ describe('sentryAstro integration', () => {
239239
await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config });
240240

241241
expect(injectScript).toHaveBeenCalledTimes(2);
242-
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('my-client-init-path.js'));
243-
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('my-server-init-path.js'));
242+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringMatching(/^import ".*\/my-client-init-path.js"/));
243+
expect(injectScript).toHaveBeenCalledWith(
244+
'page-ssr',
245+
expect.stringMatching(/^import ".*\/my-server-init-path.js"/),
246+
);
244247
});
245248

246249
it.each(['server', 'hybrid'])(

0 commit comments

Comments
 (0)