csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Empty newlines between groups in `.csproj` files to be kept to have same behavior as `C#` code

Open johan-lindqvist opened this issue 7 months ago • 1 comments

It's nice to see that XML-files are now formatted. This is a borderline opinion request but since it works this way in C# code I would expect the same for XML code.

If there is one newline between ItemGroup and PropertyGroups they should be kept, if multiple then they should be reduced to one.

Input:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net8.0</TargetFramework>
	</PropertyGroup>
	
	<ItemGroup>
		<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
	</ItemGroup>

	<ItemGroup>
		<ProjectReference Include="..\OtherProject.csproj" />
	</ItemGroup>

	<ItemGroup>
		<None Update="appSettings.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
		</None>
	</ItemGroup>
</Project>

Output:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net8.0</TargetFramework>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
	</ItemGroup>
	<ItemGroup>
		<ProjectReference Include="..\OtherProject.csproj" />
	</ItemGroup>
	<ItemGroup>
		<None Update="appSettings.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
		</None>
	</ItemGroup>
</Project>

Expected behavior: For the empty newlines to be kept if there is only one between groups. Expected result:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net8.0</TargetFramework>
	</PropertyGroup>
	
	<ItemGroup>
		<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
	</ItemGroup>

	<ItemGroup>
		<ProjectReference Include="..\OtherProject.csproj" />
	</ItemGroup>

	<ItemGroup>
		<None Update="appSettings.json">
			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
		</None>
	</ItemGroup>
</Project>

johan-lindqvist avatar May 05 '25 11:05 johan-lindqvist

I agree that CSharpier should respect single empty lines between elements. I ran into this when adding the xml formatting and thought I had created an issue to track it. If I recall correctly there wasn't a straightforward way to keep them so I pushed it off.

belav avatar May 09 '25 15:05 belav