Blogged: Creating source-only NuGet packages

https://andrewlock.net/creating-source-only-nuget-packages/

In this post I show how you can create a NuGet package that contains source code (instead of dlls) which is then compiled into the target project…

#dotnet #nuget

Creating source-only NuGet packages

In this post I show how you can create a NuGet package that contains source code (instead of dlls) which is then compiled into the target project

Andrew Lock | .NET Escapades
@andrewlock Are you aware of a way to discover source-only nuget packages on nuget.org?
@bitbonk unfortunately not, they're not actually "special" so there's no way as far as I know. Frankly discovering anything has always been terrible on nuget.org IMO 😅
@andrewlock Yet another Golang idea incorporated by .NET 👍

@andrewlock I see you enabled nullable, which is quite a minefield with a source-only package that wants to be able to work not only with nullable on or off, but also in a language where nullable isn't an option.

You end up with stuff like this:

https://github.com/xunit/assert.xunit/blob/309647ac2fc29997937a22afaaeb0a73fb80d14b/CollectionAsserts.cs#L1-L8

https://github.com/xunit/assert.xunit/blob/309647ac2fc29997937a22afaaeb0a73fb80d14b/CollectionAsserts.cs#L457-L472

You can't just "#nullable disable" because the compiler might not support it. So if they enable nullable but forget to "tell you", then you have to turn off a bunch of nullable "violations".

assert.xunit/CollectionAsserts.cs at 309647ac2fc29997937a22afaaeb0a73fb80d14b · xunit/assert.xunit

xUnit.net assertion library for sub-module purposes (please open issues in https://github.com/xunit/xunit) - xunit/assert.xunit

GitHub

@andrewlock The only way for me to keep myself sane with all of this is a bunch of projects which re-compile the source in various ways, including minimum compiler versions, and turning flags on and off.

https://github.com/xunit/xunit/tree/main/src/xunit.v3.assert.compat

The "nullable-mixed" version is the one where I've turned on nullable, but did not define XUNIT_NULLABLE. That ends up giving me a bunch of nullable warnings that drive those disable blocks at the tops of all the files.

xunit/src/xunit.v3.assert.compat at main · xunit/xunit

xUnit.net is a free, open source, community-focused unit testing tool for .NET. - xunit/xunit

GitHub

@bradwilson @andrewlock at this point I consider not using Nullable a smell and would berate someone who tried to use my package without it enabled 😂

But I am also a curmudgeon.

@chethusk @andrewlock As someone who had to convert a significant older code base over to nullable, this is *not* a trivial exercise.

@bradwilson @chethusk Yeah, honestly, I decided to just bypass that whole discussion in the post, because I knew it was going to be a nightmare 😅

And also, similarly I _very often_ don't enable Nullable.
- Making the switch is a nightmare, as you pointed out.
- For a huge amount of AspNetCore it fundamentally doesn't work well (see Razor Pages).
- For test projects, I just don't care, and would rather not have the noise of nullables 🤷‍♀️

@andrewlock @chethusk We did the work so you could choose to ignore it. 😉