skip to content

GitHub Copilot ghost text in VS Code keeps proposing a library your project does not use — which documented levers steer it back?

level: middleimportance: should knowfreq 9%

answer

  1. what is on the desk
  2. current file and open tabs
  3. the import line speaks
  4. a pasted sample primes it
  5. instruction files do not reach it

basics

~20 s

Copilot builds ghost text from the current file and open tabs, so fix the editor: add the real import, open files using the right library, close unrelated ones, add a top comment or sample. Instruction files do not reach it.

solid answer

~40 s

VS Code's docs say inline suggestions draw on the **current and open files** in the editor; GitHub's responsible-use page adds that the code around the cursor is combined with snippets from open tabs. So the levers live in the editor: add the import or module reference you actually use (with no import the docs' example suggests `console.log`, with a `Log4js` reference it logs through Log4js), open a file that already uses the right library, close irrelevant ones, add a top-of-file comment, and if Copilot keeps writing an older API, paste a short sample of the newer one and delete it once suggestions follow. What does **not** help: custom instructions such as `.github/copilot-instructions.md` are not taken into account for inline suggestions, and a file under a content exclusion never informs suggestions in other files.

go deeper

for a junior

Recall that Copilot reads your current file and open tabs, so imports, nearby code and what you have open shape every suggestion.

for a middle

Explain each documented lever — imports, open and closed tabs, top comment, sample priming — and why instruction files and chat requests do not reach ghost text.

for a senior

Diagnose a team-wide wrong-library pattern: stale open files, old code the model latches onto, content exclusions, and instruction files mistakenly trusted to steer completions.

for a principal

Decide what the team writes down where: instruction files steer chat, agents and review, while consistent imports and current code samples are what steer completions.

## Where Copilot's ghost text gets its context GitHub Copilot's **ghost text** — the dimmed suggestion at the cursor — is generated from a prompt Copilot assembles automatically. Two pages describe what goes in it: - VS Code's inline-suggestions page: Copilot "looks at the **current and open files** in your editor to analyze the context". Having related files open helps it see a bigger picture of the project. - GitHub's responsible-use page for inline suggestions: the **surrounding code** at the cursor is combined with contextual information such as **code snippets from open tabs** and sent to the model. That is why a suggestion that keeps reaching for the wrong library is usually a context problem, and the fix is in what the editor shows Copilot, not in a setting. ## The levers the docs name 1. **Imports and references.** The VS Code prompt-engineering guide's example: with no imports, Copilot suggests `console.log` to log a value; once the file references `Log4js`, it suggests logging through that framework. The guide says to set the includes yourself because you know which dependencies — and versions — you want. 2. **Open the right files, close the wrong ones.** GitHub's prompt-engineering page: open any relevant files and close irrelevant files, because Copilot uses the open files to understand your request. A tab left open on an old module that uses the rival library keeps feeding it in. 3. **A top-level comment.** A short description at the top of the file tells Copilot what the file is for. 4. **Meaningful names and scoped comments.** A function called `fetchData()` tells Copilot little; a specific name and a focused comment narrow the body it writes. 5. **Prime with sample code.** Paste a small, correct example close to what you want, let Copilot pick up the pattern, then delete the sample. The guide calls this especially helpful to jumpstart Copilot to a **newer library version** when it defaults to older code. The guide's caveat applies throughout: Copilot latches on to the existing code, so "garbage in, garbage out" — a file full of the old pattern keeps producing it. ## Comments as an explicit hint When context alone is ambiguous, say it in the file. VS Code's inline-suggestions page shows Copilot generating a whole TypeScript class from a comment that lists its properties and methods, and suggests naming the approach in the comment — "use recursion", "use a singleton pattern". For the wrong-library problem the comment can name the library outright: a line such as "parse dates with the project's date helper" right above the function is read as part of the current file, unlike a rule kept in a separate instruction file. ## Levers that do not reach ghost text | What a developer tries | Does it change ghost text? | Why | |---|---|---| | Writing "use our HTTP wrapper" in `.github/copilot-instructions.md` | No | VS Code's docs: custom instructions are not taken into account for inline suggestions as you type | | Asking in Copilot Chat to prefer the library | Not a documented input | The inline-suggestion pages name the current file and open tabs, not chat history | | Running **Change Completions Model** | Not reliably | It swaps the model; the model still sees the same open files | | Keeping the right file open while it is under a **content exclusion** | No | Excluded content does not inform inline suggestions in other files | Instruction files are still worth writing for Chat, agents and code review — but a team that adds a "never use library X" rule and expects ghost text to obey it will be disappointed. ## A worked diagnosis A developer's suggestions keep importing a date library the service dropped last quarter. Going through the levers in order: - The file has no import yet, so Copilot falls back on whatever the open tabs show; two old test files with the retired import are open. - Closing those tabs and adding the current library's import line changes the next suggestion. - For an API whose newer version differs from the older one Copilot keeps producing, a five-line pasted sample, deleted afterwards, finishes the job. ## Limits worth stating - These levers **shift probabilities**; they do not guarantee a library. Reviewing what you accept stays your job. - The docs describe the inputs at the level above; they do not publish a ranking of how much each open tab counts, so do not claim one. - If suggestions stop entirely in some files rather than going wrong, the cause is different — per-language settings, Snooze or a content exclusion.

  • Copilot keeps writing the previous major version of a library's API. What does VS Code's guide suggest?
    Prime it: paste a small sample of code written against the newer API into the open editor, let suggestions start following it, then delete the sample. The guide names this as a way to jumpstart Copilot to a newer library version when it defaults to older suggestions. Adding the correct import helps too.
  • Why might keeping a reference file open fail to help when it sits in a sensitive folder?
    If that file is covered by a Copilot content exclusion, its content does not inform inline suggestions in other files, whether or not it is open. The status-bar icon in the excluded file shows a diagonal line, and hovering it names the setting that applied the restriction.

Copilot's ghost text is like a pair programmer who can only read the papers spread on your desk: put the right documents on it and clear the stale ones, because the team handbook in the drawer — your instruction file — is never read while you type.

  • Adding a library rule to .github/copilot-instructions.md fixes Copilot's ghost text.
  • Copilot ghost text only ever reads the line you are typing.
  • Open editor tabs have no effect on what Copilot suggests inline.
  • Changing the completions model is the main fix for a wrong-library suggestion.
  • An excluded file still informs suggestions as long as it is open in a tab.