Skip to content

Commit 5102de0

Browse files
authored
feat(nextjs): Be smarter in warning about old ways of init configuration (#11882)
In theory it is possible that people still have files like `sentry.server.config.ts` and reference them from `instrumentation.ts`. In that case we do not want to show the warning about the old way.
1 parent d06e290 commit 5102de0

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,39 @@ async function addSentryToClientEntryProperty(
495495
}
496496

497497
/**
498-
* Searches for old `sentry.(server|edge).config.ts` files and warns if it finds any.
498+
* Searches for old `sentry.(server|edge).config.ts` files and Next.js instrumentation hooks and warns if there are "old"
499+
* config files and no signs of them inside the instrumentation hook.
499500
*
500501
* @param projectDir The root directory of the project, where config files would be located
501502
* @param platform Either "server" or "edge", so that we know which file to look for
502503
*/
503504
function warnAboutDeprecatedConfigFiles(projectDir: string, platform: 'server' | 'edge'): void {
504-
const possibilities = [`sentry.${platform}.config.ts`, `sentry.${platform}.config.js`];
505+
const hasInstrumentationHookWithIndicationsOfSentry = [
506+
['src', 'instrumentation.ts'],
507+
['src', 'instrumentation.js'],
508+
['instrumentation.ts'],
509+
['instrumentation.js'],
510+
].some(potentialInstrumentationHookPathSegments => {
511+
try {
512+
const instrumentationHookContent = fs.readFileSync(
513+
path.resolve(projectDir, ...potentialInstrumentationHookPathSegments),
514+
{ encoding: 'utf-8' },
515+
);
505516

506-
for (const filename of possibilities) {
517+
return (
518+
instrumentationHookContent.includes('@sentry/') ||
519+
instrumentationHookContent.match(/sentry\.(server|edge)\.config\.(ts|js)/)
520+
);
521+
} catch (e) {
522+
return false;
523+
}
524+
});
525+
526+
if (hasInstrumentationHookWithIndicationsOfSentry) {
527+
return;
528+
}
529+
530+
for (const filename of [`sentry.${platform}.config.ts`, `sentry.${platform}.config.js`]) {
507531
if (fs.existsSync(path.resolve(projectDir, filename))) {
508532
// eslint-disable-next-line no-console
509533
console.warn(

0 commit comments

Comments
 (0)