vhdl_ls_gen: Generating VHDL Language Server Config from Bazel

#bazel#VHDL#LSP#tooling#auto#FPGA

vhdl_ls_gen generates the vhdl_ls.toml configuration for VHDL-LS directly from Bazel metadata. VHDL-LS is the Rust-based language server from the rust_hdl project. The generated configuration maps each VHDL source file to its declared library across custom IP, external vendor packages, and generated code. Without an accurate library map, the editor produces false diagnostics across every file in cocoapuffs-fpga. Spurious diagnostics obscure genuine compilation errors.

The library map problem

C and C++ language servers can infer build contexts from source paths and compiler include directories. VHDL requires explicit library assignments. The semantic meaning of a design unit depends on its target compilation library. Because VHDL source paths rarely indicate library names, VHDL-LS requires a [libraries] table mapping library identifiers to file lists. In a repository where dozens of BUILD files reference shared IP, Bazel already manages this dependency structure. Maintaining a separate configuration file by hand quickly leads to drift and omissions.

Choosing tags over an aspect walk

The conventional Bazel design would use a build aspect to traverse the target graph from a designated root and emit configuration tables. Instead, vhdl_ls_gen uses target tags on standard filegroup targets:

  • vhdl_ls registers a filegroup for configuration export.
  • vhdl_ls:lib=some_library assigns the enclosed sources to a specific VHDL library.
  • vhdl_ls:rem=prefix and vhdl_ls:add=prefix adjust file paths for editor resolution.

Running bazel run @vhdl_ls_gen//:gen collects all tagged targets and writes vhdl_ls.toml to the workspace root.

Tag-based discovery offers two concrete advantages over aspect traversal:

  1. An aspect requires a root target whose dependencies span the entire codebase. An editor configuration must also index standalone testbenches and newly imported IP blocks before integration into top-level targets. Tags allow any filegroup to register its files independently.
  2. Path manipulation tags resolve generated files located in bazel-out or external repositories. The generated configuration points to unisim_VCOMP.vhd so that Xilinx primitives resolve in the editor immediately after synthesis.

This approach trades build-time validation for editor flexibility. A developer could tag a target with an incorrect library name, and the language server would adopt that setting without verification from the compiler. This trade-off is acceptable for editor navigation, where a stale entry causes only an incorrect editor diagnostic.

Managing the generated configuration

The repository commits the 20 kB vhdl_ls.toml at its root. The file includes an autogenerated warning banner. It updates via bazel run whenever the source tree changes. Committed .prefix and .suffix files provide custom settings that wrap around the emitted table. These files define VHDL standard revisions, linter rules, and vendor exclusions such as marking unisim as third-party code. Keeping the configuration format plain and inspectable makes troubleshooting straightforward.

In cocoapuffs-fpga

Tagged filegroups feed the generator from every subsystem directory. The board definition registers libraries noelv, noelv_tb, and noelv_tb_only. Peripheral blocks for DDR3, AXI, Wishbone, and TileLink each map to isolated libraries. Aggregated filegroups pull in VUnit sources as vunit_lib and OSVVM as osvvm_lib. Working definition lookup across GRLIB IP, verification frameworks, and vendor primitives provides a reliable editing environment.

Prior art and references

This post is part of the build system series behind cocoapuffs-fpga, which started with rules_vivado. The source code is hosted at filmil/bazel_vhdl_ls_gen and published through my Bazel registry.