Skip to content

Commit

Permalink
Move NuGet API usage to its own extension
Browse files Browse the repository at this point in the history
NuGet API may not be stable and loading an arbitrary version with MSBuild may cause problems. This change isolates those calls so we can pin it to our own version.

This change does a couple of things:

- Moves all NuGet usages to extension. This will now load in its own AssemblyLoadContext so we won't have any conflicts
- Updates to the latest NuGet client API libraries
- Adds a 'required' extension concept so that they will load in all cases even when loading is disabled.

Fixes #817
  • Loading branch information
twsouthwick committed Aug 25, 2021
1 parent c55585e commit 22609f8
Show file tree
Hide file tree
Showing 41 changed files with 478 additions and 257 deletions.
36 changes: 36 additions & 0 deletions Microsoft.DotNet.UpgradeAssistant.sln
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.UpgradeAss
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.UpgradeAssistant.Steps.Source.Tests", "tests\extensions\default\Microsoft.DotNet.UpgradeAssistant.Steps.Source.Tests\Microsoft.DotNet.UpgradeAssistant.Steps.Source.Tests.csproj", "{134CE4A1-E9A4-49C0-A998-7C531E6207CE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "nuget", "nuget", "{021F1610-F2D0-4F00-81C7-AD7397DA296C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet", "src\extensions\nuget\Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet\Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet.csproj", "{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "nuget", "nuget", "{824AFFC6-40A2-4AD6-8010-B939841CA16E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet.Tests", "tests\extensions\nuget\Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet.Tests\Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet.Tests.csproj", "{58633641-85F3-4F8E-AC18-0EFACC36D142}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -758,6 +766,30 @@ Global
{134CE4A1-E9A4-49C0-A998-7C531E6207CE}.Release|x64.Build.0 = Release|Any CPU
{134CE4A1-E9A4-49C0-A998-7C531E6207CE}.Release|x86.ActiveCfg = Release|Any CPU
{134CE4A1-E9A4-49C0-A998-7C531E6207CE}.Release|x86.Build.0 = Release|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Debug|x64.ActiveCfg = Debug|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Debug|x64.Build.0 = Debug|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Debug|x86.ActiveCfg = Debug|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Debug|x86.Build.0 = Debug|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Release|Any CPU.Build.0 = Release|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Release|x64.ActiveCfg = Release|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Release|x64.Build.0 = Release|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Release|x86.ActiveCfg = Release|Any CPU
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB}.Release|x86.Build.0 = Release|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Debug|x64.ActiveCfg = Debug|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Debug|x64.Build.0 = Debug|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Debug|x86.ActiveCfg = Debug|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Debug|x86.Build.0 = Debug|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Release|Any CPU.Build.0 = Release|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Release|x64.ActiveCfg = Release|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Release|x64.Build.0 = Release|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Release|x86.ActiveCfg = Release|Any CPU
{58633641-85F3-4F8E-AC18-0EFACC36D142}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -831,6 +863,10 @@ Global
{7304F25F-0F4D-450F-A529-3C7AFE9D98CA} = {82BC0AAB-94CA-4D5B-BF95-F7329D1150CB}
{CEDAAB8D-613C-4A3F-8114-7D6B480F1EE3} = {7304F25F-0F4D-450F-A529-3C7AFE9D98CA}
{134CE4A1-E9A4-49C0-A998-7C531E6207CE} = {4E12F429-CEC0-4080-B0FE-39F9B8B9AE4C}
{021F1610-F2D0-4F00-81C7-AD7397DA296C} = {AA22EE67-3BBE-49A2-8868-531FE68FE162}
{E18A3CB0-BFDB-4672-AD53-7EAAF26C52AB} = {021F1610-F2D0-4F00-81C7-AD7397DA296C}
{824AFFC6-40A2-4AD6-8010-B939841CA16E} = {82BC0AAB-94CA-4D5B-BF95-F7329D1150CB}
{58633641-85F3-4F8E-AC18-0EFACC36D142} = {824AFFC6-40A2-4AD6-8010-B939841CA16E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D02F665B-C14D-43C2-955C-9338A23836E0}
Expand Down
Binary file modified docs/images/dependency-validation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions eng/DependencyValidation/DependencyValidation.modelproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
<Name>Microsoft.DotNet.UpgradeAssistant.Extensions.Maui</Name>
<Project>{a122dd5b-4b80-4558-8db2-8e9ebff12a1a}</Project>
</ProjectReference>
<ProjectReference Include="..\..\src\extensions\nuget\Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet\Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet.csproj">
<Name>Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet</Name>
<Project>{e18a3cb0-bfdb-4672-ad53-7eaaf26c52ab}</Project>
</ProjectReference>
<ProjectReference Include="..\..\src\extensions\vb\Microsoft.DotNet.UpgradeAssistant.Extensions.VisualBasic\Microsoft.DotNet.UpgradeAssistant.Extensions.VisualBasic.csproj">
<Name>Microsoft.DotNet.UpgradeAssistant.Extensions.VisualBasic</Name>
<Project>{608e7ce5-5674-49eb-8728-719a468fc62e}</Project>
Expand Down
13 changes: 13 additions & 0 deletions eng/DependencyValidation/UpgradeAssistant.layerdiagram
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@
</reference>
</references>
</layer>
<layer Id="9905d4a3-87a3-46c4-abe5-202ee7b985ff" name="nuget">
<references>
<reference Id="2a788024-d9b8-419e-aa0a-d502a0e6aa6d" name="nuget">
<ArtifactNode Label="nuget" Category="CodeSchema_Project" Id="(Assembly=../../src/extensions/nuget)">
<Category Ref="CodeMap_SolutionFolder" />
</ArtifactNode>
<ExpandedNode Id="(Assembly=Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet)" Label="Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet.dll" Category="CodeSchema_Assembly">
<Category Ref="FileSystem.Category.FileOfType.dll" />
<LinkCategory Ref="Represents" />
</ExpandedNode>
</reference>
</references>
</layer>
</childLayers>
</layer>
<layer Id="421742ad-4313-43e3-bcee-4c01cd3c4c8c" name="Microsoft.DotNet.UpgradeAssistant.Abstractions">
Expand Down
11 changes: 7 additions & 4 deletions eng/DependencyValidation/UpgradeAssistant.layerdiagram.layout
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@
<layerShape Id="6f864661-c4c5-4da9-ad72-13a4bae5670a" absoluteBounds="24, 4, 1.625, 0.5" customColor="161, 199, 231">
<layerMoniker Id="8918b436-d958-4125-b519-fb14baa7dd53" />
</layerShape>
<layerShape Id="38feeace-77a9-4874-8eb3-1b6ccf8abcd0" absoluteBounds="24, 4.75, 1.625, 0.5" customColor="161, 199, 231">
<layerMoniker Id="9905d4a3-87a3-46c4-abe5-202ee7b985ff" />
</layerShape>
</nestedChildShapes>
</layerShape>
<dependencyConnector edgePoints="[(27.09375 : 5.75); (27.09375 : 7.375)]" fixedFrom="NotFixed" fixedTo="NotFixed" customColor="113, 111, 110">
<dependencyConnector edgePoints="[(27.90625 : 5.75); (27.90625 : 7.375)]" fixedFrom="Algorithm" fixedTo="Algorithm" customColor="113, 111, 110">
<dependencyFromLayerToLayerMoniker Id="153f1107-fc9d-4101-843e-a911901b91e4" />
<nodes>
<layerShapeMoniker Id="4be49585-80f9-43a6-9897-b31e107d49a0" />
Expand All @@ -79,7 +82,7 @@
<layerShape Id="936f240c-a956-4a13-a4c1-f7405040ccc5" absoluteBounds="29.5, 2.25, 2, 0.75" customColor="161, 199, 231">
<layerMoniker Id="a9443ba7-6840-4dd8-82e8-ae589e769244" />
</layerShape>
<dependencyConnector edgePoints="[(30.90625 : 3); (30.90625 : 7.375)]" fixedFrom="NotFixed" fixedTo="NotFixed" customColor="113, 111, 110">
<dependencyConnector edgePoints="[(30.90625 : 3); (30.90625 : 7.375)]" fixedFrom="Algorithm" fixedTo="Algorithm" customColor="113, 111, 110">
<dependencyFromLayerToLayerMoniker Id="6ef70d28-03de-4271-98c8-5dcc00559617" />
<nodes>
<layerShapeMoniker Id="936f240c-a956-4a13-a4c1-f7405040ccc5" />
Expand All @@ -103,7 +106,7 @@
<layerShape Id="e78c1014-0b86-46de-873d-5d087cf4a0e6" absoluteBounds="18.5, 6.5, 7, 0.375" customColor="161, 199, 231">
<layerMoniker Id="87aac048-b443-4d5c-bd87-162b1ff74794" />
</layerShape>
<dependencyConnector edgePoints="[(20.8125 : 5.75); (20.8125 : 6.5)]" fixedFrom="Algorithm" fixedTo="Algorithm" customColor="113, 111, 110">
<dependencyConnector edgePoints="[(20.8125 : 5.75); (20.8125 : 6.5)]" fixedFrom="NotFixed" fixedTo="NotFixed" customColor="113, 111, 110">
<dependencyFromLayerToLayerMoniker Id="ca5beb93-d957-49cb-908d-e10527cbe7e4" />
<nodes>
<layerShapeMoniker Id="8d717de7-8a7c-4613-99cc-dbb6565b9cee" />
Expand All @@ -124,7 +127,7 @@
<layerShapeMoniker Id="e78c1014-0b86-46de-873d-5d087cf4a0e6" />
</nodes>
</dependencyConnector>
<dependencyConnector edgePoints="[(22 : 6.875); (22 : 7.375)]" fixedFrom="Algorithm" fixedTo="Algorithm" customColor="113, 111, 110">
<dependencyConnector edgePoints="[(22 : 6.875); (22 : 7.375)]" fixedFrom="NotFixed" fixedTo="NotFixed" customColor="113, 111, 110">
<dependencyFromLayerToLayerMoniker Id="be6af2dc-b697-443b-ba64-fd21151968d8" />
<nodes>
<layerShapeMoniker Id="e78c1014-0b86-46de-873d-5d087cf4a0e6" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
<Extension Include="..\..\extensions\windows\Microsoft.DotNet.UpgradeAssistant.Extensions.Windows\Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.csproj">
<Name>windows</Name>
</Extension>
<Extension Include="..\..\extensions\NuGet\Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet\Microsoft.DotNet.UpgradeAssistant.Extensions.NuGet.csproj">
<Name>nuget</Name>
</Extension>
</ItemGroup>
<ItemGroup>
<Compile Update="LocalizedStrings.Designer.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ public static IHostBuilder UseUpgradeAssistant<TApp>(this IHostBuilder host, IUp
}
});

services.AddNuGet(optionss =>
{
if (upgradeOptions.Project?.FullName is string fullname)
{
optionss.PackageSourcePath = Path.GetDirectoryName(fullname);
}
});

services.AddUserInput();
services.AddAnalysis();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"Extensions": {
"Source": "https://upgradeassistant.blob.core.windows.net/extensions/index.json",

"required": [
"nuget"
],

"Default": [
"default",
"vb",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface IProjectFile

string FilePath { get; }

IEnumerable<NuGetReference> PackageReferences { get; }

void AddFrameworkReferences(IEnumerable<Reference> frameworkReferences);

void RemoveFrameworkReferences(IEnumerable<Reference> frameworkReferences);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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;

namespace Microsoft.DotNet.UpgradeAssistant
{
public interface ITargetFrameworkCollection : IReadOnlyCollection<TargetFrameworkMoniker>
{
void SetTargetFramework(TargetFrameworkMoniker tfm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ private void Load(ExtensionInstance extension, string[] assemblies)
protected override Assembly? Load(AssemblyName assemblyName)
{
// If available in the default, we want to ensure that is used.
var inDefault = Default.Assemblies.FirstOrDefault(a => string.Equals(a.GetName().Name, assemblyName.Name, StringComparison.Ordinal));
var inDefault = Default.Assemblies
.Where(a => !a.GetName().Name!.Contains("NuGet", StringComparison.OrdinalIgnoreCase))
.FirstOrDefault(a => string.Equals(a.GetName().Name, assemblyName.Name, StringComparison.Ordinal));

if (inDefault is Assembly existing)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class ExtensionOptions

public ICollection<string> DefaultExtensions { get; } = new List<string>();

public ICollection<string> RequiredExtensions { get; } = new List<string>();

public ICollection<string> ExtensionPaths { get; } = new List<string>();

public IEnumerable<AdditionalOption> AdditionalOptions { get; set; } = Enumerable.Empty<AdditionalOption>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ public ExtensionProvider(

_extensions = new Lazy<IEnumerable<ExtensionInstance>>(() =>
{
var list = new List<ExtensionInstance>();

var opts = options.Value;

foreach (var path in opts.RequiredExtensions)
{
LoadPath(path, isDefault: true);
}

// Required extensions must load, otherwise they may be turned off
if (!opts.LoadExtensions)
{
return Enumerable.Empty<ExtensionInstance>();
return list;
}

var list = new List<ExtensionInstance>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,22 @@ public static OptionsBuilder<ExtensionOptions> AddDefaultExtensions(this Options
return builder.Configure(options =>
{
const string ExtensionDirectory = "extensions";

var settings = configuration.GetSection("Extensions").Get<ExtensionSettings>();
var defaultExtensions = settings.Default
.Select(n => Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, ExtensionDirectory, n)));

options.DefaultSource = settings.Source;

foreach (var path in defaultExtensions)
AddExtensions(options.DefaultExtensions, settings.Default);
AddExtensions(options.RequiredExtensions, settings.Required);

static void AddExtensions(ICollection<string> collection, string[] names)
{
options.DefaultExtensions.Add(path);
var extensionFullPaths = names
.Select(n => Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, ExtensionDirectory, n)));

foreach (var path in extensionFullPaths)
{
collection.Add(path);
}
}
});
}
Expand All @@ -106,6 +112,8 @@ private class ExtensionSettings
public string Source { get; set; } = string.Empty;

public string[] Default { get; set; } = Array.Empty<string>();

public string[] Required { get; set; } = Array.Empty<string>();
}

public static IServiceCollection AddExtensionOption<TOption>(this IServiceCollection services, TOption option)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.DotNet.UpgradeAssistant.MSBuild
{
internal class Factories
{
private readonly Func<IUpgradeContext, IProject, INuGetReferences> _nugetReferenceFactory;
private readonly Func<IProjectFile, ITargetFrameworkCollection> _tfmCollectionFactory;

public Factories(
Func<IUpgradeContext, IProject, INuGetReferences> nugetReferenceFactory,
Func<IProjectFile, ITargetFrameworkCollection> tfmCollectionFactory)
{
_nugetReferenceFactory = nugetReferenceFactory;
_tfmCollectionFactory = tfmCollectionFactory;
}

public INuGetReferences CreateNuGetReferences(IUpgradeContext context, IProject project) => _nugetReferenceFactory(context, project);

public ITargetFrameworkCollection CreateTfmCollection(IProjectFile project) => _tfmCollectionFactory(project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ProjectRootElement ProjectRoot

public ICollection<string> Sdk => new SdkCollection(ProjectRoot);

public void SetTFM(TargetFrameworkMoniker tfm) => new TargetFrameworkMonikerCollection(this, _comparer).SetTargetFramework(tfm);
public void SetTFM(TargetFrameworkMoniker tfm) => _factories.CreateTfmCollection(this).SetTargetFramework(tfm);

public void AddPackages(IEnumerable<NuGetReference> references)
{
Expand All @@ -61,7 +61,7 @@ public void AddPackages(IEnumerable<NuGetReference> references)

public void RemovePackages(IEnumerable<NuGetReference> references)
{
foreach (var reference in PackageReferences)
foreach (var reference in NuGetReferences.PackageReferences)
{
if (references.Contains(reference))
{
Expand Down Expand Up @@ -184,7 +184,7 @@ public void RemoveProperty(string propertyName)
{
Project.RemoveProperty(property);
}
}
}

private static string GetPathRelativeToProject(string path, string projectDir) =>
Path.IsPathFullyQualified(path)
Expand Down
Loading

0 comments on commit 22609f8

Please sign in to comment.