[Bug]: BeforeTargets and AfterTargets comments are incorrect for SDK style projects
- Dominant language
- C#
- Stars
- 5.5k
- Forks
- 1.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 139
Description
### Issue Description
`Microsoft.Common.CurrentVersion.targets` defines the `BeforeBuild` and `AfterBuild` targets, with the comment "Redefine this target in your project in order to run tasks":
https://github.com/dotnet/msbuild/blob/457218b3304b07a05c8b51640792d8125d36bba8/src/Tasks/Microsoft.Common.CurrentVersion.targets#L897-L913
I was trying to manually upgrade a non-SDK style project to SDK style (maybe I should have tried Upgrade Assistant? 🤷), and the project had re-defined the `AfterBuild` target after importing `Microsoft.CSharp.targets`. After converting to an SDK style project, this stopped working. Fortunately due to my MSBuild knowledge/experience, I knew that the SDK was importing the common targets after my csproj, and therefore redefining my project's target, rather than the other way around. But when I was looking at the preprocessed file and saw these comments explicitly telling me to override the target, it took me longer to realize the import-order issue. I imagine that less experienced developers it would be more difficult to find a working solution.
### Steps to Reproduce
This is kind of similar to what my rushed, naïve attempt at porting my non-SDK style project looked like:
```xml
net6.0
This is after build
```
### Expected Behavior
Either the "this is after build" message is output when I run `dotnet build`, or get more feedback about why it's not running (ideally with info on how to fix it)
### Actual Behavior
My target is silently ignored
### Analysis
In non-SDK style projects, the "common targets" get imported explicitly, allowing more MSBuild script to be entered after the import, but still within the project file. With the SDK syntax as an XML attribute on the root `Project` element, the targets are implicitly imported at the end of the project file. I happen to know that there are other syntax to import the SDK's targets explicitly in the project file, which is another solution: https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-use-project-sdk?view=vs-2022
I wonder if it's feasible for `
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.