Skip to content

fix: improve Next.js monorepo compatibility#148

Merged
sohankshirsagar merged 1 commit intomainfrom
sohan/nextjs-monorepo
Mar 27, 2026
Merged

fix: improve Next.js monorepo compatibility#148
sohankshirsagar merged 1 commit intomainfrom
sohan/nextjs-monorepo

Conversation

@sohankshirsagar
Copy link
Copy Markdown
Contributor

Summary

Fixes several issues that arise when using the SDK inside monorepo setups (e.g. Turborepo, Nx) and with certain ORM driver adapters. The Next.js version detection now falls back to Node's module resolution for hoisted packages, a turbopack config key is preserved to avoid errors in Next.js 16+, and the pg instrumentation is hardened against non-thenable return values from query().

Changes

  • src/nextjs/utils.tsgetNextjsVersion() now falls back to require.resolve("next/package.json") when the cwd-based node_modules lookup fails, supporting monorepos where next is hoisted to a root node_modules.
  • src/nextjs/withTuskDrift.ts — Preserves the user's turbopack config (or sets an empty object) so Next.js 16+ doesn't error when it detects a webpack config without a turbopack counterpart.
  • src/instrumentation/libraries/pg/Instrumentation.ts — Adds a guard for when originalQuery returns a non-thenable value (e.g. Prisma's driver adapter), preventing a Cannot read properties of undefined (reading 'then') crash

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

@tusk-dev
Copy link
Copy Markdown

tusk-dev bot commented Mar 27, 2026

Generated 13 tests - 13 passed

Commit tests View tests

Test Summary

  • _wrapQueryWithInstrumentation - 5 ✓
  • getNextjsVersion - 8 ✓

Results

Tusk's tests are all passing and cover the three critical fixes in this PR. The pg instrumentation tests validate that the guard against non-thenable return values works across multiple edge cases—undefined, null, plain objects, and malformed then properties—ensuring compatibility with ORM adapters like Prisma without breaking standard Promise-based queries. The Next.js version detection tests confirm the fallback to require.resolve handles monorepo setups where next is hoisted, while also covering graceful degradation for missing files, invalid JSON, permission errors, and incomplete package.json files. Together, these tests protect against runtime crashes in real-world monorepo and adapter scenarios.

Key Points:

  • pg instrumentation — Five high-value tests ensure the _wrapQueryWithInstrumentation guard prevents crashes from non-thenable query returns while maintaining normal Promise flow
  • Next.js version detection — Eight tests validate monorepo compatibility via require.resolve fallback and robust error handling across file system edge cases
  • Hot paths covered — ORM adapter compatibility (Prisma) and monorepo dependency resolution, both critical for production stability
View check history

Commit Status Output Created (UTC)
16ecfd8 Generated 13 tests - 13 passed Tests Mar 27, 2026 5:05AM

Was Tusk helpful? Give feedback by reacting with 👍 or 👎

@sohankshirsagar sohankshirsagar merged commit f4abd19 into main Mar 27, 2026
19 checks passed
@sohankshirsagar sohankshirsagar deleted the sohan/nextjs-monorepo branch March 27, 2026 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants