Skip to content

Commit 008fccc

Browse files
Copilotdsplaisted
andcommitted
Refactor framework loading to happen only when tool settings not found
Moved framework enumeration logic from ToolPackageInstance constructor into DeserializeToolConfiguration method. The frameworks are now loaded only when the tool settings file is not found, which is when we need to check for framework incompatibility. This reduces unnecessary filesystem operations when the tool installs successfully. Co-authored-by: dsplaisted <[email protected]>
1 parent 1618a37 commit 008fccc

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

src/Cli/dotnet/ToolPackage/ToolPackageInstance.cs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,13 @@ public ToolPackageInstance(PackageId id,
9797
ResolvedPackageVersion = Version;
9898
}
9999

100+
var toolConfiguration = DeserializeToolConfiguration(library, packageDirectory, ResolvedPackageId, _fileSystem);
101+
Warnings = toolConfiguration.Warnings;
102+
100103
var installPath = new VersionFolderPathResolver(PackageDirectory.Value).GetInstallPath(ResolvedPackageId.ToString(), ResolvedPackageVersion);
101104
var toolsPackagePath = Path.Combine(installPath, "tools");
102-
103-
// Get available frameworks before deserializing tool configuration
104-
// This allows us to provide better error messages if the tool requires a higher .NET version
105-
List<NuGetFramework> availableFrameworks = [];
106-
if (_fileSystem.Directory.Exists(toolsPackagePath))
107-
{
108-
availableFrameworks = _fileSystem.Directory.EnumerateDirectories(toolsPackagePath)
109-
.Select(path => NuGetFramework.ParseFolder(Path.GetFileName(path))).ToList();
110-
}
111-
Frameworks = availableFrameworks;
112-
113-
var toolConfiguration = DeserializeToolConfiguration(library, packageDirectory, ResolvedPackageId, availableFrameworks, _fileSystem);
114-
Warnings = toolConfiguration.Warnings;
105+
Frameworks = _fileSystem.Directory.EnumerateDirectories(toolsPackagePath)
106+
.Select(path => NuGetFramework.ParseFolder(Path.GetFileName(path))).ToList();
115107

116108
LockFileItem entryPointFromLockFile = FindItemInTargetLibrary(library, toolConfiguration.ToolAssemblyEntryPoint);
117109
if (entryPointFromLockFile == null)
@@ -173,46 +165,55 @@ public static ToolConfiguration GetToolConfiguration(PackageId id,
173165
{
174166
var lockFile = new LockFileFormat().Read(assetsJsonParentDirectory.WithFile(AssetsFileName).Value);
175167
var lockFileTargetLibrary = FindLibraryInLockFile(lockFile);
176-
// For this method, we don't have framework information available, so pass empty list
177-
return DeserializeToolConfiguration(lockFileTargetLibrary, packageDirectory, id, [], fileSystem);
168+
return DeserializeToolConfiguration(lockFileTargetLibrary, packageDirectory, id, fileSystem);
178169

179170
}
180171

181-
private static ToolConfiguration DeserializeToolConfiguration(LockFileTargetLibrary library, DirectoryPath packageDirectory, PackageId packageId, IReadOnlyList<NuGetFramework> availableFrameworks, IFileSystem fileSystem)
172+
private static ToolConfiguration DeserializeToolConfiguration(LockFileTargetLibrary library, DirectoryPath packageDirectory, PackageId packageId, IFileSystem fileSystem)
182173
{
183174
try
184175
{
185176
var dotnetToolSettings = FindItemInTargetLibrary(library, ToolSettingsFileName);
186177
if (dotnetToolSettings == null)
187178
{
188179
// Check if this is because of framework incompatibility
189-
if (availableFrameworks.Count > 0)
180+
// Load available frameworks from the package to provide better error messages
181+
var installPath = new VersionFolderPathResolver(packageDirectory.Value).GetInstallPath(library.Name, library.Version);
182+
var toolsPackagePath = Path.Combine(installPath, "tools");
183+
184+
if (fileSystem.Directory.Exists(toolsPackagePath))
190185
{
191-
var currentFramework = new NuGetFramework(FrameworkConstants.FrameworkIdentifiers.NetCoreApp, new Version(Environment.Version.Major, Environment.Version.Minor));
192-
193-
// Find the minimum framework version required by the tool
194-
var minRequiredFramework = availableFrameworks
186+
var availableFrameworks = fileSystem.Directory.EnumerateDirectories(toolsPackagePath)
187+
.Select(path => NuGetFramework.ParseFolder(Path.GetFileName(path)))
195188
.Where(f => f.Framework == FrameworkConstants.FrameworkIdentifiers.NetCoreApp)
196-
.MinBy(f => f.Version);
189+
.ToList();
197190

198-
// If all available frameworks require a higher version than current runtime
199-
if (minRequiredFramework != null && minRequiredFramework.Version > currentFramework.Version)
191+
if (availableFrameworks.Count > 0)
200192
{
201-
var requiredVersionString = $".NET {minRequiredFramework.Version.Major}.{minRequiredFramework.Version.Minor}";
202-
var currentVersionString = $".NET {currentFramework.Version.Major}.{currentFramework.Version.Minor}";
203-
204-
var errorMessage = string.Format(
205-
CliStrings.ToolRequiresHigherDotNetVersion,
206-
packageId,
207-
requiredVersionString,
208-
currentVersionString);
209-
210-
var suggestion = string.Format(
211-
CliStrings.ToolRequiresHigherDotNetVersionSuggestion,
212-
minRequiredFramework.Version.Major,
213-
currentFramework.Version.Major);
214-
215-
throw new ToolConfigurationException($"{errorMessage} {suggestion}");
193+
var currentFramework = new NuGetFramework(FrameworkConstants.FrameworkIdentifiers.NetCoreApp, new Version(Environment.Version.Major, Environment.Version.Minor));
194+
195+
// Find the minimum framework version required by the tool
196+
var minRequiredFramework = availableFrameworks.MinBy(f => f.Version);
197+
198+
// If all available frameworks require a higher version than current runtime
199+
if (minRequiredFramework != null && minRequiredFramework.Version > currentFramework.Version)
200+
{
201+
var requiredVersionString = $".NET {minRequiredFramework.Version.Major}.{minRequiredFramework.Version.Minor}";
202+
var currentVersionString = $".NET {currentFramework.Version.Major}.{currentFramework.Version.Minor}";
203+
204+
var errorMessage = string.Format(
205+
CliStrings.ToolRequiresHigherDotNetVersion,
206+
packageId,
207+
requiredVersionString,
208+
currentVersionString);
209+
210+
var suggestion = string.Format(
211+
CliStrings.ToolRequiresHigherDotNetVersionSuggestion,
212+
minRequiredFramework.Version.Major,
213+
currentFramework.Version.Major);
214+
215+
throw new ToolConfigurationException($"{errorMessage} {suggestion}");
216+
}
216217
}
217218
}
218219

0 commit comments

Comments
 (0)