csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Format files in the root solution folder during msbuild

Open aleksator opened this issue 1 month ago • 1 comments

Currently the files in the solution folder, such as Directory.Build.props, Directory.Packages.props, and others, are not formatted during msbuild. We also have custom XMLs in the root folder, which would be good to have formatted with CSharpier.

Environments:

  • CSharpier Version: 1.2.1
  • Running CSharpier via: msbuild
  • Operating System: Windows/Linux
  • .csharpierrc Settings: -
  • .editorconfig Settings:
indent_style = tab
indent_size = 4

Steps to reproduce: Add:

    <GlobalPackageReference Include="CSharpier.MSBuild" PrivateAssets="all" Version="1.2.1" />

to Directory.Packages.props. Run dotnet build or build via an IDE. Expected behavior: Directory.Packages.props and other files in the root directory, containing .sln are formatted. Actual behavior: Directory.Packages.props and other files in the root directory, containing .sln are not formatted.

aleksator avatar Dec 02 '25 00:12 aleksator

There isn't really a obvious way this would work.

GlobalPackageReference is used to include a given nuget package in every csproj. When a given csproj references CSharpier.MsBuild it will format the directory that contains the csproj using a Target within that csproj. There would need to be logic that determines a csproj is including CSharpier.MsBuild via a GlobalPackageReference and then it would only conditionally format the root directory. You don't want the build event for each individual csproj to format everything in the root directory - that could waste a lot of time in solutions with a large number of projects. That also assumes CSharpier.MsBuild is able to reliably determine what is considered the root directory.

It would be much easier just reference CSharpier as a dotnet tool.

If desired you can hook into a build event on one of the projects and do the formatting/checking from there, hard coding the root directory. At work we keep track of the root directory with a Directory.Build.props file that is in the root with this property defined.

<Project>
  <PropertyGroup>
    <RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>
  </PropertyGroup>
</Project>

belav avatar Dec 02 '25 01:12 belav