Skip to content

feat(nextjs): Be smarter in warning about old ways of init configuration #11882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2024
Merged
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
30 changes: 27 additions & 3 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,39 @@ async function addSentryToClientEntryProperty(
}

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

for (const filename of possibilities) {
return (
instrumentationHookContent.includes('@sentry/') ||
instrumentationHookContent.match(/sentry\.(server|edge)\.config\.(ts|js)/)
);
} catch (e) {
return false;
}
});

if (hasInstrumentationHookWithIndicationsOfSentry) {
return;
}

for (const filename of [`sentry.${platform}.config.ts`, `sentry.${platform}.config.js`]) {
if (fs.existsSync(path.resolve(projectDir, filename))) {
// eslint-disable-next-line no-console
console.warn(
Expand Down
Loading