Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.21k stars 745 forks source link

feat: Add input parameter to `bicep build` command that allows suppressing verbosity during execution #12653

Closed AlexanderSehr closed 9 months ago

AlexanderSehr commented 9 months ago

Working recently on troubleshooting why something like $templatePaths | Foreach-Object -Parallel { bicep build $_ } would work for some Bicep templates but not for others I found out that the vebosity of the bicep build command seems to conflict with this kind of multithreading on Linux pipeline agents (I'm not able to reproduce this locally neither on Windows nor the WSL) - consistently.

The verbosity I'm referring to specifically are warnings returned by the template. For example /home/runner/work/bicep-registry-modules/bicep-registry-modules/avm/res/operational-insights/workspace/saved-search/main.bicep(44,5) : Warning BCP037: The property "etag" is not allowed on objects of type "SavedSearchProperties". No other properties are allowed. If this is an inaccuracy in the documentation, please report it to the Bicep Team. [https://aka.ms/bicep-type-issues] which is, unforatuntely, a false positive.

image

What happens using the above command is, that bicep build starts running in a thread, it prints the warning, and then the code goes into an 'error' state without an actual error to show for. To find out that it's because of the verbosity took a while:

While there is a 'workaround', that is, adding #disable-next-line to any line in question, there is a fair chance these issues will return each time e.g. the Bicep linter rules are updated.

To solve this for cases where I'm not interested in this verbositry (e.g., when I just want to build a dozen templates to statically test them) it would be very helpful if there would be a parameter to prevent the command from trying to write anything to the output/console stream. For example bicep build <path> --no-verbose.

AlexanderSehr commented 9 months ago

This may or may not be related to #2334

anthony-c-martin commented 9 months ago

It sounds like the powershell script or your CI pipeline is treating stderr as an indication that the command has failed? I would recommend instead checking the exit code to determine success or failure, and allowing stderr to be written to the normal error output channel.

If you're just looking to suppress stderr, then using 2> is a standard mechanism for doing this. Powershell:

bicep build main.bicep --stdout 2>$null

Bash:

bicep build main.bicep --stdout 2>/dev/null
AlexanderSehr commented 9 months ago

Dear @anthony-c-martin , thank you for the swift reply. Let me give that a shot. :)

AlexanderSehr commented 9 months ago

So... the Bicep command really didn't like the --stderr: Unrecognized parameter "--stderr" :D Hence I'm not entirely sure how to check the exit code on this one.

Nethertheless - it did allow us to get past the issue. Thank you @anthony-c-martin :) If you happen to have a pointer regarding the stderr, let me know. :)

anthony-c-martin commented 9 months ago

@AlexanderSehr I'll start by saying that I'm not a powershell expert. Generally, there are 3 things to check when you invoke a process: stdout, stderr, exit code.

My recommendation would be:

Example of the approach I followed for accomplishing something similar in C# here:

Personally I'm not a PowerShell expert, so I'd have to defer to someone else to recommend how to accomplish this in Pwsh.