-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MSBuild linter #1777
Comments
Thanks, @danmosemsft: #1774 (comment). |
My favorite linter rule would be to catch <ItemGroup>
<MyItem Include="$(SomeProperty)/**" />
</ItemGroup> Where |
@stan-sz that is a great linter use case but also see #3642 (comment) and the upcoming #7029. |
Another case are conditions in form of Condition="$(SomeProperty)"> where at least the linter should signal the missing single quotes and comparison with empty string like: Condition=" '$(SomeProperty)' != '' "> |
Some of the items listed in the initial scope for linter are captured in Feature: Warning Waves |
We had an interesting issue today in our team, where:
Broke Publishing, and was not trivial what was happening, until we remembered that ProjectReference mapped to MsBuild task and removed the Properties delegated trough, removing the TargetFramework. Interesting case as well for a linter warning, suggest to use AdditinalProperties instead |
For the record: a linter to catch missing single quotes in string comparison
|
What's the difference between |
This is to handle case when a property evaluates to an empty value. From: https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions?view=vs-2022
|
quoted.proj: <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Run">
<PropertyGroup>
<Property />
</PropertyGroup>
<Target Name="Run">
<Message Condition="$(Property) == ''" Importance="high" Text="Is empty without quotation marks" />
<Message Condition="$(Property) != ''" Importance="high" Text="Is not empty without quotation marks" />
<Message Condition="'$(Property)' == ''" Importance="high" Text="Is empty with quotation marks" />
<Message Condition="'$(Property)' != ''" Importance="high" Text="Is not empty with quotation marks" />
</Target>
</Project>
Perhaps the documentation means you cannot compare to an empty unquoted string literal like |
This could also include formatting checks like Prettier for XML does. Ideally, I'm thinking of full static code analysis for csproj, props, and targets files. |
Another possibility (in strict mode?) could be to help with Boolean properties. Properties of course have no type but in practice many are Boolean existing in a world where empty string is default. This can lead to ambiguity about what blank means, and since often we don't want to add a line to explicitly set a default, blank is treated as default. If that means blank is true, then properties end up with confusing negative names like say "DoNotOptimize". The obvious sources of errors here are the confusion of seeing a negative property to false, or assuming blank means true (or false) when it doesn't, or comparing against blank when it was set explicitly, or setting anything other than true or false as the value. Years ago when we wrote the original "targets" I tried to have a rule that either blank was assumed to be false or else the value was explicitly defaulted to "true", then comparisons were always and only against "true", "false" and "" never appeared in conditionals on these Booleans, and hopefully property names were not negative (in practice some were inherited from the old format and already were). A linter could possibly help by
This would of course be noisy. Perhaps the real answer is to have some way to annotate a property as Boolean with a default. Note of course that the conditional parser IIRC does have some special casing for Booleans for example you can negate: |
Another linting ideas:
|
Another linter idea: #6277 |
Another idea: #3976 |
Fail\warn on property overwrite without Overwrite="true" metadata. |
Another idea: A colleague just ran into a case where a project specified |
Not sure if it's a bug, something to catch with a linter, or maybe if there's already a workaround, but a problem I'm noticing is that this is an error (MSB4035): <ProjectReference Include="" /> But these are not errors or even warnings: <ProjectReference Include="$(empty)" />
<ProjectReference Include=" " /> |
MSBuild could have an opt-in mode that would apply rules and heuristics to give suggestions about project "health".
Possible warnings:
@(I->'%(Identity))
with a missing closing'
).The text was updated successfully, but these errors were encountered: