GitHub Copilot in VS Code With Format-on-Save Enabled: Conflict Resolution
🔍 WiseChecker

GitHub Copilot in VS Code With Format-on-Save Enabled: Conflict Resolution

When you enable format-on-save in VS Code, GitHub Copilot suggestions can clash with the auto-formatter. The editor reformats your code the moment you save, which often overwrites Copilot-generated changes or triggers unexpected indentation and bracket placement. This conflict happens because both features try to modify the same text buffer at the same time. This article explains why the conflict occurs and provides concrete steps to resolve it without disabling either feature.

Key Takeaways: Resolving Format-on-Save and Copilot Conflicts in VS Code

  • VS Code settings.json > editor.formatOnSave: Set to true to enable automatic formatting on file save.
  • VS Code settings.json > editor.codeActionsOnSave: Disable source.organizeImports or source.fixAll if they interfere with Copilot completions.
  • GitHub Copilot extension settings > Editor > Inline Suggest: Enable: Keep set to true to allow Copilot to provide suggestions before format-on-save runs.

ADVERTISEMENT

Why Format-on-Save and Copilot Conflict in VS Code

The conflict stems from the order in which VS Code applies changes when you save a file. When format-on-save is enabled, VS Code runs the formatter immediately after the save command. At the same time, Copilot may still be providing inline suggestions or completing a multi-line snippet. The formatter then reformats the entire file, which can remove Copilot’s inserted code, change indentation, or add missing imports that Copilot had already placed. This race condition produces two common symptoms: Copilot-generated code disappears after save, or the formatter and Copilot fight over bracket placement, creating syntax errors.

The Role of the VS Code Language Server

VS Code uses a language server to provide code intelligence and formatting. The language server for each language, such as TypeScript or Python, has its own formatting rules. When format-on-save triggers, the language server sends a formatting request to the editor. Copilot, running as a separate extension, also sends text edits to the buffer. If both requests arrive at nearly the same time, the editor merges them in an unpredictable way. This is not a bug in either extension but a design limitation of how VS Code handles concurrent text edits.

Common Formatters That Cause Conflicts

Not all formatters behave the same way. Prettier, for example, reformats the entire file and is aggressive about removing trailing commas or adjusting line breaks. ESLint with --fix only applies rule-specific changes. The conflict is most pronounced with Prettier because it rewrites large sections of code. Python formatters like Black also reformat the whole file, causing similar issues.

Steps to Resolve the Conflict Between Format-on-Save and Copilot

The following steps let you keep both format-on-save and Copilot active while minimizing conflicts. Apply them in the order listed.

  1. Open VS Code Settings
    Press Ctrl+, to open the Settings editor. Click the Open Settings icon in the upper-right corner to switch to the JSON file view. This gives you full control over advanced settings.
  2. Set editor.formatOnSave to true
    Add or modify this line in settings.json: "editor.formatOnSave": true. This enables automatic formatting every time you save a file.
  3. Disable codeActionsOnSave that modify imports
    Add this block to settings.json: "editor.codeActionsOnSave": {"source.organizeImports": false, "source.fixAll": false}. These actions often conflict with Copilot because they reorganize imports immediately after the formatter runs.
  4. Enable Copilot inline suggestions
    Open the Extensions view, find GitHub Copilot, and click the gear icon. Select Extension Settings. Ensure Editor > Inline Suggest: Enable is checked. This keeps Copilot active even when format-on-save is enabled.
  5. Set a delay for format-on-save
    Add "editor.formatOnSaveTimeout": 2000 to settings.json. This gives VS Code 2 seconds to finish Copilot suggestions before the formatter runs. Adjust the value upward if conflicts persist.
  6. Restart VS Code
    Close and reopen VS Code to apply all changes. Test by typing a line of code and pressing Ctrl+S. The formatter should run after Copilot has finished its suggestion.

Alternative Method: Use a Keyboard Shortcut Instead of Auto-Format

If the delay method does not resolve the conflict, disable format-on-save entirely and use a manual shortcut. Set "editor.formatOnSave": false and press Shift+Alt+F to format only when you choose. This gives Copilot full control over the buffer until you decide to format.

ADVERTISEMENT

If Copilot Still Has Issues After the Main Fix

Even after applying the steps above, some users experience residual conflicts. Here are the most common patterns and their specific fixes.

Copilot Suggestions Disappear After Save

This happens when the formatter runs before Copilot finishes writing to the buffer. Increase the editor.formatOnSaveTimeout value to 3000 or 5000 milliseconds. If the problem remains, check whether another extension like ESLint or Stylelint also runs on save. Disable those extensions temporarily to isolate the cause.

Format-on-Save Adds Extra Blank Lines After Copilot Completions

Some formatters, especially Prettier, insert blank lines after certain constructs like function declarations. This is a formatter rule, not a Copilot issue. Adjust the formatter configuration. For Prettier, add a .prettierrc file with "insertFinalNewline": false or modify the printWidth setting to reduce line wrapping.

Copilot Stops Providing Suggestions After Format-on-Save Runs

This can occur if the language server crashes after a formatting request. Open the VS Code Developer Tools with Help > Toggle Developer Tools. Look for error messages in the Console tab related to the language server. Restart the language server by running the command Developer: Reload Window With Extensions Disabled and then re-enable extensions one by one to find the culprit.

Format-on-Save Delay vs Manual Format: Key Differences

Item Format-on-Save With Delay Manual Format (Shift+Alt+F)
Automation Fully automatic on save Requires manual trigger
Conflict risk with Copilot Low with timeout set to 2000ms or higher None, because formatting runs after Copilot is done
Workflow impact Seamless, no extra action needed Adds one extra keystroke per file save
Best for Teams that enforce consistent formatting on every commit Developers who want full control over when formatting applies

The conflict between GitHub Copilot and format-on-save in VS Code is manageable with the right settings. Use the delay method to keep both features active, or switch to manual formatting if conflicts persist. After applying the settings, test with a short code snippet to confirm that Copilot suggestions remain intact after save. For advanced control, consider adding language-specific overrides in settings.json so that format-on-save applies only to certain file types like JavaScript or Python.

ADVERTISEMENT