Skip to content

fix(reference): stop attributing overwritten require members to Node - #3801

Merged
kaoru0822-kitauji merged 2 commits into
mainfrom
fix/3393-overwritten-require-members
Oct 15, 2026
Merged

kaoru0822-kitauji merged 2 commits into
mainfrom
fix/3393-overwritten-require-members

Conversation

@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator

Fixes #3393

Root cause

unshadowedReferenceSourceRequireCalls (src/domain/referenceSourceRequireBindings.ts) decides which calls are Node's loader. For require.resolve(...) and require.main(...) it checked only the require binding: unbound or wrapper-owned, and never reassigned. It never checked whether the member had been replaced. After require.resolve = () => "custom", the literal argument still became a parsed internal requires edge, although the call never resolves a module.

Change

Member calls are attributed only while the loader's resolve and main members keep Node's identity (requireLoaderMembersIntact). Their identity is unknown, and the member calls are not attributed, when either holds:

  • A write to require.resolve, require.main or a computed require[...]. Write sites come from the shared semanticWriteSiteTarget / semanticWriteTargets helpers, so assignment, compound assignment, update, delete, for…in/of and destructuring targets are all covered.
  • require used as a value other than a direct call, a member read or typeof, for example Object.defineProperty(require, …), Object.assign(require, …) or const r = require. A mutator or alias can then replace members without a visible member write.

This matches how the boundary already excludes shadowed or reassigned loaders. Bare require("…") calls are unaffected.

Validation

The issue's repro and controls, public CLI, main vs this PR:

main.cjs main this PR
require.resolve = () => "custom"; require.resolve("./dep.cjs") requires dep.cjs (false) none
Object.defineProperty(require, "resolve", …); require.resolve("./dep.cjs") requires dep.cjs (false) none
patch(require); require.resolve("./dep.cjs") requires dep.cjs none (member identity unknown)
require.resolve("./dep.cjs") (control) requires dep.cjs requires dep.cjs
patch(require); require("./dep.cjs") (bare call) requires dep.cjs requires dep.cjs

Real sources, main vs this PR: relationships are identical and the rest of the graph is byte-identical. node_modules/pino has 575 relationships (0.55 s / 0.53 s), playwright-core/lib 1,362 (2.24 s / 2.26 s) and REA src/ 11,637 (2.97 s / 2.99 s). Output size is unchanged.

Tests:

  • New does not attribute overwritten require members to Node's resolver in referenceSourceShadowedRequire.test.ts covers assigned, defineProperty and computed-key overwrites plus a control. Each is first executed under Node to establish the oracle (custom or string), and only the control keeps its edge. The test fails on main; I checked by restoring main's classifier and rebuilding.
  • All 22 reference-source boundary test files pass (154 tests), along with the reference domain unit tests and npm run check:fast.

🤖 Generated with Claude Code

import-reference-source treated require.resolve(...) and require.main(...)
as Node's resolver even after the file replaced that member, so a literal
argument became a parsed internal dependency that the source never loads.

Attribute member calls only while the loader's resolve and main members
keep Node's identity: no write to either member or a computed member
(assignment, update, delete, destructuring) and no use of require as a
value where a mutator or alias could replace them. Bare require(...) calls
are unaffected.

Fixes #3393

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji kaoru0822-kitauji added the track:perf Performance track PRs (issues & perf loop) label Oct 15, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-15T01:59:34.925421Z 60424ab PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; both previous findings are addressed.

Summary

The PR stops treating overwritten require members as Node's loader.

  • Calls through require.resolve and require.main count only when their members stay intact.

Reviews (2) · Last reviewed commit: "fix(reference): keep optional and wrappe..." · Reviewed by Greptile

An optional call require?.(...) or a TypeScript or parenthesis wrapper
around require was read as require escaping as a value, which disabled
every member-call link in the file. Look through inert wrappers and
accept optional calls before deciding the loader escaped, and add
controls for bare calls after a member overwrite, a local loader's
member write, and optional calls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working track:perf Performance track PRs (issues & perf loop)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Overwritten require.resolve produces a false reference-source dependency

2 participants