2
votes

TL;DR - Is there an Azure DevOps pipeline task for formatting code as part of the build process? I have not been able to find one and would really find it useful.

My team uses the free CodeMaid Visual Studio extension to "beautify" (format) C# code. While the extension works well, there are several limitations to relying on developers to format their own code in Visual Studio:

  • Developers constantly forget to run the tool.
  • Enabling "Automatically run file clean-up on save" can be frustrating to use while code is being worked as it reorganizes code which might break one's train-of-thought.
  • Developers can change the format settings to their own liking instead of abiding by the formatting standard of the team which can cause code merge issues and formatting wars.

I think a much better solution would be to have a pipeline task in Azure DevOps that ran a tool like Code Maid as part of the build process. This would ensure that no code gets into source control in an ugly, unformatted state and would ensure that all code is formatted to whatever standard is enabled as part of the build.

3

3 Answers

3
votes

In my opinion, the commit triggering the build should be immutable. If you put something to format code you are going to have push a new commit to the repo for the change to really be incorporated. So then, are you just going to trigger the CI build again?

My suggestion might be to automate running the formatting tool as part of the pipeline. If it finds any changes that would be made (meaning someone didn't run the tool before pushing to remote), just fail the build. Make a passing build be a requirement to merging a Pull Request. People will get in line if they can't complete their work through the normal process. The settings you configure for it in the pipeline will be the standard, so that resolves developers picking tabs or spaces. :)

Code Maid might not be the best option for this. I see an open github ticket about it not really being great for command line automation. It is pretty tightly coupled to Visual Studio. Also, it might be somewhat large effort, but I imagine you would want to someone to run the formatting on the full source, so everyone can start from green.

1
votes

You can run a native command on the command line which is dotnet-format which will achieve what you want to. You could run this in your pipeline. It does need installing so may add a overhead to your builds.

Install using the following command:

dotnet tool install -g dotnet-format

Then to format the current project or solution in the current directly, use:

dotnet format

To just fix code style analyser warnings you could use this command as well:

dotnet format --fix-style warn

The full list of commands available is on the projects GitHub.

0
votes

I think a much better solution would be to have a pipeline task in Azure DevOps that ran a tool like Code Maid as part of the build process. This would ensure that no code gets into source control in an ugly, unformatted state and would ensure that all code is formatted to whatever standard is enabled as part of the build.

Azure Devops doesn't have such pipeline task to format files during pipeline run. And it's not recommended to make changes to git repo during pipeline run.

The normal scenario is:

1.We have git repo hosted in Azure Devops and someone is developing locally with dev branch.

2.Someone make some changes and commit the changes(modify code or add extra source files)

3.Sync the commits with remote repo, the change in remote repo trigger the corresponding pipeline to run. (CI to check whether the code is ok, CD for deploy new changes)

Now it you modify source files during pipeline, it will cause endless trigger unless you disable the CI. Instead you could use Branch policy to make the process better, you can set branch policy for master branch. So that any changes via PR to master should be approved by reviewers. And the specific reviewers can help check the code format.