visual-studio-codesecurityenvironment-variablesgithub-copilot

How to Exclude Specific Files (like .env) from GitHub Copilot in VS Code?


I'm currently using GitHub Copilot in Visual Studio Code and am trying to find a way to prevent Copilot from accessing certain file types, specifically .env files, which contain sensitive information. I attempted to disable Copilot for these files by adding 'env' and '.env' to the list of disabled files in the Copilot settings, but this approach didn't work, presumably because these aren't recognized as distinct language tags in VS Code.

Has anyone successfully found a way to exclude specific files or file types from GitHub Copilot's analysis? If so, could you please share how you achieved this?


Solution

  • In your VS Code user settings.json file,

    1. Add .env* to the files.associations object
    2. Update the github.copilot.enable object

    Example:

    "files.associations": {
        ".env*": "dotenv"
    },
    "github.copilot.enable": {
        "*": true,
        // ...
        "dotenv": false,
    }
    

    To validate, open a test .env file. See that the Copilot icon has a slash over it.

    Consider the limitations from https://docs.github.com/en/copilot/managing-copilot-business/configuring-content-exclusions-for-github-copilot#limitations whether they apply, and if they do, how they may apply in this case.

    • Excluding content from GitHub Copilot currently only affects code completion. GitHub Copilot Chat is not affected by these settings.
    • Content exclusion prevents Copilot directly accessing the content of excluded files. Copilot may draw information about non-excluded files from semantic information provided by the IDE - for example, type information, or hover-over definitions for symbols used in code. It's possible that the IDE may derive this information from excluded files.