MSBuild dialect
@pbuilder/sdk/msbuild edits the XML files of a .NET build: project files, the shared .props
and .targets files and .slnx solutions. It has one named op, addProjectReference, which
fits schematics that scaffold several projects and wire them together, such as the layers of a
hexagonal solution. The dialect edits the XML text itself and never runs the dotnet CLI. All
ops chain on one handle and the file is written once; see
Dialects for how a handle works.
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net10.0</TargetFramework> </PropertyGroup></Project>import * as msbuild from "@pbuilder/sdk/msbuild";
export const run = async () => { await msbuild .find("src/Shop.Infrastructure/Shop.Infrastructure.csproj") .addProjectReference("../Shop.Application/Shop.Application.csproj") .addProjectReference("../Shop.Domain/Shop.Domain.csproj");};<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net10.0</TargetFramework> </PropertyGroup>
<ItemGroup> <ProjectReference Include="../Shop.Application/Shop.Application.csproj" /> <ProjectReference Include="../Shop.Domain/Shop.Domain.csproj" /> </ItemGroup></Project>find(path)accepts.csproj,.props,.targetsand.slnx. The file name must end in one of them after a non-empty stem, as inDirectory.Build.propsorShop.slnx. Any other path throws synchronously with thedialect operation failed:prefix, before any op runs.- The classic
.slnsolution format is rejected, because it is not XML. Use.slnx. - A file that is not well-formed XML fails the factory with
could not parse "<path>", and the file is left as it is. An empty file parses as a document with no root element, so it round-trips when no op edits it.
addProjectReference(path)
Section titled “addProjectReference(path)”Adds <ProjectReference Include="path" /> to a file whose root element is Project. path is
a relative path whose file name ends in proj (.csproj, .fsproj, .vbproj, .proj),
written with the separators you give.
A reference goes after the last ProjectReference of the first ItemGroup without a
Condition that holds one:
<Project Sdk="Microsoft.NET.Sdk"> <ItemGroup> <ProjectReference Include="..\Shop.Domain\Shop.Domain.csproj" /> </ItemGroup></Project>await msbuild .find("Shop.Infrastructure.csproj") .addProjectReference("../Shop.Application/Shop.Application.csproj");<Project Sdk="Microsoft.NET.Sdk"> <ItemGroup> <ProjectReference Include="..\Shop.Domain\Shop.Domain.csproj" /> <ProjectReference Include="../Shop.Application/Shop.Application.csproj" /> </ItemGroup></Project>When there is no such ItemGroup, a new ItemGroup holding the reference goes after the last
child element of Project, after a blank line, as in the first example on this page. An empty
Project gets it as its first child, and a self-closing <Project /> is opened to hold it.
A reference to the same path anywhere in the file leaves the file as it is, comparing / and \
as one separator. On a file that already has
<ProjectReference Include="..\Shop.Application\Shop.Application.csproj" />,
addProjectReference("../Shop.Application/Shop.Application.csproj") changes nothing, so a second
run of the factory changes nothing either.
Rejections
Section titled “Rejections”The op rejects with the dialect operation failed: prefix, naming addProjectReference, and
leaves the file as it is, for:
- a
paththat is not a string, that is absolute (/src/A.csproj,C:\A.csproj), or whose file name does not end inproj; - a file whose root element is not
Project, such as the<Solution>root of a.slnxfile; - an empty file, which has no root element.
Format preservation
Section titled “Format preservation”A file no op touched prints byte-identical. An edited file changes only the inserted range: the XML declaration, comments, attribute quotes, blank lines, whitespace, a UTF-8 byte order mark and the final newline stay as they are.
- A new element takes the indentation of its siblings, or one unit deeper than its parent when it
has none. The unit is the first indentation step the file shows between an element and its
parent, and two spaces when the file shows none. The dialect never reads
.editorconfig. - A new line uses the file’s line ending:
\r\nwhen the file has one,\notherwise. - The value written into
Includeis escaped (&,<,>,"), so it can never become markup.
Custom edits with .modify()
Section titled “Custom edits with .modify()”.modify(fn) hands fn the live MsbuildDocument, whose text is the file without its byte
order mark. Replace text for any edit the op does not cover. astLibrary is
saxes, the streaming XML parser the dialect uses. With
{ position: true }, its position after an opentag or closetag event is the offset just
past the tag’s >. This factory sets the value of <Nullable>:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <Nullable>disable</Nullable> </PropertyGroup></Project>import * as msbuild from "@pbuilder/sdk/msbuild";
export const run = async () => { await msbuild.find("Shop.Api.csproj").modify((doc) => { const parser = new msbuild.astLibrary.SaxesParser({ position: true }); let start: number | undefined; parser.on("opentag", (node) => { if (node.name === "Nullable") start = parser.position; }); parser.on("closetag", (node) => { if (node.name === "Nullable" && start !== undefined) { const end = doc.text.lastIndexOf("<", parser.position - 1); doc.text = `${doc.text.slice(0, start)}enable${doc.text.slice(end)}`; } }); parser.write(doc.text).close(); });};<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <Nullable>enable</Nullable> </PropertyGroup></Project>The general rules for custom edits are in Dialects.
Next steps
Section titled “Next steps”- C# dialect and ASP.NET dialect: edit the
.csfiles of the projects you reference. - AST libraries: which library and version back each dialect.
- The SDK’s MSBuild dialect guide.