Skip to content

Commit 9475b64

Browse files
authored
Remove ReleaseVersion from interface definition and ProjectRetargetHandler to avoid premature image load of assembly (#9779)
We will only use this internally in DotNetReleasesProvider
1 parent 1b33c2e commit 9475b64

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/Retargeting/DotNetReleasesProvider.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ private Task CreateDefaultFileIfNotExistAsync(string path, string resource, bool
115115
return await _product.GetValueAsync(cancellationToken);
116116
}
117117

118-
public async Task<ReleaseVersion?> GetSupportedOrLatestSdkVersionAsync(
119-
ReleaseVersion? sdkVersion,
118+
public async Task<string?> GetSupportedOrLatestSdkVersionAsync(
119+
string? sdkVersion,
120120
bool includePreview = false,
121121
CancellationToken cancellationToken = default)
122122
{
@@ -129,27 +129,30 @@ private Task CreateDefaultFileIfNotExistAsync(string path, string resource, bool
129129

130130
if (sdkVersion is not null)
131131
{
132-
// Find the product that matches the major/minor version of the SDK
133-
Product matchingProduct = products.FirstOrDefault( p => p.LatestSdkVersion.Major == sdkVersion.Major &&
134-
p.LatestSdkVersion.Minor == sdkVersion.Minor && p.LatestSdkVersion.IsLaterThan(sdkVersion));
135-
136-
if (matchingProduct is not null)
132+
if (ReleaseVersion.TryParse(sdkVersion, out ReleaseVersion parsedSdkVersion))
137133
{
138-
try
139-
{
140-
return await GetLatestSupportedSdkVersionAsync(sdkVersion, includePreview, matchingProduct);
141-
}
142-
catch
134+
// Find the product that matches the major/minor version of the SDK
135+
Product matchingProduct = products.FirstOrDefault(p => p.LatestSdkVersion.Major == parsedSdkVersion.Major &&
136+
p.LatestSdkVersion.Minor == parsedSdkVersion.Minor && p.LatestSdkVersion.IsLaterThan(sdkVersion));
137+
138+
if (matchingProduct is not null)
143139
{
144-
// we can just fall through and return null here
140+
try
141+
{
142+
return await GetLatestSupportedSdkVersionAsync(parsedSdkVersion, includePreview, matchingProduct);
143+
}
144+
catch
145+
{
146+
// we can just fall through and return null here
147+
}
145148
}
146149
}
147150
}
148151

149152
return null;
150153
}
151154

152-
private async Task<ReleaseVersion?> GetLatestSupportedSdkVersionAsync(ReleaseVersion? currentVersion, bool includePreview, Product matchingProduct)
155+
private async Task<string?> GetLatestSupportedSdkVersionAsync(ReleaseVersion? currentVersion, bool includePreview, Product matchingProduct)
153156
{
154157
string resourceFileName = Path.Combine(AppDataPath, RetargetingAppDataFolder, $"{matchingProduct.ProductVersion}{ReleasesFileName}");
155158

@@ -197,15 +200,15 @@ private Task CreateDefaultFileIfNotExistAsync(string path, string resource, bool
197200
if (currentVersion is not null
198201
&& string.Equals(currentVersion.ToString(), latestSdk.Version.ToString(), StringComparison.OrdinalIgnoreCase))
199202
{
200-
return currentVersion;
203+
return currentVersion.ToString();
201204
}
202205

203-
return ReleaseVersion.Parse(latestSdk.DisplayVersion.ToString());
206+
return latestSdk.DisplayVersion.ToString();
204207
}
205208

206209
return null;
207210
}
208-
211+
209212
private async Task<IReadOnlyCollection<ProductRelease>?> GetReleasesAsync(Product product, string resourceFileName, string version, bool forceLocalFile = false, CancellationToken cancellationToken = default)
210213
{
211214
try

src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/Retargeting/IDotNetReleasesProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
22

3-
using Microsoft.Deployment.DotNet.Releases;
4-
53
namespace Microsoft.VisualStudio.ProjectSystem.VS.Retargeting;
64

75
/// <summary>
@@ -18,8 +16,8 @@ internal interface IDotNetReleasesProvider
1816
/// <returns>
1917
/// A task that represents the asynchronous operation. The task result contains the supported or latest SDK version as a string, or null if no suitable version is found.
2018
/// </returns>
21-
Task<ReleaseVersion?> GetSupportedOrLatestSdkVersionAsync(
22-
ReleaseVersion? sdkVersion,
19+
Task<string?> GetSupportedOrLatestSdkVersionAsync(
20+
string? sdkVersion,
2321
bool includePreview = false,
2422
CancellationToken cancellationToken = default);
2523
}

src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/Retargeting/ProjectRetargetHandler.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
22

33
using System.Text.Json;
4-
using Microsoft.Deployment.DotNet.Releases;
54
using Microsoft.VisualStudio.Shell;
65
using Microsoft.VisualStudio.Shell.Interop;
76
using Microsoft.VisualStudio.Threading;
@@ -76,14 +75,14 @@ public Task RetargetAsync(TextWriter outputLogger, RetargetOptions options, IPro
7675

7776
private async Task<TargetChange?> GetTargetChangeAsync(IVsTrackProjectRetargeting2 retargetingService)
7877
{
79-
ReleaseVersion? sdkVersion = await GetSdkVersionForProjectAsync();
78+
string? sdkVersion = await GetSdkVersionForProjectAsync();
8079

8180
if (sdkVersion is null)
8281
{
8382
return null;
8483
}
8584

86-
ReleaseVersion? retargetVersion = await _releasesProvider.Value.GetSupportedOrLatestSdkVersionAsync(sdkVersion, includePreview: true);
85+
string? retargetVersion = await _releasesProvider.Value.GetSupportedOrLatestSdkVersionAsync(sdkVersion, includePreview: true);
8786

8887
if (retargetVersion is null || sdkVersion == retargetVersion)
8988
{
@@ -132,7 +131,7 @@ public Task RetargetAsync(TextWriter outputLogger, RetargetOptions options, IPro
132131
return null;
133132
}
134133

135-
private async Task<ReleaseVersion?> GetSdkVersionForProjectAsync()
134+
private async Task<string?> GetSdkVersionForProjectAsync()
136135
{
137136
string projectDirectory = _unconfiguredProject.GetProjectDirectory();
138137

@@ -148,12 +147,7 @@ public Task RetargetAsync(TextWriter outputLogger, RetargetOptions options, IPro
148147
if (doc.RootElement.TryGetProperty("sdk", out JsonElement sdkProp) &&
149148
sdkProp.TryGetProperty("version", out JsonElement versionProp))
150149
{
151-
if (ReleaseVersion.TryParse(versionProp.GetString(), out ReleaseVersion parsedVersions))
152-
{
153-
return parsedVersions;
154-
}
155-
156-
return null;
150+
return versionProp.GetString();
157151
}
158152
}
159153
catch /* ignore errors */

0 commit comments

Comments
 (0)