GitHub Copilot Chat, What Why Don't You Know About My Selected File?

The Change In Behavior

GitHub Copilot is a fantastic tool. I have used it since the closed beta, and it has become part of my daily process. I enjoy opening the GitHub Copilot Chat view after working on a file and asking, "Is there any other suggested refactor to be made?" Giving a "second pair of eyes" on my code to give me new ideas on writing cleaner code. But as of late, something weird has been happening. After selecting the file I have been working on and going to the GitHub Copilot Chat view to ask my usual questions, it doesn't know anything about my selected code; it gives general answers and nothing specific to my actual code, or worse.

What the heck happened? Since I had no clue what was happening, I would select my code changes as a temporary solution and use the Copilot Chat options that would appear in the editor window. This works in the short term but becomes more cumbersome when getting suggestions for an entire file.

The Solution

It turns out that the default behavior of automatically including a selected Windows code in the context of Copilot Chat was changed between the beta release and general availability. That explains a lot...

How do you go back to the previous behavior? Use the context variables. Here is the documentation for each one directly from GitHub Copilot Chat docs:

The #selection context variable enables you to focus Copilot's suggestions on the specific text you select in the editor.

The #file variable lets you reference specific files from your workspace in your chat prompt. This helps make the answers from Copilot Chat more relevant to your code by providing context about the file you are working with. You can ask questions like "Can you suggest improvements to #[file:package.json](file:package.json)?" or "How do I add an extension in #[file:devcontainer.json](file:devcontainer.j..?". By using the #file variable, you can get more targeted and accurate responses from Copilot.

With the #editor context variable, you have control over whether to include the visible code of the active editor in your prompt to Copilot Chat. Previously, this information was automatically included when you hadn't selected text in the editor. Now, you can choose to explicitly add the visible code to the context or omit it for more general questions.

Given this newfound information, I need to tweak my prompt to include the #editor context variable to get the old behavior back, "#editor Is there any other suggested refactor to be made?"

And just like that, I can return to my old workflow by asking Copilot Chat to grill my entire file!

Hopefully, this little guide helps you out if you were an early adopter of Copilot Chat like me, and if you are a new user, maybe you learned something new.

Happy Coding!