How to Use GitHub Copilot With Bazel Build Files in Polyglot Repos
🔍 WiseChecker

How to Use GitHub Copilot With Bazel Build Files in Polyglot Repos

Polyglot repositories that use Bazel as their build system require developers to write and maintain BUILD and .bzl files for each language and target. These files have a strict Starlark syntax that can be tedious to type manually, especially when the repo contains dozens or hundreds of targets. GitHub Copilot can generate Bazel rules, dependencies, and macros directly from natural language comments and existing file context, reducing boilerplate and syntax errors. This article explains how to configure Copilot for Bazel files, write effective prompts, and avoid common pitfalls when working with multi-language repositories.

Key Takeaways: Using Copilot with Bazel in Polyglot Repos

  • Copilot settings > Enable for Starlark files: Add Starlark file extensions to Copilot’s recognized languages so it provides completions in BUILD and .bzl files.
  • Natural language comments in BUILD files: Write a comment like # python_library for data processing above a target definition to trigger rule suggestions.
  • Multi-language target generation: Use a single comment to request rules for multiple languages in one file, for example # go_binary, java_library, py_binary.

ADVERTISEMENT

How Copilot Interprets Bazel BUILD and .bzl Files

Bazel uses Starlark, a Python-like domain-specific language, to define build targets in BUILD files and macros in .bzl files. Copilot’s model is trained on a large corpus of open-source Bazel repositories, so it can recognize common patterns such as go_library, java_binary, py_test, and cc_library. However, Copilot does not have built-in awareness of Bazel’s exact syntax rules unless the file extension is mapped to a supported language.

By default, Copilot treats files with the extensions .bzl and BUILD as plain text or Python-like files. To get accurate completions, you must add these extensions to Copilot’s language list. After that, Copilot uses the file’s existing imports, function calls, and comments to predict the next line or block. For polyglot repos, Copilot can generate targets for multiple languages in the same BUILD file if the comment and context are explicit.

Prerequisites for Copilot in Bazel Files

Before you start, confirm the following:

  • GitHub Copilot is enabled in your editor (VS Code, JetBrains, or Neovim).
  • Your Bazel workspace has at least one BUILD or .bzl file open.
  • Copilot is not disabled for the workspace or file type in your settings.
  • You have a stable internet connection for Copilot to query its model.

Steps to Enable and Use Copilot for Bazel Files

Follow these steps to configure Copilot and start generating Bazel rules with natural language prompts.

  1. Add Starlark file extensions to Copilot’s language list
    In VS Code, open the Command Palette with Ctrl+Shift+P, search for Preferences: Open Settings (JSON), and add the following line to your settings.json file:
    "github.copilot.editor.enableAutoCompletions": true,
    "files.associations": {"bzl": "python", "BUILD": "python"}

    This tells Copilot to treat .bzl and BUILD files as Python, which activates completions. For JetBrains IDEs, go to Settings > Languages & Frameworks > GitHub Copilot and add bzl and BUILD to the list of file patterns.
  2. Open a BUILD file and write a comment describing the target
    Create a new line in your BUILD file and type a comment that describes what you want Copilot to generate. For example:
    # go_library for the auth package
    Press Enter and wait for Copilot to suggest the next lines. Copilot typically generates the go_library rule with the correct name, srcs, deps, and importpath attributes.
  3. Accept the suggestion or cycle through alternatives
    Press Tab to accept the suggested code block. If the suggestion is not what you need, press Alt+] or Option+] to cycle through alternative completions. Copilot may offer different attribute sets or dependency lists based on the file’s context.
  4. Request multi-language targets in one BUILD file
    For polyglot repos, write a comment that lists the languages and targets you need. Example:
    # py_binary for the CLI tool, go_library for the backend, java_library for the SDK
    Copilot will generate three separate rules in the order you specified. Verify that the deps attributes reference the correct internal targets.
  5. Use Copilot to generate .bzl macros
    Open a .bzl file and write a comment describing the macro you want. Example:
    # macro that defines a go_binary and a go_test for a service
    Copilot will generate a function with parameters for name, srcs, and deps. You can then call this macro from multiple BUILD files.

ADVERTISEMENT

Common Pitfalls When Using Copilot With Bazel

Copilot Suggests Python Syntax Instead of Starlark

Because Copilot treats .bzl and BUILD files as Python, it sometimes suggests Python-specific constructs like def __init__ or import os. Starlark does not support these. To fix this, write a comment at the top of the file that says # Starlark Bazel build file or # build file for Bazel. This helps Copilot adjust its completions to Starlark syntax.

Copilot Generates Incorrect Dependency Names

Copilot may suggest dependency labels that do not exist in your workspace, such as //third_party:some_lib when your repo uses a different path. To mitigate this, provide a comment that lists the actual dependency paths. For example:
# deps: //internal/auth, //internal/logger, @com_google_protobuf//:protobuf
Copilot will then use those exact paths in the generated rule.

Copilot Does Not Offer Completions in BUILD Files

If Copilot remains silent when you type in a BUILD file, the file extension is likely not mapped to a supported language. Double-check the settings from Step 1. Also verify that the file is not part of a workspace where Copilot is disabled. In VS Code, look at the status bar icon for Copilot; it should show a green checkmark. If it shows a gray icon, click it and select Enable Completions.

Generated Rules Miss Required Attributes

Bazel rules often require specific attributes like visibility, testonly, or tags. Copilot may omit these. After accepting a suggestion, manually add the missing attributes. You can also write a comment that explicitly requests them: # go_library with visibility public and tags manual.

Item BUILD File .bzl File
Description Defines build targets for a single package Contains reusable macros and functions
Copilot use case Generate one or more rules from a comment Generate a macro with parameters and logic
Context needed Existing deps, srcs, and workspace paths Function signature and return type
Common prompt # java_library for the core module # macro that creates a go_binary and a go_test
Risk Missing attributes or wrong deps Python-specific syntax in Starlark

You can now use Copilot to write Bazel rules faster in polyglot repositories. Start by adding Starlark file extensions to Copilot’s language list, then write precise comments for each target. For complex macros, break the prompt into smaller parts and verify each generated block. A practical next step is to create a small .bzl macro that wraps your most common target pattern, then reuse it across multiple BUILD files. This reduces manual work and ensures consistency across languages.

ADVERTISEMENT