Skip to content

[11.0-preview.1] Uri.ctor() no longer throws UriFormatException when no host specified #124426

@martincostello

Description

@martincostello

Description

Updating open-telemetry/opentelemetry-dotnet-contrib to add a net11.0 target using .NET 11 preview 1, some of our unit tests fail due to invalid URIs no longer being considered as invalid.

For a value such as "tcp://:11111" it is no longer considered invalid, and instead the Host property of the constructed Uri instance has a value of ":11111".

❯ dotnet test
Restore complete (0.4s)
    info NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  UriRegressionRepro net10.0 succeeded (0.1s) → bin\Debug\net10.0\UriRegressionRepro.dll
  UriRegressionRepro net11.0 succeeded (0.2s) → bin\Debug\net11.0\UriRegressionRepro.dll
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v3.1.5+1b188a7b0a (64-bit .NET 10.0.3)
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v3.1.5+1b188a7b0a (64-bit .NET 11.0.0-preview.1.26104.118)
[xUnit.net 00:00:00.05]   Discovering: UriRegressionRepro
[xUnit.net 00:00:00.06]   Discovering: UriRegressionRepro
[xUnit.net 00:00:00.08]   Discovered:  UriRegressionRepro
[xUnit.net 00:00:00.10]   Starting:    UriRegressionRepro
[xUnit.net 00:00:00.10]   Discovered:  UriRegressionRepro
[xUnit.net 00:00:00.13]   Finished:    UriRegressionRepro
[xUnit.net 00:00:00.11]   Starting:    UriRegressionRepro
[xUnit.net 00:00:00.15]     UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(uri: "udp://:11111") [FAIL]
[xUnit.net 00:00:00.15]       Assert.Throws() Failure: No exception was thrown
[xUnit.net 00:00:00.15]       Expected: typeof(System.UriFormatException)
[xUnit.net 00:00:00.15]       Stack Trace:
[xUnit.net 00:00:00.15]         D:\Coding\_repros\ReproTests.cs(11,0): at UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(String uri)
[xUnit.net 00:00:00.15]            at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
[xUnit.net 00:00:00.15]            at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
[xUnit.net 00:00:00.16]     UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(uri: "unix://:11111") [FAIL]
[xUnit.net 00:00:00.16]       Assert.Throws() Failure: No exception was thrown
[xUnit.net 00:00:00.16]       Expected: typeof(System.UriFormatException)
[xUnit.net 00:00:00.16]       Stack Trace:
[xUnit.net 00:00:00.16]         D:\Coding\_repros\ReproTests.cs(11,0): at UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(String uri)
[xUnit.net 00:00:00.16]            at InvokeStub_UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(Object, Span`1)
[xUnit.net 00:00:00.16]            at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[xUnit.net 00:00:00.16]     UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(uri: "tcp://:11111") [FAIL]
[xUnit.net 00:00:00.16]       Assert.Throws() Failure: No exception was thrown
[xUnit.net 00:00:00.16]       Expected: typeof(System.UriFormatException)
[xUnit.net 00:00:00.16]       Stack Trace:
[xUnit.net 00:00:00.16]         D:\Coding\_repros\ReproTests.cs(11,0): at UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(String uri)
[xUnit.net 00:00:00.16]            at InvokeStub_UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(Object, Span`1)
[xUnit.net 00:00:00.16]            at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[xUnit.net 00:00:00.16]   Finished:    UriRegressionRepro
  UriRegressionRepro test net10.0 succeeded (0.6s)
  UriRegressionRepro test net11.0 failed with 3 error(s) (0.7s)
    D:\Coding\_repros\ReproTests.cs(11): error TESTERROR:
      UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(uri: "udp://:11111") (3ms): Error Messag
      e: Assert.Throws() Failure: No exception was thrown
      Expected: typeof(System.UriFormatException)
      Stack Trace:
         at UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(String uri) in D:\Coding\_repros\R
      eproTests.cs:line 11
         at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
         at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags
       invokeAttr)
    D:\Coding\_repros\ReproTests.cs(11): error TESTERROR:
      UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(uri: "unix://:11111") (< 1ms): Error Mes
      sage: Assert.Throws() Failure: No exception was thrown
      Expected: typeof(System.UriFormatException)
      Stack Trace:
         at UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(String uri) in D:\Coding\_repros\R
      eproTests.cs:line 11
         at InvokeStub_UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(Object, Span`1)
         at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Obj
      ect[] parameters, CultureInfo culture)
    D:\Coding\_repros\ReproTests.cs(11): error TESTERROR:
      UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(uri: "tcp://:11111") (< 1ms): Error Mess
      age: Assert.Throws() Failure: No exception was thrown
      Expected: typeof(System.UriFormatException)
      Stack Trace:
         at UriRegressionRepro.UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(String uri) in D:\Coding\_repros\R
      eproTests.cs:line 11
         at InvokeStub_UriTests.Uri_Constructor_Should_Throw_When_No_Hostname(Object, Span`1)
         at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Obj
      ect[] parameters, CultureInfo culture)

Test summary: total: 6, failed: 3, succeeded: 3, skipped: 0, duration: 0.7s
Build failed with 3 error(s) in 1.6s

Reproduction Steps

namespace UriRegressionRepro;

public class UriTests
{
    [Theory]
    [InlineData("tcp://:11111")]
    [InlineData("udp://:11111")]
    [InlineData("unix://:11111")]
    public void Uri_Constructor_Should_Throw_When_No_Host(string uri)
    {
        Assert.Throws<UriFormatException>(() => new Uri(uri));
    }
}
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <ImplicitUsings>enable</ImplicitUsings>
    <TargetFrameworks>net11.0;net10.0</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
    <PackageReference Include="xunit" Version="2.9.3" />
    <PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
  </ItemGroup>
  <ItemGroup>
    <Using Include="Xunit" />
  </ItemGroup>
</Project>

Expected behavior

All three values cause an UriFormatException to be thrown.

Actual behavior

All three values create a Uri with a Host value of ":11111" and do not throw an exception.

Regression?

Yes.

Known Workarounds

No response

Configuration

11.0.100-preview.1.26104.118

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions