html-agility-pack icon indicating copy to clipboard operation
html-agility-pack copied to clipboard

New nullable issue when on .net 9

Open FvS-MR opened this issue 3 months ago • 7 comments

Hi, like in issue https://github.com/zzzprojects/html-agility-pack/issues/594 i ran into a new nullable issue.

In HtmlNode I see a lot of code like this:

#if NET8_0 HtmlNodeCollection? #else HtmlNodeCollection #endif

This gives me false nullable warnings (return value can not be null regarding the interface) in .net 9 projects. I think all code like this should be changed to

#IF NET8_0_OR_GREATER

FvS-MR avatar Sep 02 '25 16:09 FvS-MR

Hello @FvS-MR,

Could you provide a minimal project that reproduces the warning? I thought compiling HAP with .NET 7 would remove them, as this should start only with projects compiled with .NET 8 or later.

I tried to reproduce it with the latest version but was unsuccessful (it was easy with v1.12.1).

Best Regards,

Jon

JonathanMagnan avatar Sep 02 '25 19:09 JonathanMagnan

Sure. For its really short and I am not allowed to upload a csproj I wil just paste it all as text.

ConsoleApp1.csproj:


<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net9.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="HtmlAgilityPack" Version="1.12.2" />
    </ItemGroup>

</Project>

Program.cs:

using HtmlAgilityPack;

var document = new HtmlDocument();
document.LoadHtml(string.Empty);

//Here my ide shows that the conditional access can be removed
var nodes = document.DocumentNode.SelectNodes("Fake")?.ToArray() ?? [];
Console.WriteLine(nodes.Length);

FvS-MR avatar Sep 03 '25 07:09 FvS-MR

That’s weird — I still don’t get the warning on my brand-new laptop, nor on any of my older PCs.

Image

JonathanMagnan avatar Sep 03 '25 07:09 JonathanMagnan

I am using Rider 2025.2.0.1

Image

FvS-MR avatar Sep 03 '25 09:09 FvS-MR

I described the issue in the #597. One of the issues is that the conditional directives use #if NET8_0, but the library is published under net7. So the minimal fix is to replace #if NET8_0 with #if NET7_0.

Image

To prevent the issue from recurring, it's a good idea to use the *_OR_GREATER directive.

The error is not present when your solution is not using nullable annotations, which might be why there is a difference between @JonathanMagnan and @FvS-MR .

As stated in the #597, I am happy to create a PR for this if there is a desire.

trejjam avatar Oct 03 '25 16:10 trejjam

@JonathanMagnan You can target .NET 8.0 (and later) without nullable analysis. If you don't want to support nullable analysis, you should remove this line:

https://github.com/zzzprojects/html-agility-pack/blob/64a718a815a6a25e03a73d6c53dfc58a7ed692d3/src/HtmlAgilityPack.Net7/HtmlAgilityPack.Net7.csproj#L8

(and then remove any existing nullable annotations from your source files)

wjrogers avatar Oct 15 '25 14:10 wjrogers

Hello @wjrogers ,

Thank you, that will be part for the next time we release this library.

@trejjam , there is already several pull request about this. I will try to make it happens in very early 2026 as so many people ask for it but at this moment, it's just impossible to fully support it.

Best Regards,

Jon

JonathanMagnan avatar Oct 15 '25 15:10 JonathanMagnan