Skip to content

[eslint-plugin-react-hooks] LRUCache.get() overwrites cached entries when promoting a non-head hit #38437

Backlog

Description

@Tortleya

Affected versions

I tested [email protected] (current npm latest) with [email protected] on Node 22.22.2 and Node 24.22.1. The same 10-entry cache (RunReactCompiler.ts:308) and shift loop (RunReactCompiler.ts:292-300) are present in React commit edae846a9a17442c00429f2b15375afbfa0376c9.

What happens

In LRUCache.get(), promoting a cached file at physical index idx !== #headIdx overwrites Math.min(idx, len - 1) slots behind #headIdx with copies of the previous head entry — and when idx === 9 (for example, revisiting the first cached file after 3–10 inserts), this collapses the 10-entry compiler cache down to just 2 unique files.

With three unchanged files (a.js, b.js, c.js), revisiting a.js hits the cache but evicts b.js, forcing b.js to recompile on its next lint. In practice this bites long-lived IDE/editor ESLint language servers and watch mode when switching between open tabs (whereas single-pass CLI linting runs all rules against one file in a row and mostly hits the #headIdx fast path).

Reproduction

In an empty directory, run npm install [email protected] [email protected]. Save this as cache.cjs and run node cache.cjs.

Here SourceCode.prototype.getAllComments acts as a proxy for compiler re-execution: runReactCompiler invokes getFlowSuppressions(sourceCode) (RunReactCompiler.ts:251), which calls getAllComments only on a cache miss, whereas cache hits return cached diagnostics directly without calling it.

const {Linter, SourceCode} = require('eslint');
const hooks = require('eslint-plugin-react-hooks');
const original = SourceCode.prototype.getAllComments;
let calls = 0;
SourceCode.prototype.getAllComments = function (...args) {
  calls++;
  return original.apply(this, args);
};
const linter = new Linter();
const config = {
  languageOptions: {parserOptions: {ecmaFeatures: {jsx: true}}},
  plugins: {hooks},
  rules: {'hooks/purity': 'error'},
};
const code = 'export function App() { return <div>{Math.random()}</div>; }';
for (const file of ['a.js', 'b.js', 'c.js', 'a.js', 'b.js', 'b.js']) {
  calls = 0;
  const diagnostics = linter.verify(code, config, file);
  console.log(file, calls, diagnostics.length);
}

Output

The columns are filename, getAllComments calls (1 = cache miss / compiler re-run, 0 = cache hit), and emitted diagnostic count (1 on every pass, including hits served from cache):

a.js 1 1
b.js 1 1
c.js 1 1
a.js 0 1
b.js 1 1
b.js 0 1

On the 4th pass (a.js), a.js hits the cache (0 calls, 1 cached diagnostic), but promoting a.js evicts b.js, causing b.js to recompile on the 5th pass (1 call). I expected 0 calls on all three revisits.

Cause and suggested fix

In RunReactCompiler.ts:292-300, LRUCache.get() has two compounding bugs when promoting a non-head hit:

  1. Forward shift overwrites unread slots: Copying forward from #headIdx destroys the entry immediately behind the head (#values[(this.#headIdx + 1) % len], here b.js) on iteration i = 0, then propagates the previous head entry across every subsequent slot (a, b, c, get(a) starts with #headIdx = 7 (c), 8 (b), 9 (a) and ends with {a at slot 7, c × 9}).
  2. Wrong loop bound (Math.min(idx, len - 1)): The trip count uses the raw physical index idx instead of the head-relative circular distance (idx - this.#headIdx + len) % len. Even if Bug Run each test in its own <iframe> #1 were fixed alone, whenever the matched entry sits at physical slot idx === 0 (#headIdx !== 0), Math.min(0, len - 1) is 0, so the loop runs zero times and #values[this.#headIdx] = entry immediately overwrites the MRU head entry without shifting it.

Shifting backward from the matched slot to #headIdx over the head-relative distance keeps all entries prior to idx in MRU order intact (including across wraparound):

const dist = (idx - this.#headIdx + len) % len;
for (let i = dist; i > 0; i--) {
  this.#values[(this.#headIdx + i) % len] =
    this.#values[(this.#headIdx + i - 1) % len];
}
this.#values[this.#headIdx] = entry;

(Alternatively, a small Map using delete-and-reinsert on hit and evicting keys().next().value on overflow avoids circular-index arithmetic altogether.)

For test coverage, the 6-pass sequence above reproduces Bug #1, and a unit test on LRUCache (or >10 files) should also cover circular wraparound and hits at physical slot idx === 0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions