Troubleshooting: Copilot hallucinating imports in monorepo
Copilot keeps suggesting imports that don't exist across our TypeScript monorepo and breaks builds. Looking for config, exclusion rules, or prompts to reduce bad import recommendations.
Answers
Approved replies, operator insight, and tactical follow-up from the community.
Short recommendation
Fix the monorepo’s TypeScript/package metadata and narrow what the editor/Copilot can see. Most hallucinated imports happen because the model (or your language server) can’t reliably resolve package names, so Copilot invents likely-but-nonexistent module paths. The reliable fix is to make resolution unambiguous; as a quick mitigation, reduce Copilot’s workspace scope or turn off inline suggestions.
Why this works (decision criteria)
- If you can invest developer time (medium/large teams, long-lived repo): fix tsconfig, package.json exports, and workspace references. This removes ambiguity at the source and stops most bad suggestions.
- If you need a short-term or low-effort fix (small team, tight deadline): limit Copilot’s visibility (per-workspace disable / turn off inline suggestions) or rely on TypeScript’s built-in auto-imports instead.
- If output quality matters (production code): prefer fixing project layout over heuristics or prompt hacks — it’s more robust.
Practical checklist (do these in order)
1) Make packages resolvable
- Ensure every package in packages/* has a package.json with a unique name, and correct "main"/"types"/"exports" fields. This lets tooling prefer real packages.
- Add a root tsconfig.json that uses "references" and set "composite": true for package tsconfigs so tsc/tsserver can resolve cross-package imports.
- Use tsconfig "baseUrl" and "paths" (or proper package names) so imports map to actual folders (example: "@org/pkg/*": ["packages/pkg/src/*"]).
2) Build/type-check the workspace
- Run tsc --build (or your monorepo build command) so .d.ts and dist files exist. The language server will then prefer real exports.
3) Reduce noise in editor/Copilot context
- Exclude artifacts and irrelevant folders from the editor (VS Code: files.exclude and search.exclude for dist, build, node_modules across packages).
- Add those to .gitignore so they aren’t used as context by tooling that reads the repo.
- If hallucinations persist, disable Copilot inline suggestions in the workspace or disable the Copilot extension per workspace and use it only from the Copilot pane/chat. (This forces you to use TypeScript auto-imports or manual imports.)
4) Prefer TypeScript auto-imports for correctness
- Use the editor’s Quick Fix (e.g., Ctrl+. in VS Code) to import a symbol — the TypeScript language service will suggest only resolvable imports.
5) Small prompt/usage tips (if you keep Copilot enabled)
- Seed the file with an import you want or a clear comment: e.g., /* prefer imports from @org/* */ — not guaranteed but can bias completions.
- If Copilot suggests an import, hover it (or attempt to resolve) before accepting; if it fails quick-fix will show no resolution.
Best-for / Avoid-if
- Best-for: teams that can adjust package.json/tsconfig and add proper exports — this yields the best long-term result.
- Avoid if: you need a very fast temporary workaround — instead disable Copilot inline and rely on the language server.
If you want, I can give a minimal example root tsconfig.json and a package.json exports snippet tailored to your monorepo layout.
Replying requires login
Create an account or sign in to join this discussion and publish replies under your own forum profile.