Repository navigation
fix(access): cut the @/plugins/access barrel out of client code - #772
Conversation
… barrel Nine modules a browser executes imported `@/plugins/access`, and that barrel reaches `@/lib/env/server` through `accessPlugin → fieldAccess → accessConfigs → @/lib/utilities/previewSecret → @/lib/env`. Twelve of the 14 browser entries that reached the server module did so this way — the #633 shape, in a second barrel. Nothing failed at runtime: `@/lib/env/server` imports only zod and parses lazily, so the deploy survived. What it cost was the access plugin's whole runtime — `accessPlugin`, `fieldAccess`, `accessConfigs`, `permissions` — downloaded by every admin page. `@/plugins/access/config` re-exports exactly the helpers these nine want and is pure: `config/projects.ts`, `config/roles.ts` and `types.ts` between them import nothing but types. It is a documented barrel of its own, so this is not a deep import into the plugin's internals — which was the objection recorded in `ProjectSelector/utils.ts`. `buildStatusGlobalConfig` takes `./adminOnly`, equally pure. Nothing under `src/plugins/access/` changes. Measured over all 92 browser entries, this takes the count reaching `lib/env/server.ts` from 14 to 2, and both of those are false positives the next commit removes. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
`client-bundle-safety.spec.ts` has been green since #633 with the defect the previous commit fixes sitting in front of it, for two independent reasons. It walked four hand-written entries, so 88 browser entries had no guard at all. And it matched the import **specifier** a caller wrote, while the edge into `lib/env/server.ts` is `@/lib/env`'s own `./server` — a specifier no caller writes and no list can enumerate. Both halves are needed. Derived entries alone still find nothing, because the matcher never sees the edge. File matching alone still misses every entry outside the list of four. So the walk now resolves each specifier before offering it, and passes the resolved file alongside it. `SERVER_ONLY` holds src-relative file paths and matches those; the failure message still reports the specifier chain, which is what names the import to cut. Entries come from `clientEntries()`, the same derivation `public-env-substitution.spec.ts` already used. The walk also stops at a `'use server'` module. Next compiles one to a client reference — the body never enters the browser bundle — so the two form/action chains it used to report were false positives. `KNOWN_OFFENDERS` in `public-env-substitution.spec.ts` is now empty: its one entry was `lib/env/server.ts`, which no browser entry reaches. Each of the three changes was verified by reintroducing the defect it prevents and watching the spec go red — including the third, which goes GREEN with the barrel import restored if the matcher reverts to specifiers. That vacuity is what this commit removes. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
`src/AGENTS.md` told the reader to add new client entry points to `client-bundle-safety.spec.ts`'s list. There is no list any more, and the instruction would have read as still current. Records what the guard's two defects were, since a graph guard tends to have exactly those two, and the failure is invisible — CI does not build this app. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
Four findings from the review pass, all inside what this branch already touched: - `ProjectSelector/utils.ts` borrowed `ContentSlug` through `Parameters<typeof isCollectionVisibleInProject>[0]` to avoid a deep import. This branch makes the deep import the house style, and `@/plugins/access/types` exports `ContentSlug` directly. Six lines of comment and an indirection go with it. - One `hasDirective(file, directive)` replaces two copies of the leading-comment-tolerant directive regex. `'use client'` picks the entry set and `'use server'` prunes the walk, so a fix to one that misses the other makes them disagree about the same file. - `importGraph` reads each file once per process and memoizes `clientEntries()`. A 92-entry walk was 563 reads; it is now 148, and the two guard specs run 347ms → 271ms of test time. The walk revisits the same module from every edge and `src/` cannot change mid-run. - `SERVER_ONLY` is keyed by absolute path, so the per-edge callback no longer calls `relative()` on every traversed edge. Also pins the `'use server'` stop, which was the one new way for the walk to end early and the one thing with no test. Two controls, proven to fail in both directions: drop the stop and a client entry reports its server action's chain; widen it to the entry and a server action walked in its own right reports nothing. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
`SERVER_ONLY` named `@/jobs/VerifyEmbeds/browserRendering`, and the module has been at `src/lib/embedVerification/browserRendering.ts` since it moved. So one of the guard's four rows matched nothing at all, from #633 until now, and the previous commit carried the stale name over into the new file matcher unchanged. Nothing else would ever say so — an unmatchable key looks exactly like a rule nobody breaks. A new control walks the map and fails on any key with no module behind it; it goes red on the old name. Also corrects the derived-entries control, which claimed to cover the four entries this spec used to list and named three. `CanonicalEmbedPicker/model.ts` carries no directive, so it is not an entry and never was — `public-env-substitution.spec.ts` already pins it as a module the walk reaches. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01A8FsJqcyLKyWD3MX6SmsNf
|
🚅 Deployed to the SahajCloud-pr-772 environment in sahajcloud
1 service not affected by this PR
|
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
The diff is the shape #770 asked for — ten one-line import moves onto pure leaves, one matcher change, no src/plugins/access/ edit — and 0bc8540 fixes a real dead guard row. Two claims stop short: src/AGENTS.md calls the new barrel rule enforced, yet the barrel is caught only by happening to reach lib/env/server.ts; and hasDirective now picks the entry set for both guards, so one file can leave both silently.
Findings:
should-fix— the barrel rule is enforced only incidentally · spec:40should-fix— a//before'use client'drops a file from both guards · importGraph:60consider— the #633→#770 story is told in four places · AGENTS.md:142consider— two new controls duplicate cases above · spec:107
Checked: #770, the PR body, the diff. Read whole: importGraph.ts, both guard specs, both barrels. Confirmed the guard's chain is live (lib/env/index.ts runtime-exports ./server), browserRendering.ts exists at its new path, and VerifyForm.tsx is itself an entry. No cross-repo surface.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
| const verifyAction = 'app/(frontend)/events/verify/actions.ts' | ||
|
|
||
| it('does not follow a client entry into a server action', () => { | ||
| expect(findServerOnlyImport(join(SRC, 'app/(frontend)/events/verify/VerifyForm.tsx'))).toBeNull() |
There was a problem hiding this comment.
consider — this control cannot fail on its own.
VerifyForm.tsx carries 'use client', so it is already an entry in the it.each above. Drop the 'use server' stop and both go red together. This case earns its place only on the day that file loses its directive.
Same question on line 74: entries.length > 50 is already asserted on the same shared clientEntries() at public-env-substitution.spec.ts:113. The arrayContaining half is what actually pins this PR's swap.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
There was a problem hiding this comment.
Both kept, with evidence — your call.
The 'use server' pair is the only place the stop's direction is stated, and its redundancy holds only while VerifyForm.tsx keeps 'use client'. Nothing asserts that.
entries.length > 50 guards this spec's it.each from running zero cases. public-env-substitution.spec.ts:113 cannot do that job, since either file can be deleted alone.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
`src/AGENTS.md` states that client code must not import the `@/plugins/access` barrel, and called that rule enforced. It was not enforced directly: the barrel was caught only because it happens to reach `lib/env/server.ts` through `accessConfigs → previewSecret → @/lib/env → ./server`. Cutting that one edge — a live alternative fix named in #770's own notes — would have left the barrel green while the browser still downloaded `accessPlugin`, `fieldAccess`, `accessConfigs` and `permissions`, which is the cost the ticket is about. `plugins/access/index.ts` is now a `SERVER_ONLY` row in its own right, so the documented rule and the guard say the same thing. Verified by reintroducing `from '@/plugins/access'` in `ProjectSelector/utils.ts` with the `lib/env/server.ts` row removed: three entries go red naming the barrel. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WQJWHhheKp2mGUWNM8dyYw
`hasDirective` tolerated a leading block comment but not a `//` line comment, so a file written that way was not a `'use client'` entry. That is not one weakened guard: the file leaves `clientEntries()`, so `client-bundle-safety` and `public-env-substitution` both go quiet about it at once, and `entries.length > 50` cannot see one file leave. The scan now strips any run of leading comments, in a loop rather than a regex — the nested quantifier for "any run of either comment form" backtracks badly on a long file that never matches. A new control cross-checks the result against TypeScript's own parse of the directive prologue, which is the independent oracle the entry set lacked. Verified both ways on `UserMessageStatus.tsx` with a `//` line added above its directive: green with the new scan, and with the old regex restored the control goes red naming the file. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WQJWHhheKp2mGUWNM8dyYw
`src/AGENTS.md` says it is the one home for the bundle-guard story, and that code sites carry a one-line pointer to it. This branch retold the #633/#760/#770 history at four code sites as well, which is what that rule exists to prevent — five copies to update when the story changes. Each site keeps the part a reader needs there (why files and not specifiers, why the entry set is derived, what a dead `SERVER_ONLY` row is) and drops the history behind a pointer. The spec's own header keeps its incident, since that states what the guard is for rather than retelling the rule. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WQJWHhheKp2mGUWNM8dyYw
|
Three findings adopted, one argued. Pushed
Each adoption was verified by reintroducing the defect it prevents — two new rows in the PR body. Lean gate green: 1871 unit tests. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
|
@sydevs-bot Would it make sense to add a specific client-safe export barrel to the access plugin? eg. Or is that unnecessary overhead? |
|
Recommend against it — your call. The rule points the other way. Those leaves' other importers are server code. It buys no enforcement. The guard walks every browser entry and fails on any reaching the barrel or
Say the word and I will add it — one commit here. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
`main` #772 replaced the hand-written client-entry list in `tests/unit/client-bundle-safety.spec.ts` with a derived set — `clientEntries()` over every `'use client'` file. This branch had added `components/admin/PreviewTarget/PreviewTarget.tsx` to that list. Took the derived set. `PreviewTarget.tsx` carries a leading `'use client'` directive, so it is an entry automatically, and the spec's own `misses no file TypeScript reads as a client entry` case proves that rather than assuming it. The per-entry comment went with the list: the rule it recorded — `@/fields/previewTargetField` is types and a plain object literal so the declaration can cross to the browser — is already stated in that module's own header. Nothing else conflicted. The branch's new code imports no `@/plugins/access` barrel, the new `SERVER_ONLY` row #772 added. Gate after the merge: lint, typecheck, typecheck:tests, 1952 unit tests, plus `translations-globals` and `translations-field` (60 tests) — all green. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_0158CavRbZEnPZhmp4fnCwa5
Summary
@/plugins/accessbarrel, which reached@/lib/env/serverand shipped the whole access-plugin runtime to every admin page.@/plugins/access/config(or./adminOnly,./types). Nothing undersrc/plugins/access/changes.SERVER_ONLYrow in its own right, so the documented rule holds even if the barrel stops reaching@/lib/env/server.Changes
src/**(9 files) — one import line each, onto a pure leaf.tests/utils/importGraph.ts—onSpecreceives the resolved file; the walk stops at'use server'; file reads andclientEntries()are memoized; the directive scan strips any run of leading comments.tests/unit/client-bundle-safety.spec.ts— entries fromclientEntries(),SERVER_ONLYkeyed by file and carrying the access barrel, five new controls.tests/unit/public-env-substitution.spec.ts—KNOWN_OFFENDERSemptied.src/AGENTS.md— the client-side import rule, stated as a rule.Preview
https://sahajcloud-sahajcloud-pr-772.up.railway.app
Test results
pnpm lint && pnpm typecheck && pnpm typecheck:tests && pnpm test:unit(1871 unit tests)app-status-readiness.int.spec.ts+role-based-access.int.spec.ts— 68 passedpnpm build— the check that matters here, since CI does not build and this change is about bundlingManual verification
/admin, switch project in the selector, and load a collection list — the switch, the nav, the logo and the permissions table all read from the moved imports.The risk is low by construction:
src/plugins/access/index.tsre-exports these symbols from./config,./adminOnlyand./typesverbatim, so every moved import resolves to the same module object it did before.Notes for reviewer
/simplifypass, one defect the guard review turned up, and three from the adversarial review (below).SERVER_ONLYhad a fifth way to be vacuous, and this branch nearly shipped it. One of its four rows named@/jobs/VerifyEmbeds/browserRendering; that module has been atlib/embedVerification/since it moved, so the row matched nothing from feat(access): add embed metadata and canonical ownership to Clients #633 until now.0bc8540corrects it and adds a control that fails on any key with no module behind it.//gap is closed, not deferred.dfdb1a5–910d55aanswer the review: the barrel is guarded directly, the directive scan reads behind any leading comment, and TypeScript's own parse is the entry set's independent oracle. The one finding left open isspec:108— two controls the review calls duplicative, argued in the thread.@/lib/statusis the same barrel shape (ReadinessGrouppulls nine modules through it for one function), andSERVER_ONLYis still hand-maintained.securityReview.triggerPattern, so the security review was skipped.Detail
Why the barrel and not
previewSecret. CuttingaccessConfigs → @/lib/utilities/previewSecretclears the same 12 chains and is the worse fix:accessConfigs.ts:110needshasValidPreviewSecretat runtime, so it means redesigning howpreviewSecret.tsreaches its secret — a design decision insidesrc/plugins/access/, which is on this repo's security-review trigger. And it only satisfies the guard; the browser would still downloadaccessPlugin,fieldAccess,accessConfigsandpermissions.The deep imports are not new house style.
@/plugins/access/adminOnlyis already imported that way bysrc/fields/slugField.ts, and@/plugins/access/typesbysrc/plugins/openapi/specFilter.ts.Each change was verified by reintroducing the defect it prevents.
from '@/plugins/access'inProjectSelector/utils.ts./server'use server'stopVerifyForm.tsxandFeedbackForm.tsxSERVER_ONLYto specifiers, keepclientEntries()jobs/VerifyEmbeds/keylib/env/server.ts, import the barrel fromProjectSelector/utils.ts@/plugins/access//line aboveUserMessageStatus.tsx's directive, with the old regexclientEntries()Measurements, walking the real graph from all 92
'use client'files plusinstrumentation-client.ts:lib/env/server.tswhen matching resolved files, 0 when matching specifiers.'use server'action modules.'use server'stop: 0.src/.Why
'use server'pruning is correct, not a convenience. Next compiles such a module to a client reference — an id the browser posts back — so its body never enters the browser bundle. That makes it neither a bundle-safety offender nor a place a browser readsprocess.env, which is why both consumers of the walk want it.Why a loop, not a widened regex. The directive can sit behind a licence block, a
//note, or both in either order. The nested quantifier that expresses "any run of either" backtracks badly on a long file that never matches, and this scan runs over every file undersrc/.ContentSlug.ProjectSelector/utils.tsborrowed it asParameters<typeof isCollectionVisibleInProject>[0]to avoid a deep import. This branch makes that import ordinary, and@/plugins/access/typesexports the type directly, so the borrow and its comment are gone.Closes #770
🤖 Written by the sydevs autonomous loop — see what this is
🤖 Generated with Claude Code
https://claude.ai/code/session_01WQJWHhheKp2mGUWNM8dyYw