rules_auto_dotnetAPI docs @0.2.0

@rules_auto_dotnet//auto_dotnet:defs.bzl

Public API surface is re-exported here.

Users should not load files under "/auto_dotnet/private"

Functions & Macros

dotnet_generated_props

Generate a .props file to sync IDE with Bazel-generated sources.

This creates a .props file containing <Compile Include="..."> elements for
Bazel-generated source files. The .props file can be imported into a .csproj
to enable IDE features (IntelliSense, Go to Definition) for generated code.

Usage in BUILD.bazel:

load("@rules_dotnet//dotnet:defs.bzl", "dotnet_generated_props")

proto_csharp_library(
    name = "my_protos",
    protos = ["//proto:services"],
)

dotnet_generated_props(
    name = "sync_ide",
    generated_srcs = [":my_protos"],
    out = "MyProject.Generated.props",
)

Usage in .csproj:

<Project Sdk="Microsoft.NET.Sdk"> <Import Project="MyProject.Generated.props" Condition="Exists('MyProject.Generated.props')" /> </Project>

To update the .props file:

bazel run //:sync_ide
Parameters
*name

Target name. Use this with bazel run to update the .props file.

*generated_srcs

List of labels to Bazel-generated source files.

out

Output filename (default: "{name}.props" in the current package).

Default: None
suggested_update_target

Override the update target shown in the generated comment.

Default: None
kwargs

Additional arguments passed to write_source_file.

dotnet_generated_props_test

Test that a .props file is up-to-date with generated sources.

This is the test-only version that fails if the .props file needs updating.
Use in CI to ensure generated files are committed.

Parameters
*name

Target name.

*generated_srcs

List of labels to Bazel-generated source files.

*out

The .props file to check.

kwargs

Additional arguments passed to write_source_file.

@rules_auto_dotnet//auto_dotnet:extensions.bzl

Extensions for bzlmod

Module Extensions

auto_dotnet

Extension for automatic .NET project scanning and Bazel target generation.

This extension scans .csproj and .fsproj files in your workspace and generates
Bazel targets using rules_dotnet. Use alongside @rules_dotnet for toolchain
registration and build rules.

Tag Classes
toolchain

Declare a .NET toolchain version.

These declarations are used by scan_projects to validate that registered
toolchains cover all discovered target frameworks. The actual toolchain
registration should be done via rules_dotnet's extension.

AttributeTypeDescription
namename

Base name for generated repositories

Default: "dotnet"
dotnet_versionstring

Version of the .Net SDK

Default: ""
scan_projects

Enable automatic scanning of .csproj and .fsproj files.

When enabled, the extension will:

  1. Scan the workspace for .csproj and .fsproj files
  2. Parse each file to extract properties, sources, and dependencies
  3. Generate a @dotnet_projects repository with .bzl files for each project
  4. Validate that registered toolchains cover all discovered target frameworks

Usage in MODULE.bazel:
dotnet = use_extension("@rules_dotnet//dotnet:extensions.bzl", "dotnet")
dotnet.toolchain(dotnet_version = "10.0.100")
use_repo(dotnet, "dotnet_toolchains")
register_toolchains("@dotnet_toolchains//:all")

auto_dotnet = use_extension("@rules_auto_dotnet//auto_dotnet:extensions.bzl", "auto_dotnet")
auto_dotnet.toolchain(dotnet_version = "10.0.100")
auto_dotnet.scan_projects()
use_repo(auto_dotnet, "dotnet_projects")

Usage in BUILD files:
load("@dotnet_projects//path/to:MyProject.csproj.bzl", "auto_dotnet_targets")
auto_dotnet_targets(name = "MyProject")

File Change Detection:
Changes to existing .csproj/.fsproj files are automatically detected.
New files in existing project directories are also detected.

However, new project files in entirely new directories require manual sync:
    bazel sync --only=@dotnet_projects

Requirements:
Bazel 7.1 or later is required for the scan_projects feature.

AttributeTypeDescription
exclude_patternslist of strings

Glob patterns for paths to exclude from scanning.

Default patterns exclude bin/, obj/, .git/, .jj/, and bazel-* directories.

Default: []
nuget_repo_namestring

Name of the NuGet repository to generate.

Packages can be referenced as @{nuget_repo_name}//PackageName.

Default: "dotnet_projects.nuget"
fail_on_missing_toolchainboolean

If true, fail when a project targets a TFM not covered by registered toolchains.

Set to false to only emit warnings instead of failing.

Default: True