Repository navigation
fix(kubernetesjs): resolve node:fs at runtime so the ops-dashboard bundle builds - #42
Conversation
PR #40 made @kubernetesjs/ops import kubernetesjs's APIClient, whose static require('node:fs') then reached the ops-dashboard client bundle and failed next build with UnhandledSchemeError. process.getBuiltinModule keeps fs out of the bundler graph, and also makes the ESM build read the service-account token (require is undefined there, so it always returned undefined).
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Review complete. 🟠 1 high 💬 Inline comments (1)
The change introduces a
The intent (avoiding bundler errors on Reviewed commit: fc68797 |
There was a problem hiding this comment.
This PR replaces the static require('node:fs') in the in-cluster config path of packages/kubernetesjs/src/client.ts with a process.getBuiltinModule?.('node:fs') helper so browser bundlers don't try to resolve node:fs, but it silently breaks token/CA reads on Node versions older than v20.16/v22.3.
Key findings
- 🟠
nodeFs()breaks in-cluster token reads on older Node — client.ts:7
| function nodeFs(): typeof import('node:fs') { | ||
| const fs = typeof process !== 'undefined' ? process.getBuiltinModule?.('node:fs') : undefined; | ||
| if (!fs) { | ||
| throw new Error('APIClient: node:fs is not available in this runtime'); | ||
| } | ||
| return fs; | ||
| } |
There was a problem hiding this comment.
🟠 bug · high
nodeFs() breaks in-cluster token reads on older Node
The new nodeFs() helper resolves fs solely via process.getBuiltinModule?.('node:fs') (packages/kubernetesjs/src/client.ts:8), an API that only exists from Node v22.3.0 (backported to v20.16.0); the removed code used require('node:fs'), which worked on all versions. On Node 18.x and Node 20.0–20.15 the optional-chain yields undefined, so nodeFs() throws — and both readServiceAccountToken (line 165) and getServiceAccountCAPath (line 179) catch that throw in the same bare catch designed for ENOENT and return undefined. In-cluster auth therefore silently degrades, and getInClusterConfig (line 199) reports a misleading "service account token not found. Are you running inside a Kubernetes pod?" even inside a real pod. There is no engines field declaring the new runtime floor, so the regression is silent.
📋 Prompt for AI Agents
In packages/kubernetesjs/src/client.ts lines 7-13, make nodeFs() resilient on Node versions before v20.16/v22.3 where process.getBuiltinModule does not exist: try process.getBuiltinModule?.('node:fs') first, and when it returns undefined fall back to require('node:fs') guarded by typeof require === 'function' (kept dynamic so bundlers still do not statically see a node:fs import), throwing the 'node:fs is not available' error only if both paths fail. Alternatively, if dropping older Node is intentional, add an engines field declaring node >= 20.16 to packages/kubernetesjs/package.json so the requirement is explicit instead of failing silently at runtime.
Summary
Broken today:
Dashboard E2E Testsonmainfails atBuild all packages(run). #40 made@kubernetesjs/opsreuse theAPIClientfromkubernetesjs. That client has a staticrequire('node:fs')(inreadServiceAccountToken/getServiceAccountCAPath), and it now gets pulled into the ops-dashboard client bundle:#40 itself didn't trip this: the dashboard workflow only runs when
apps/ops-dashboard/**changes, andpnpm buildskips the dashboard. The next release commit bumped the dashboard's version, which ran the workflow and exposed the failure.Change: a small
nodeFs()helper loadsfsat runtime throughprocess.getBuiltinModule('node:fs'), so bundlers never see the import. Outside Node it throws, and the existingtryturns that intoundefined. The "Node.js only" contract is unchanged.Side effect (bug fix):
requireis undefined in the ESM build, soreadServiceAccountTokenalways returnedundefinedthere. It now reads the token.Verified locally (Node 24, pnpm 10.12.2):
pnpm build:all: before the fix it fails with the sameUnhandledSchemeError; after, it prints✓ Compiled successfullyand exits 0.readServiceAccountToken('/tmp/satok'): CJS returnstok123(a missing file returnsundefined). ESM returnedundefinedbefore the fix andtok123after.process.getBuiltinModuleneeds Node ≥ 20.16 / 22.3. Node 18 and 20 are EOL, and CI runs the latest 20.x.Link to Devin session: https://app.devin.ai/sessions/ce051f98f6454105bacf2a400f950e64
Open in Devin Desktop: https://app.devin.ai/desktop/session/ce051f98f6454105bacf2a400f950e64?variant=devin
Requested by: @pyramation