Question

Claude Code re-implements helpers that already exist in my repo

Solved · 236 viewsasked by rosa_m

It started as one page and now it's a real app. Claude Code keeps re-implementing helpers that already exist (formatDate, useUser) because it doesn't see them, so I have three versions of everything.

What I’ve tried
  1. Wrote a CLAUDE.md with the folder structure
  2. Asked it to read the whole repo first (it runs out of context)
Comment

3 answers

Marked as helpful by the asker
felix_codes

Reading the whole repo doesn't scale. What works for me:

  • A short section in CLAUDE.md: "Before writing a helper, grep lib/ for an existing one." Claude Code actually runs the grep.
  • Keep lib/index.ts that re-exports everything. One file to read, and it's the map of what exists.
  • Once a week ask it: "list duplicate helpers in this repo and merge them". Cheap cleanup.

The root cause is that it starts each session blind. Give it a map, not the territory.

Comment
The grep rule works. This morning it found formatDate in lib/ on its own instead of writing a fourth one. rosa_m
olu_backend

Also worth it: npx knip finds unused and duplicate exports. Paste the output into the chat and ask it to fix exactly that list.

Comment
sergio_ruiz

Complementary trick: make the duplicates fail loudly instead of hoping the model remembers. A lint rule that bans imports from anywhere except lib/ for the helpers you care about:

// eslint.config.js
rules: {
  'no-restricted-syntax': ['error', {
    selector: 'FunctionDeclaration[id.name=/^(formatDate|useUser)$/]',
    message: 'Use the existing helper from lib/.'
  }]
}

Claude Code runs the linter, sees the error, and fixes it itself. A rule it can hit beats a rule it has to remember.

Comment
That's a clever one. Adding it for the three helpers that keep coming back. rosa_m