Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating for dependencies that were explicitly installed #667

Merged
merged 3 commits into from
Oct 26, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/NuGetForUnity.Tests/Assets/Tests/Resources/NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
<add key="All" value="(Aggregate source)" />
</activePackageSource>
<config>
<add key="packageInstallLocation" value="CustomWithinAssets" />
<add key="repositoryPath" value="./Packages" />
<add key="PackagesConfigDirectoryPath" value="." />
<add key="slimRestore" value="true" />
<add key="ReadOnlyPackageFiles" value="false" />
<add key="PreferNetStandardOverNetFramework" value="true" />
</config>
</configuration>
30 changes: 26 additions & 4 deletions src/NuGetForUnity/Editor/NugetPackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ public static class NugetPackageInstaller
/// <param name="package">The identifier of the package to install.</param>
/// <param name="refreshAssets">True to refresh the Unity asset database. False to ignore the changes (temporarily).</param>
/// <param name="isSlimRestoreInstall">True to skip checking if lib is imported in Unity and skip installing dependencies.</param>
/// <param name="allowUpdateForExplicitlyInstalled">False to prevent updating of packages that are explicitly installed.</param>
/// <returns>True if the package was installed successfully, otherwise false.</returns>
public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package, bool refreshAssets = true, bool isSlimRestoreInstall = false)
public static bool InstallIdentifier(
[NotNull] INugetPackageIdentifier package,
bool refreshAssets = true,
bool isSlimRestoreInstall = false,
bool allowUpdateForExplicitlyInstalled = true)
{
if (!isSlimRestoreInstall && UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package.Id, false))
{
Expand All @@ -42,7 +47,7 @@ public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package,
}

foundPackage.IsManuallyInstalled = package.IsManuallyInstalled;
return Install(foundPackage, refreshAssets, isSlimRestoreInstall);
return Install(foundPackage, refreshAssets, isSlimRestoreInstall, allowUpdateForExplicitlyInstalled);
}

/// <summary>
Expand All @@ -51,8 +56,13 @@ public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package,
/// <param name="package">The package to install.</param>
/// <param name="refreshAssets">True to refresh the Unity asset database. False to ignore the changes (temporarily).</param>
/// <param name="isSlimRestoreInstall">True to skip checking if lib is imported in Unity and skip installing dependencies.</param>
/// <param name="allowUpdateForExplicitlyInstalled">False to prevent updating of packages that are explicitly installed.</param>
/// <returns>True if the package was installed successfully, otherwise false.</returns>
private static bool Install([NotNull] INugetPackage package, bool refreshAssets, bool isSlimRestoreInstall)
private static bool Install(
[NotNull] INugetPackage package,
bool refreshAssets,
bool isSlimRestoreInstall,
bool allowUpdateForExplicitlyInstalled)
{
if (!isSlimRestoreInstall && UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package.Id, false))
{
Expand All @@ -64,6 +74,18 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets,
if (InstalledPackagesManager.TryGetById(package.Id, out var installedPackage))
{
var comparisonResult = installedPackage.CompareTo(package);

if (!allowUpdateForExplicitlyInstalled && comparisonResult != 0 && installedPackage.IsManuallyInstalled)
{
NugetLogger.LogVerbose(
"{0} {1} is installed explicitly so it will not be replaced with {3}",
installedPackage.Id,
installedPackage.Version,
package.Version,
package.Version);
return true;
}

if (comparisonResult < 0)
{
NugetLogger.LogVerbose(
Expand Down Expand Up @@ -131,7 +153,7 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets,
foreach (var dependency in frameworkGroup.Dependencies)
{
NugetLogger.LogVerbose("Installing Dependency: {0} {1}", dependency.Id, dependency.Version);
var installed = InstallIdentifier(dependency, false);
var installed = InstallIdentifier(dependency, false, false, false);
if (!installed)
{
throw new InvalidOperationException($"Failed to install dependency: {dependency.Id} {dependency.Version}.");
Expand Down
Loading