Skip to content

Conversation

@heatlikeheatwave
Copy link
Contributor

@heatlikeheatwave heatlikeheatwave commented Aug 20, 2025

Description

The telemetry event KEYLESS_ENV_DRIFT_DETECTED is not sent due to the error Failed to create telemetry flag file: TypeError: mkdir is not a function. This PR changes mkdir, and writeFile to mkdirSync, and writeFileSync in the keyless telemetry logic.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of keyless drift-detection telemetry events in Next.js so telemetry is emitted more consistently; no changes to app behavior or telemetry event structure.
  • Chores

    • Added a changeset documenting a patch release for @clerk/nextjs that records the telemetry reliability fix.

@changeset-bot
Copy link

changeset-bot bot commented Aug 20, 2025

🦋 Changeset detected

Latest commit: b8b72af

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/nextjs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Aug 20, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
clerk-js-sandbox Ready Ready Preview Comment Aug 21, 2025 10:35am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 20, 2025

📝 Walkthrough

Walkthrough

A new changeset file .changeset/sweet-vans-wonder.md was added documenting a patch release for @clerk/nextjs fixing keyless drift detection failure. In packages/nextjs/src/server/keyless-telemetry.ts, async fs calls in tryMarkTelemetryEventAsFired were replaced with synchronous variants: nodeFsOrThrow now exposes mkdirSync and writeFileSync; directory creation uses mkdirSync(..., { recursive: true }); flag file creation uses writeFileSync(..., { flag: 'wx' }). Control flow, return values, and telemetry/drift-detection logic are unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@heatlikeheatwave heatlikeheatwave changed the title fix(nextjs): Keyless drift detection not firing due to async node fs methods fix(nextjs): Keyless drift detection telemetry bug from usage of async methods Aug 20, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 20, 2025

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@6594

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@6594

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@6594

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@6594

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@6594

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@6594

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@6594

@clerk/clerk-expo

npm i https://pkg.pr.new/@clerk/clerk-expo@6594

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@6594

@clerk/express

npm i https://pkg.pr.new/@clerk/express@6594

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@6594

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@6594

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@6594

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@6594

@clerk/clerk-react

npm i https://pkg.pr.new/@clerk/clerk-react@6594

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@6594

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@6594

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@6594

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@6594

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@6594

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@6594

@clerk/types

npm i https://pkg.pr.new/@clerk/types@6594

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@6594

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@6594

commit: b8b72af

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (6)
.changeset/sweet-vans-wonder.md (1)

5-5: Polish the changeset summary for clarity and tense.

Current phrasing is a bit clunky. Consider tightening and explicitly naming the sync APIs used.

-fix(nextjs): Fix for keyless drift detection telemetry event not firing due to usage of async methods in the keyless telemetry logic
+fix(nextjs): Keyless drift detection telemetry event did not fire because async Node fs APIs were used; replace with mkdirSync/writeFileSync in keyless telemetry logic
packages/nextjs/src/server/keyless-telemetry.ts (5)

52-59: Remove stray await keywords around synchronous fs calls.

mkdirSync and writeFileSync are synchronous and do not return Promises; awaiting them is a no-op and may confuse future readers and linters.

Apply this diff:

-      // Ensure the directory exists before attempting to write the file
-      await mkdirSync(flagDirectory, { recursive: true });
+      // Ensure the directory exists before attempting to write the file
+      mkdirSync(flagDirectory, { recursive: true });

       const flagData = {
         firedAt: new Date().toISOString(),
         event: EVENT_KEYLESS_ENV_DRIFT_DETECTED,
       };
-      await writeFileSync(flagFilePath, JSON.stringify(flagData, null, 2), { flag: 'wx' });
+      writeFileSync(flagFilePath, JSON.stringify(flagData, null, 2), { flag: 'wx' });

Optional follow-up (if you want to fully de-async the helper): make tryMarkTelemetryEventAsFired non-async and drop the await at the single call site.

