Good article!
As far as I can tell there’s almost no tutorial content for MSBuild, and that bugs me. As far as I can tell most devs just lean on it as a magic tool that “just works”, but a lot of janky DevOps pipeline tasks would be better implemented in MSBuild.


And if MAUI’s anything like Xamarin, more people are going to find out “it just works” is a myth.
A previous developer left a project with some custom MSBuild tasks to do things that could have been implemented just as well with simple PowerShell scripts (e.g. injecting a version number in assembly files). That was a pain to troubleshoot and maintain.
When it comes to MSBuild, just bend to the system instead of trying to tame it.
It’s true that tutorials are lacking, but I’ve mentioned it here before that Sayed Hashimi’s Inside The Microsoft Build Engine._2JU2WQDzn5pAlpxqChbxr7{height:16px;margin-right:8px;width:16px}._3E45je-29yDjfFqFcLCXyH{margin-top:16px}._13YtS_rCnVZG1ns2xaCalg{font-family:Noto Sans,Arial,sans-serif;font-size:14px;font-weight:400;line-height:18px;display:-ms-flexbox;display:flex}._1m5fPZN4q3vKVg9SgU43u2{margin-top:12px}._17A-IdW3j1_fI_pN-8tMV-{display:inline-block;margin-bottom:8px;margin-right:5px}._5MIPBF8A9vXwwXFumpGqY{border-radius:20px;font-size:12px;font-weight:500;letter-spacing:0;line-height:16px;padding:3px 10px;text-transform:none}._5MIPBF8A9vXwwXFumpGqY:focus{outline:unset} /*# sourceMappingURL=https://www.redditstatic.com/desktop2x/chunkCSS/TopicLinksContainer.361933014be843c79476.css.map*/ is an absolutely incredible reference.
I hope they’ll one day next integrate binary logging into VS.
(The extension isn’t quite the same.)
Is there a reasonable way via msbuild to rewrite a packagereference based on selected configuration?
Say I’m looking at MyNamespace.SomeNuget.csproj, which has this:
<PackageReference Include="MyNamespace.SomeOtherNuget" Version="1.2.3" />
And when building my MyNamespace.SomeNuget.csproj with Debug Configuration, I want it to rewrite to:
<PackageReference Include="MyNamespace.SomeOtherNuget" Version="1.2.3-Debug" />
I.e. append “-Debug” to the version string for PackageReferences with a certain namespace.
The reason I want to do this automatically instead of via conditionals in the csproj is that this is the only feasible way I see where my team mates can use nuget UI without causing all sorts of trouble in the csproj.
And since all our internal nugets are built in two versions: 1.2.3 and 1.2.3-Debug, it would mean that if I switched one PackageReference to Debug in a project (for debugging purposes), all the dependent nugets would also load their debug nugets, which combined with sourcelink would make stepping through code feel as if the nugets are part of the project.
I can’t think of a very clean way to do this, but maybe this will help.
So you’d need to do a restore between switching between debug and release, but something like this could work:
Another approach which might be more magic (but I’m not sure it would actually work) would be to update the Version metadata in a target which runs before Restore.


EDIT: Formatting…
I think you might be able to use a property function for that. https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties?view=vs-2019#property-functions
If that doesn’t work, then you should be able to create different property files corresponding to each build configuration alongside your main project file, then import the appropriate one. Then in your PackageReference, you’d interpolate the property value in the Version string. It’s been a while since I’ve done this, so I don’t remember exactly how to do it. The book I mentioned in my other comment here has instructions on how to do it. Property interpolation in MSBuild is super powerful and a good way to avoid dealing with conditionals. You just need to make sure to be consistent with its use.
C# devs
null reference exceptions

source