"Unmerged change from project" after running dotnet format
When starting with a piece of code like this where I purposely set the if condition all in one line:
public static OptionsBuilder<T> AddOptionsValue<T>(this IServiceCollection services, IConfiguration configuration)
where T : class, new()
{
if (services is null) { throw new ArgumentNullException(nameof(services)); }
return services
.AddOptions<T>()
.Bind(configuration)
.AddOptionsValue();
}
After running dotnet format I get the following:
public static OptionsBuilder<T> AddOptionsValue<T>(this IServiceCollection services, IConfiguration configuration)
where T : class, new()
{
/* Unmerged change from project 'DotNet.Sdk.Extensions(net5.0)'
Before:
if (services is null) { throw new ArgumentNullException(nameof(services)); }
After:
if (services is null)
{
throw new ArgumentNullException(nameof(services)); }
*/
/* Unmerged change from project 'DotNet.Sdk.Extensions(net6.0)'
Before:
if (services is null) { throw new ArgumentNullException(nameof(services)); }
After:
if (services is null)
{
throw new ArgumentNullException(nameof(services)); }
*/
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
return services
.AddOptions<T>()
.Bind(configuration)
.AddOptionsValue();
}
I seem to be able to reproduce this reliable from this repo of mine. The file I changed and mention in this example was this one: OptionsBuilderExtensions.cs.
The project where this happens targets multiple frameworks: netcoreapp3.1;net5.0;net6.0.
The dotnet format command my GitHub workflow is running looks like the following:
dotnet format ${{ github.workspace }}/DotNet.Sdk.Extensions.sln `
--severity info `
--verbosity diagnostic `
--report ${{ env.DOTNET_FORMAT_REPORT_FILEPATH }}
This seems related with #1574 .
Hopefully this provides enough information to be able to replicate the issue. Let me know if you need more info.
I created a new branch to format the project, then run: dotnet format -v diag myproj.csproj
I use the built-in format in net6
dotnet format --version
6.4.345109+d2f060021d5a71871927bb961a6c09e76bad2be7
I face this problem in multi target project net6, net472 in almost all files like:
/* Unmerged change from project 'myproject.Tests(net472)'
Before:
}
After:
}
*/
Although before and after is the same.
I had to undo the changes in these files using git reset..
how to avoid this issue?
Issue Resolution: Workaround solution
I resolved my issue by temporary using only one framework net6, then run format without issues.
Then modify the project as multi target and resolve the remainder manually.
It seems that format run twice:
First run,net472, modify files without saving changes.
Then run second time on net6 and had to remodify files Unmerged change from project ...
Hitting this quite a lot myself with net6.0 and net7.0. It would be great if it didn't happen at all, but would be helpful if we could specify a framework to avoid it.
Is there any progress in this issue or any guideline document to avoid such comments?
@moh-hassan If you run dotnet-format with the logging set to diagnostic (-v diag) does it tell you the change that is being requested? It looks like it could be end-of-line changes.
@JoeRobich
I run dotnet format with -v diag and inspected the
The logs in screen is something like:
Determining diagnostics...
Running 142 analyzers on myproject(net6.0).
Running 10 analyzers on myproject(netstandard2.0).
Running 10 analyzers on myproject(net461).
Running 10 analyzers on myproject(net472).
The logs did not show if there is any confliction that may cause the Unmerged change'
Also the comments written to the source file are sometimes unhelpful like this.
It is nice if the log show these Unmerged changes, so we can exclude these files from processing or even undo changes using git.
Or allow to select framework in case of multi-target project, especially the format tool use different editorconfig files (2 or 3 files) for different framework including the global editorconfig in the root of the solution.