-async function tryMarkTelemetryEventAsFired(): Promise<boolean> {
+function tryMarkTelemetryEventAsFired(): boolean {
-    const shouldFireEvent = await tryMarkTelemetryEventAsFired();
+    const shouldFireEvent = tryMarkTelemetryEventAsFired();

175-177: Reuse the EVENT_SAMPLING_RATE constant for consistency.

Avoid magic numbers and single-source the sampling rate.

-      telemetry: {
-        samplingRate: 1,
-      },
+      telemetry: {
+        samplingRate: EVENT_SAMPLING_RATE,
+      },

10-31: Consider moving the flag file under os.tmpdir() to improve portability.

Writing to process.cwd()/.clerk/.tmp may fail on some serverless hosts (read-only deploy assets). Using the OS temp directory reduces the chance of EACCES while keeping the per-process “once” semantics.

If you want, I can open a follow-up PR to:

  • switch to join(os.tmpdir(), 'clerk', 'telemetry.json'),
  • ensure recursive dir creation,
  • and update tests accordingly.

92-96: Nit: duplicate canUseKeyless guard.

detectKeylessEnvDrift already early-returns when !canUseKeyless. The second if (canUseKeyless) guard inside tryMarkTelemetryEventAsFired is redundant unless you expect this helper to be reused elsewhere.

Two options:

  • Keep it for defensive programming, or
  • Remove the inner guard to simplify control flow.

180-191: Add a targeted test to prevent regressions (event fired once, not more).

This change fixes a production issue; adding a small test helps ensure we don’t regress. Suggested coverage:

  • When drift is present, the first call records the event, subsequent calls do not.
  • The helper gracefully handles pre-existing flag file (EEXIST) without throwing.

I can provide a Jest test that:

  • stubs nodeFsOrThrow to use real sync fs,
  • points process.cwd() to a temporary directory,
  • spies on clerkClient.telemetry.record,
  • asserts the single-record behavior.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 4ac7b25 and 84e77e6.

📒 Files selected for processing (2)
  • .changeset/sweet-vans-wonder.md (1 hunks)
  • packages/nextjs/src/server/keyless-telemetry.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (8)
.changeset/**

📄 CodeRabbit Inference Engine (.cursor/rules/monorepo.mdc)

Automated releases must use Changesets.

Files:

  • .changeset/sweet-vans-wonder.md
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)

**/*.{js,jsx,ts,tsx}: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels

Files:

  • packages/nextjs/src/server/keyless-telemetry.ts
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}

📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)

Use Prettier for consistent code formatting

Files:

  • packages/nextjs/src/server/keyless-telemetry.ts
packages/**/*.{ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)

TypeScript is required for all packages

Files:

  • packages/nextjs/src/server/keyless-telemetry.ts
packages/**/*.{ts,tsx,d.ts}

📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)

Packages should export TypeScript types alongside runtime code

Files:

  • packages/nextjs/src/server/keyless-telemetry.ts
**/*.{ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)

Use proper TypeScript error types

**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoid any type - prefer unknown when type is uncertain, then narrow with type guards
Use interface for object shapes that might be extended
Use type for unions, primitives, and computed types
Prefer readonly properties for immutable data structures
Use private for internal implementation details
Use protected for inheritance hierarchies
Use public explicitly for clarity in public APIs
Prefer readonly for properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Use const assertions for literal types: as const
Use satisfies operator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports: import type { ... } from ...
No any types without justification
Proper error handling with typed errors
Consistent use of readonly for immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)

Files:

  • packages/nextjs/src/server/keyless-telemetry.ts
**/*.{js,ts,tsx,jsx}

📄 CodeRabbit Inference Engine (.cursor/rules/monorepo.mdc)

Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.

Files:

  • packages/nextjs/src/server/keyless-telemetry.ts
**/*

⚙️ CodeRabbit Configuration File

If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes.

Files:

  • packages/nextjs/src/server/keyless-telemetry.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (25)
  • GitHub Check: pr-title-lint
  • GitHub Check: Integration Tests (nuxt, chrome)
  • GitHub Check: Integration Tests (nextjs, chrome, 15)
  • GitHub Check: Integration Tests (expo-web, chrome)
  • GitHub Check: Integration Tests (machine, chrome)
  • GitHub Check: Integration Tests (react-router, chrome)
  • GitHub Check: Integration Tests (vue, chrome)
  • GitHub Check: Integration Tests (billing, chrome)
  • GitHub Check: Integration Tests (astro, chrome)
  • GitHub Check: Integration Tests (tanstack-react-router, chrome)
  • GitHub Check: Integration Tests (nextjs, chrome, 14)
  • GitHub Check: Integration Tests (sessions, chrome)
  • GitHub Check: Integration Tests (express, chrome)
  • GitHub Check: Integration Tests (ap-flows, chrome)
  • GitHub Check: Integration Tests (tanstack-react-start, chrome)
  • GitHub Check: Integration Tests (generic, chrome)
  • GitHub Check: Integration Tests (localhost, chrome)
  • GitHub Check: Integration Tests (elements, chrome)
  • GitHub Check: Integration Tests (quickstart, chrome)
  • GitHub Check: Publish with pkg-pr-new
  • GitHub Check: Unit Tests (18, --filter=@clerk/astro --filter=@clerk/backend --filter=@clerk/express --filter=@c...
  • GitHub Check: Unit Tests (22, **)
  • GitHub Check: Static analysis
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: semgrep-cloud-platform/scan
🔇 Additional comments (2)
.changeset/sweet-vans-wonder.md (1)

2-2: Package and bump type look correct.

Patch bump for @clerk/nextjs matches the scope and impact of the change.

packages/nextjs/src/server/keyless-telemetry.ts (1)

55-57: Good use of atomic creation via 'wx'.

Using { flag: 'wx' } ensures only the first caller creates the flag file; subsequent attempts fail with EEXIST, which you already handle below.

try {
if (canUseKeyless) {
const { mkdir, writeFile } = nodeFsOrThrow();
const { mkdirSync, writeFileSync } = nodeFsOrThrow();
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify nodeFsOrThrow exposes sync variants in all runtimes.

We now rely on mkdirSync and writeFileSync. Ensure nodeFsOrThrow() always returns these in supported Next.js server runtimes (Node.js only), and its type definition reflects that.

Run this repo-scoped script to confirm the implementation and typings of nodeFsOrThrow:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Locate nodeFsOrThrow definition and ensure it returns mkdirSync/writeFileSync:"
rg -nP -C3 'export\s+(function|const)\s+nodeFsOrThrow\b|nodeFsOrThrow\s*\(' --type ts

echo
echo "Check for sync fs members in the returned object:"
rg -nP -C2 'mkdirSync|writeFileSync' --type ts | sed -n '1,200p'

Length of output: 10556


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Locate utils.ts in packages/nextjs/src/server/fs:"
fd --base-directory packages/nextjs/src/server/fs -t f utils.ts

echo
echo "Display first 200 lines of utils.ts to inspect nodeFsOrThrow definition and its typing:"
sed -n '1,200p' packages/nextjs/src/server/fs/utils.ts

Length of output: 1302


Enforce FS sync methods in types

At runtime nodeFsOrThrow() returns the real Node.js fs module (which includes mkdirSync, writeFileSync, etc.), so your usages in keyless-telemetry.ts are safe. However, because we import #safe-node-apis with @ts-ignore, nodeFsOrThrow() is currently typed as any and does not guarantee those sync methods at compile time.

To fully verify and enforce that callers always have access to the sync variants, please:

  • In packages/nextjs/src/server/fs/utils.ts, change the signature of nodeFsOrThrow to explicitly return Node’s fs type:
    // before
    const nodeFsOrThrow = () => {
      if (!nodeRuntime.fs) throwMissingFsModule('fs')
      return nodeRuntime.fs
    }
    // after
    const nodeFsOrThrow = (): typeof import('fs') => {
      if (!nodeRuntime.fs) throwMissingFsModule('fs')
      return nodeRuntime.fs
    }
  • Add a TypeScript declaration for #safe-node-apis so that its fs export is correctly typed. For example, create a packages/nextjs/src/server/fs/utils.d.ts (or in your global types) with:
    declare module '#safe-node-apis' {
      export const fs: typeof import('fs')
      export const path: typeof import('path')
      export const cwd: () => string
    }

These changes ensure the compiler will flag any missing sync methods immediately and align the types with the actual runtime behavior.

🤖 Prompt for AI Agents
In packages/nextjs/src/server/fs/utils.ts around the nodeFsOrThrow definition
(used by packages/nextjs/src/server/keyless-telemetry.ts line 47), the function
is currently untyped and returns any; change its signature to explicitly return
Node’s fs type (i.e., nodeFsOrThrow(): typeof import("fs")) and keep the
existing runtime checks, and add a TypeScript declaration file (e.g.,
packages/nextjs/src/server/fs/utils.d.ts) declaring module '#safe-node-apis'
that exports fs: typeof import("fs"), path: typeof import("path"), and cwd: ()
=> string so callers get proper typings and the compiler will enforce presence
of sync methods.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
.changeset/sweet-vans-wonder.md (1)

5-5: Make the release note more specific about the root cause and the fix.

Suggest explicitly calling out the async→sync switch and the affected telemetry to aid changelog readers.

-Fix keyless drift detection failure caused by invalid fs module methods.
+Fix KEYLESS_ENV_DRIFT_DETECTED not being sent due to use of async fs methods.
+Replaced fs.mkdir/fs.writeFile with mkdirSync/writeFileSync in the keyless telemetry flag writer.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 84e77e6 and b8b72af.

📒 Files selected for processing (1)
  • .changeset/sweet-vans-wonder.md (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
.changeset/**

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Automated releases must use Changesets.

Files:

  • .changeset/sweet-vans-wonder.md
🪛 LanguageTool
.changeset/sweet-vans-wonder.md

[grammar] ~5-~5: There might be a mistake here.
Context: ...ure caused by invalid fs module methods.

(QB_NEW_EN)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (24)
  • GitHub Check: Integration Tests (nextjs, chrome, 15)
  • GitHub Check: Integration Tests (sessions, chrome)
  • GitHub Check: Integration Tests (nextjs, chrome, 14)
  • GitHub Check: Integration Tests (react-router, chrome)
  • GitHub Check: Integration Tests (elements, chrome)
  • GitHub Check: Integration Tests (tanstack-react-router, chrome)
  • GitHub Check: Integration Tests (quickstart, chrome)
  • GitHub Check: Integration Tests (billing, chrome)
  • GitHub Check: Integration Tests (tanstack-react-start, chrome)
  • GitHub Check: Integration Tests (machine, chrome)
  • GitHub Check: Integration Tests (expo-web, chrome)
  • GitHub Check: Integration Tests (vue, chrome)
  • GitHub Check: Integration Tests (nuxt, chrome)
  • GitHub Check: Integration Tests (astro, chrome)
  • GitHub Check: Integration Tests (localhost, chrome)
  • GitHub Check: Integration Tests (generic, chrome)
  • GitHub Check: Integration Tests (express, chrome)
  • GitHub Check: Integration Tests (ap-flows, chrome)
  • GitHub Check: Static analysis
  • GitHub Check: Unit Tests (22, **)
  • GitHub Check: Publish with pkg-pr-new
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: semgrep-cloud-platform/scan
🔇 Additional comments (1)
.changeset/sweet-vans-wonder.md (1)

1-4: Changeset front-matter looks correct.

Package name and bump type are valid for a patch release.

@panteliselef panteliselef merged commit eb49928 into main Aug 21, 2025
39 checks passed
@panteliselef panteliselef deleted the heat/telemetry-flag-creation-warning branch August 21, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants