diff --git a/src/Cli/dotnet/Commands/Sdk/Check/ProductCollectionProvider.cs b/src/Cli/dotnet/Commands/Sdk/Check/ProductCollectionProvider.cs index 780427aa026f..0c3ff39c8940 100644 --- a/src/Cli/dotnet/Commands/Sdk/Check/ProductCollectionProvider.cs +++ b/src/Cli/dotnet/Commands/Sdk/Check/ProductCollectionProvider.cs @@ -30,9 +30,15 @@ public IEnumerable GetProductReleases(Deployment.DotNet.Releases { return product.GetReleasesAsync().Result; } - catch (Exception e) + catch (Exception ex) when (ex is HttpRequestException || + ex is FileNotFoundException || + ex is AggregateException aggregateEx && + (aggregateEx.InnerException is HttpRequestException || + aggregateEx.InnerException is FileNotFoundException)) { - throw new GracefulException(string.Format(CliCommandStrings.ReleasesLibraryFailed, e.Message)); + // Return empty collection if releases are not available (e.g., unreleased preview versions) + // This handles cases where the releases.json file doesn't exist yet for a version + return Enumerable.Empty(); } } } diff --git a/src/Cli/dotnet/Commands/Sdk/Check/SdkOutputWriter.cs b/src/Cli/dotnet/Commands/Sdk/Check/SdkOutputWriter.cs index ce7d7c9df8eb..0ee9e8b5d225 100644 --- a/src/Cli/dotnet/Commands/Sdk/Check/SdkOutputWriter.cs +++ b/src/Cli/dotnet/Commands/Sdk/Check/SdkOutputWriter.cs @@ -68,7 +68,13 @@ private bool NewerSdkPatchExists(NetSdkInfo bundle) private ReleaseVersion? NewestSdkPatchVersion(NetSdkInfo bundle) { - var product = _productCollection.First(product => product.ProductVersion.Equals($"{bundle.Version.Major}.{bundle.Version.Minor}")); + var product = _productCollection.FirstOrDefault(product => product.ProductVersion.Equals($"{bundle.Version.Major}.{bundle.Version.Minor}")); + if (product == null) + { + // No release information available for this SDK version + return null; + } + if (product.LatestSdkVersion.SdkFeatureBand == bundle.Version.SdkFeatureBand) { // This is the latest feature band @@ -88,11 +94,24 @@ private bool NewerSdkPatchExists(NetSdkInfo bundle) private bool NewFeatureBandAvailable() { - return NewestFeatureBandAvailable() > _sdkInfo.Select(sdk => sdk.Version).Max(); + if (!_sdkInfo.Any()) + { + return false; + } + + var newestAvailable = NewestFeatureBandAvailable(); + return newestAvailable != null && newestAvailable > _sdkInfo.Select(sdk => sdk.Version).Max(); } - private ReleaseVersion NewestFeatureBandAvailable() + private ReleaseVersion? NewestFeatureBandAvailable() { - return _productCollection.OrderByDescending(product => product.ProductVersion).First().LatestSdkVersion; + var newestProduct = _productCollection.OrderByDescending(product => product.ProductVersion).FirstOrDefault(); + if (newestProduct != null) + { + return newestProduct.LatestSdkVersion; + } + + // Fallback to the newest installed SDK if no product collection is available + return _sdkInfo.Any() ? _sdkInfo.Select(sdk => sdk.Version).Max() : null; } } diff --git a/test/dotnet.Tests/CommandTests/Sdk/Check/GivenDotnetSdkCheck.cs b/test/dotnet.Tests/CommandTests/Sdk/Check/GivenDotnetSdkCheck.cs index 5782aa2b9022..7fcc0264722d 100644 --- a/test/dotnet.Tests/CommandTests/Sdk/Check/GivenDotnetSdkCheck.cs +++ b/test/dotnet.Tests/CommandTests/Sdk/Check/GivenDotnetSdkCheck.cs @@ -207,6 +207,32 @@ public void ItUsesConfigFile() _reporter.Lines.Should().Contain(replacementString); } + [Fact] + public void WhenSdkHasNoReleasesJsonItShowsVersionCheckUnavailable() + { + var parseResult = Parser.Parse(new string[] { "dotnet", "sdk", "check" }); + // Install SDK 99.0.100 which doesn't have releases.json in the test assets + var bundles = GetFakeEnvironmentInfo(new[] { "3.1.100", "5.0.100", "99.0.100" }, new[] { "3.1.0", "5.0.0" }); + + // This should not throw even though 99.0 doesn't have releases.json + new SdkCheckCommand(parseResult, new MockNETBundleProvider(bundles), new MockProductCollectionProvider(fakeReleasesPath), _reporter).Execute(); + + // Verify all SDKs are shown + foreach (var version in bundles.SdkInfo.Select(b => b.Version.ToString())) + { + string.Join(' ', _reporter.Lines) + .Should() + .Contain(version); + } + + // The SDK without releases should show version check failure + string.Join(' ', _reporter.Lines) + .Should() + .Contain("99.0.100") + .And + .Contain(CliCommandStrings.VersionCheckFailure); + } + private NetEnvironmentInfo GetFakeEnvironmentInfo(IEnumerable sdkVersions, IEnumerable runtimeVersions) { var sdks = sdkVersions.Select(version => new NetSdkInfo(version, string.Empty));