# .NET

Build and test the C# and F# projects of .NET solutions alongside the rest of a repository.

The .NET plugin lets Axle see the C# and F# projects in a repository, so they build and test in the same run as everything else, in dependency order, from the cache when nothing changed. It's written in C# and runs with `dotnet`, which every repository it serves already has.

## Add it

```sh
axle add dotnet
```

This writes it into the root `axle.toml`:

```toml
[plugins.dotnet]
source = "npm:@axle-rs/plugin-dotnet@0.1.0"
hash = "sha256-…"
```

It needs the .NET 11 SDK or newer on the `PATH`. The plugin is a file-based app, so the first time it starts the SDK compiles it, which takes a few seconds, and keeps what it compiled outside the plugin's folder; later starts take about half a second. It uses only .NET's standard library, so nothing is downloaded. On a computer that has never run a file-based app, the SDK first sets itself up, which can take longer than Axle's 30 seconds; if Axle [stops the plugin](https://axlers.dev/docs/messages/plugin-timed-out.md), run the command again or raise `timeout` in `[plugins.dotnet]`.

A repository with no JavaScript needs no `package.json`: the `axle.toml` at its root, the one that lists the plugin, is what Axle finds it by.

## What it finds

When the repository has solution files, `.sln` or `.slnx`, the projects are the `.csproj` and `.fsproj` files they list. Without a solution, every `.csproj` and `.fsproj` is a project. Each is named after its project file, without the extension, and `axle list` shows them with the kind `plugin`. Files Git ignores are skipped, and so are folders named `bin`, `obj`, `node_modules` and `.git`, a project at the root of the repository and a folder that is already a package. Two project files in one folder are an error, since each project needs a folder of its own.

A project depends on another project the plugin found when it has a `ProjectReference` to it, so its tasks wait for that project's.

A project is a test project when it sets `IsTestProject`, uses the `MSTest.Sdk` project SDK or references `Microsoft.NET.Test.Sdk`, MSTest, xUnit, NUnit or TUnit.

## Versions and the cache

The plugin reads the versions each project uses from the `packages.lock.json` in its folder, for every framework it targets, and Axle hashes them like a JavaScript lockfile. A new version of a package misses the cache of the projects that use it and no others. Projects without a `packages.lock.json` report no versions; turn on `RestorePackagesWithLockFile` to give Axle them. The lockfiles themselves aren't among the files every task reads, and `--affected` counts a change to one against every .NET project.

Each task reads its project's files, the files of the projects it depends on, and any `Directory.Build.props`, `Directory.Build.targets`, `Directory.Packages.props`, `global.json` and `NuGet.config` from the project's folder up to the root. MSBuild reads environment variables as properties, so tasks only get the variables Axle gives every task, plus `Configuration`, `Platform` and `ContinuousIntegrationBuild`, which change their hash, and the `DOTNET_*`, `NUGET_*` and `MSBUILD*` variables, which don't, so a setting such as `DOTNET_CLI_TELEMETRY_OPTOUT` still reaches `dotnet`.

The plugin's answers are kept until a project file, a solution, a `packages.lock.json` or a `.gitignore` changes.

## Tasks

| Task    | Projects      | Command                                 | Settings                                                   |
| ------- | ------------- | --------------------------------------- | ---------------------------------------------------------- |
| `build` | every project | `dotnet build <project file>`           | `outputs = ["bin/**", "obj/**"]`, `dependsOn = ["^build"]` |
| `test`  | test projects | `dotnet test <project file> --no-build` | `outputs = []`, `dependsOn = ["build"]`                    |

`dotnet build` also builds the projects a project references, so `build` waits for theirs: by the time it starts they are up to date, and two builds never write the same project's `bin` and `obj` at once. `build` stores `bin` and `obj`, so a project from the cache is ready for the projects that reference it. `test` waits for its own project's `build` and doesn't build again.

`tasks` in the plugin's settings adds tasks or changes them for every project, and `test` only ever goes to test projects. A task is the `dotnet` command and its arguments, which the plugin runs with the project file after the command's name, a table with that command in `run` and any [task settings](https://axlers.dev/docs/reference/configuration.md#tasks), or `false` to leave it out:

```toml
[plugins.dotnet]
source = "npm:@axle-rs/plugin-dotnet@0.1.0"
hash = "sha256-…"
tasks = { format = "format --verify-no-changes", pack = { run = "pack -c Release", outputs = [
    "bin/Release/*.nupkg",
] }, test = false }
```

A project's own `axle.toml` and the root one change them as they would any task, and win over the plugin.

The tasks don't run in [batches](https://axlers.dev/docs/reference/plugins.md#batch-mode). MSBuild can build many projects in one process, but its output and its result belong to the whole build, so the plugin couldn't tell Axle exactly which project printed what and which one failed, and Axle caches each project's result on its own.
