Skip to content
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

What is the best way to validate a property in a .props file? #3858

Closed
sanderobdeijn opened this issue Oct 13, 2018 · 1 comment
Closed

What is the best way to validate a property in a .props file? #3858

sanderobdeijn opened this issue Oct 13, 2018 · 1 comment

Comments

@sanderobdeijn
Copy link

I'm making a .props file that reads some properties in a .csproj and then sets other properties and loads some nugetpackages.
I want to add errors or warnings when the properties are empty. So I added a warning tag that looks for a empty property.
But I got an error that the warning tag can no longer be placed directly into the project tag. So I added an target tag and placed the warning tag into the target tag.
And to make sure the properties are always checked I have added the target to the the InitialTargets attribute in the Project tag. Is this the correct way, or will I now override the InitialTargets attribute in the csproj file.
Also I would like to add a link to a support page in visual studio. I used the HelpKeyword attribute but visual seems to mangle the url. Can an url be added to this attribute.

Here is my code:

<Project InitialTargets="ValidateProjectParameters">
    <Target Name="ValidateProjectParameters">
        <Warning Text="The property TestProperty is empty in the projectfile"
                 Condition="'$(TestProperty)' == ''"
                 Code="1"
                 HelpKeyword="http://bfy.tw/19ge"/>
    </Target>
</Project>
@rainersigwald
Copy link
Member

That's a pretty good way to do this. InitialTargets are additive (PR to mention this in the docs: MicrosoftDocs/visualstudio-docs#1737) so you won't be clobbering anything.

One consideration: projects are built in several contexts that aren't normal builds. Most important is probably Visual Studio's "design-time build" which is used to extract information about the project to drive IntelliSense and other IDE features. You should be careful to only fail design-time builds in very broken circumstances. Since this target emits a warning, rather than an error, it's fine.

As a general guideline, please provide a more specific error code than 1. Users can suppress warnings using the code, so it should ideally be unique to your project. This is often done with 3- or 4-letter prefixes (like MSBuild's MSBxxxx or C#'s CS1234).

Would you mind opening a separate bug about the HelpKeyword? Someone would need to dig into that a bit to figure out what's mangling the URL and whether that's intentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants