Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/Cli/dotnet/ToolPackage/LocalToolsResolverCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,35 @@ public void Save(
if (_fileSystem.File.Exists(packageCacheFile))
{
var existingCacheTable = GetCacheTable(packageCacheFile);

var diffedRow = distinctPackageIdAndRestoredCommandMap
.Where(pair => !TryGetMatchingRestoredCommand(
pair.Key,
existingCacheTable, out _))
.Select(pair => ConvertToCacheRow(pair.Key, pair.Value));
var existingCount = existingCacheTable.Length;

foreach (var pair in distinctPackageIdAndRestoredCommandMap)
{
var updatedRow = ConvertToCacheRow(pair.Key, pair.Value);
bool replaced = false;

for (int i = 0; i < existingCount; i++)
{
var existingIdentifier = Convert(distinctPackageId, existingCacheTable[i]).restoredCommandIdentifier;
if (existingIdentifier == pair.Key)
{
existingCacheTable[i] = updatedRow;
replaced = true;
break;
}
}

if (!replaced)
{
Array.Resize(ref existingCacheTable, existingCount + 1);
existingCacheTable[existingCount] = updatedRow;
existingCount++;
}
}

_fileSystem.File.WriteAllText(
packageCacheFile,
JsonSerializer.Serialize(existingCacheTable.Concat(diffedRow)));
JsonSerializer.Serialize(existingCacheTable));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Microsoft.DotNet.Cli.ToolPackage;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.Extensions.DependencyModel.Tests;
Expand Down Expand Up @@ -147,6 +148,37 @@ public void GivenExecutableIdentifierItCanSaveMultipleSameAndLoadContainsOnlyOne
tool2.Should().BeEquivalentTo(restoredCommands[1]);
}

[Fact]
public void GivenExecutableIdentifierItUpdatesExecutablePathOnSubsequentSave()
{
(DirectoryPath nuGetGlobalPackagesFolder, LocalToolsResolverCache localToolsResolverCache) = Setup();

NuGetFramework targetFramework = NuGetFramework.Parse(ToolsetInfo.CurrentTargetFramework);
string runtimeIdentifier = Constants.AnyRid;
PackageId packageId = new("my.toolBundle");
NuGetVersion nuGetVersion = NuGetVersion.Parse("1.0.2");
var commandName = new ToolCommandName("tool1");

var originalCommand = new ToolCommand(commandName, "dotnet", nuGetGlobalPackagesFolder.WithFile("tool1.dll"));
var identifier = new RestoredCommandIdentifier(packageId, nuGetVersion, targetFramework, runtimeIdentifier, commandName);

localToolsResolverCache.Save(new Dictionary<RestoredCommandIdentifier, ToolCommand>
{
[identifier] = originalCommand
});

DirectoryPath alternativePackagesFolder = nuGetGlobalPackagesFolder.GetParentPath().WithSubDirectories("nugetGlobalPackageLocation2");
var updatedCommand = new ToolCommand(commandName, "dotnet", alternativePackagesFolder.WithFile("tool1.dll"));

localToolsResolverCache.Save(new Dictionary<RestoredCommandIdentifier, ToolCommand>
{
[identifier] = updatedCommand
});

localToolsResolverCache.TryLoad(identifier, out ToolCommand restoredCommand).Should().BeTrue();
restoredCommand.Executable.Value.Should().Be(updatedCommand.Executable.Value);
}

[Fact]
public void GivenExecutableIdentifierItCanSaveMultipleVersionAndLoad()
{
Expand Down
Loading