Skip to content

Commit 0218218

Browse files
committed
work
1 parent dc55bce commit 0218218

12 files changed

Lines changed: 338 additions & 59 deletions

File tree

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyProjectAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private IReadOnlyList<IItem> GetProjectReferences(string[] metadataNames)
143143
List<IItem> references = new List<IItem>();
144144
foreach (Reference6 projectReference in _project4.References)
145145
{
146-
if (projectReference.SourceProject != null && EnvDTEProjectUtility.IsSupported(projectReference.SourceProject, _projectAdapter.VsHierarchy))
146+
if (projectReference.SourceProject != null && _projectAdapter.IsSupported(projectReference))
147147
{
148148
Array metadataElements;
149149
Array metadataValues;

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/VsProjectAdapter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using NuGet.Frameworks;
1818
using NuGet.ProjectManagement;
1919
using NuGet.VisualStudio;
20+
using VSLangProj150;
2021

2122
namespace NuGet.PackageManagement.VisualStudio
2223
{
@@ -276,6 +277,12 @@ public bool IsCapabilityMatch(string capabilityExpression)
276277
return VsHierarchy.IsCapabilityMatch(capabilityExpression);
277278
}
278279

280+
public bool IsSupported(Reference6 projectReference)
281+
{
282+
ThreadHelper.ThrowIfNotOnUIThread();
283+
return EnvDTEProjectUtility.IsSupported(projectReference.SourceProject.Kind, VsHierarchy);
284+
}
285+
279286
#endregion Getters
280287
}
281288
}

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Utility/EnvDTEProjectUtility.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,22 +394,22 @@ public static async Task<bool> IsSupportedAsync(EnvDTE.Project envDTEProject)
394394
Assumes.Present(envDTEProject);
395395
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
396396
var hierarchy = await envDTEProject.ToVsHierarchyAsync();
397-
return IsSupported(envDTEProject, hierarchy);
397+
return IsSupported(envDTEProject.Kind, hierarchy);
398398
}
399399

400-
public static bool IsSupported(EnvDTE.Project envDTEProject, IVsHierarchy hierarchy)
400+
public static bool IsSupported(string projectKind, IVsHierarchy hierarchy)
401401
{
402402
ThreadHelper.ThrowIfNotOnUIThread();
403403

404-
Assumes.Present(envDTEProject);
404+
Assumes.Present(projectKind);
405405
Assumes.Present(hierarchy);
406406

407407
if (VsHierarchyUtility.IsProjectCapabilityCompliant(hierarchy))
408408
{
409409
return true;
410410
}
411411

412-
return envDTEProject.Kind != null && ProjectType.IsSupported(envDTEProject.Kind) && !VsHierarchyUtility.HasUnsupportedProjectCapability(hierarchy);
412+
return projectKind != null && ProjectType.IsSupported(projectKind) && !VsHierarchyUtility.HasUnsupportedProjectCapability(hierarchy);
413413
}
414414

415415
public async static Task<NuGetProject> GetNuGetProjectAsync(EnvDTE.Project project, ISolutionManager solutionManager)

src/NuGet.Clients/NuGet.VisualStudio.Common/IVsProjectAdapter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.VisualStudio.Shell.Interop;
88
using NuGet.Frameworks;
9+
using VSLangProj150;
910

1011
namespace NuGet.VisualStudio
1112
{
@@ -77,5 +78,12 @@ public interface IVsProjectAdapter
7778
/// See <see cref="Microsoft.VisualStudio.Shell.PackageUtilities.IsCapabilityMatch(IVsHierarchy, string)"/>
7879
/// </summary>
7980
bool IsCapabilityMatch(string capabilityExpression);
81+
82+
/// <summary>
83+
/// Checks whether a project reference supports NuGet.
84+
/// </summary>
85+
/// <param name="projectReference"></param>
86+
/// <returns></returns>
87+
bool IsSupported(Reference6 projectReference);
8088
}
8189
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
#nullable enable
2+
const NuGet.Commands.Restore.Utility.PackageSpecFactory.EnvironmentVariableName = "NUGET_USE_NEW_PACKAGESPEC_FACTORY" -> string!
3+
NuGet.Commands.SignArgs.AllowUntrustedRoot.get -> bool
4+
NuGet.Commands.SignArgs.AllowUntrustedRoot.set -> void
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
#nullable enable
2+
const NuGet.Commands.Restore.Utility.PackageSpecFactory.EnvironmentVariableName = "NUGET_USE_NEW_PACKAGESPEC_FACTORY" -> string!
3+
NuGet.Commands.SignArgs.AllowUntrustedRoot.get -> bool
4+
NuGet.Commands.SignArgs.AllowUntrustedRoot.set -> void

src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/PackageSpecFactory.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ private static string[] GetFallbackFolders(string? startupDirectory, string proj
471471
{
472472
// Fallback folders
473473
var currentFallbackFolders = GetValue(
474-
() => fallbackFoldersOverride?.Select(e => UriUtility.GetAbsolutePath(startupDirectory, e)).ToArray(),
474+
() => string.IsNullOrEmpty(startupDirectory) ? null : fallbackFoldersOverride?.Select(e => UriUtility.GetAbsolutePath(startupDirectory, e)).ToArray(),
475475
() => MSBuildRestoreUtility.ContainsClearKeyword(fallbackFolders) ? Array.Empty<string>() : null,
476476
() => fallbackFolders?.Select(e => UriUtility.GetAbsolutePath(projectDirectory, e)).ToArray(),
477477
() => SettingsUtility.GetFallbackPackageFolders(settings).ToArray());
@@ -496,7 +496,10 @@ private static (bool IsEnabled, bool IsVersionOverrideDisabled, bool IsCentralPa
496496
{
497497
if (projectStyle == ProjectStyle.PackageReference)
498498
{
499-
bool isEnabled = IsPropertyTrue(project, "_CentralPackageVersionsEnabled");
499+
bool isEnabled =
500+
MSBuildStringUtility.IsTrue(project.GetProperty("BuildingInsideVisualStudio"))
501+
? IsPropertyTrue(project, "ManagePackageVersionsCentrally")
502+
: IsPropertyTrue(project, "_CentralPackageVersionsEnabled");
500503
bool isVersionOverrideDisabled = IsPropertyFalse(project, "CentralPackageVersionOverrideEnabled");
501504
bool isCentralPackageTransitivePinningEnabled = IsPropertyTrue(project, "CentralPackageTransitivePinningEnabled");
502505
bool isCentralPackageFloatingVersionsEnabled = IsPropertyTrue(project, "CentralPackageFloatingVersionsEnabled");
@@ -842,7 +845,7 @@ private static string[] GetSources(string? startupDirectory, string projectDirec
842845
{
843846
// Sources
844847
var currentSources = GetValue(
845-
() => sourcesOverride?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => UriUtility.GetAbsolutePath(startupDirectory, e)).ToArray(),
848+
() => string.IsNullOrEmpty(startupDirectory) ? null : sourcesOverride?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => UriUtility.GetAbsolutePath(startupDirectory, e)).ToArray(),
846849
() => MSBuildRestoreUtility.ContainsClearKeyword(sources) ? Array.Empty<string>() : null,
847850
() => sources?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => UriUtility.GetAbsolutePath(projectDirectory, e)).ToArray(),
848851
() => (PackageSourceProvider.LoadPackageSources(settings)).Where(e => e.IsEnabled).Select(e => e.Source).ToArray());
@@ -930,7 +933,7 @@ internal static bool IsPropertyTrue(this ITargetFramework project, string proper
930933
return defaultValue;
931934
}
932935

933-
return string.Equals(value, bool.TrueString, StringComparison.OrdinalIgnoreCase);
936+
return string.Equals(value!.Trim(), bool.TrueString, StringComparison.OrdinalIgnoreCase);
934937
}
935938

936939
internal static bool IsPropertyFalse(this ITargetFramework project, string propertyName, bool defaultValue = false)
@@ -942,7 +945,7 @@ internal static bool IsPropertyFalse(this ITargetFramework project, string prope
942945
return defaultValue;
943946
}
944947

945-
return string.Equals(value, bool.FalseString, StringComparison.OrdinalIgnoreCase);
948+
return string.Equals(value!.Trim(), bool.FalseString, StringComparison.OrdinalIgnoreCase);
946949
}
947950

948951
internal static bool IsMetadataTrue(this IItem item, string metadataName, bool defaultValue = false)

test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectFactories.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using NuGet.Versioning;
2222
using NuGet.VisualStudio;
2323
using Test.Utility;
24+
using VSLangProj150;
2425
using Xunit;
2526
using Xunit.Abstractions;
2627

@@ -106,9 +107,13 @@ internal static IVsProjectAdapter CreateProjectAdapter(string fullPath, Mock<IVs
106107
{
107108
var projectAdapter = CreateProjectAdapter(projectBuildProperties);
108109

110+
var projectFilePath = Path.Combine(fullPath, "foo.csproj");
109111
projectAdapter
110112
.Setup(x => x.FullProjectPath)
111-
.Returns(Path.Combine(fullPath, "foo.csproj"));
113+
.Returns(projectFilePath);
114+
projectAdapter
115+
.Setup(x => x.ProjectDirectory)
116+
.Returns(fullPath);
112117
projectAdapter
113118
.Setup(x => x.GetTargetFramework())
114119
.Returns(NuGetFramework.Parse("netstandard13"));
@@ -119,6 +124,19 @@ internal static IVsProjectAdapter CreateProjectAdapter(string fullPath, Mock<IVs
119124
.Setup(x => x.GetMSBuildProjectExtensionsPath())
120125
.Returns(testMSBuildProjectExtensionsPath);
121126

127+
// MSBuild always defines these properties. Set them up so the PackageSpecFactory code path works.
128+
projectBuildProperties
129+
.Setup(x => x.GetPropertyValue("MSBuildProjectName"))
130+
.Returns(Path.GetFileNameWithoutExtension(projectFilePath));
131+
#pragma warning disable CS0618 // GetPropertyValueWithDteFallback is obsolete
132+
projectBuildProperties
133+
.Setup(x => x.GetPropertyValueWithDteFallback(ProjectBuildProperties.MSBuildProjectExtensionsPath))
134+
.Returns(testMSBuildProjectExtensionsPath);
135+
projectBuildProperties
136+
.Setup(x => x.GetPropertyValueWithDteFallback(ProjectBuildProperties.TargetFrameworkMoniker))
137+
.Returns(NuGetFramework.Parse("netstandard13").DotNetFrameworkName);
138+
#pragma warning restore CS0618
139+
122140
return projectAdapter.Object;
123141
}
124142

@@ -138,6 +156,10 @@ internal static Mock<IVsProjectAdapter> CreateProjectAdapter(Mock<IVsProjectBuil
138156
.Setup(x => x.BuildProperties)
139157
.Returns(projectBuildProperties.Object);
140158

159+
projectAdapter
160+
.Setup(x => x.IsSupported(It.IsAny<Reference6>()))
161+
.Returns(true);
162+
141163
return projectAdapter;
142164
}
143165

test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,6 @@ public async Task GetPackageSpecsAsync_WithDefaultVersion_Succeeds(bool usePacka
245245
SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);
246246

247247
Assert.Equal("1.0.0", actualRestoreSpec.Version.ToString());
248-
249-
// Verify
250-
Mock.Get(projectAdapter)
251-
.VerifyGet(x => x.Version, Times.AtLeastOnce);
252-
Mock.Get(projectAdapter)
253-
.VerifyGet(x => x.ProjectName, Times.AtLeastOnce);
254-
Mock.Get(projectAdapter)
255-
.VerifyGet(x => x.FullProjectPath, Times.AtLeastOnce);
256-
Mock.Get(projectAdapter)
257-
.Verify(x => x.GetTargetFramework(), Times.AtLeastOnce);
258248
}
259249
}
260250

@@ -272,6 +262,11 @@ public async Task GetPackageSpecsAsync_WithVersion_Succeeds(bool usePackageSpecF
272262
Mock.Get(projectAdapter)
273263
.SetupGet(x => x.Version)
274264
.Returns("2.2.3");
265+
#pragma warning disable CS0618 // GetPropertyValueWithDteFallback is obsolete
266+
Mock.Get(projectAdapter.BuildProperties)
267+
.Setup(x => x.GetPropertyValueWithDteFallback("Version"))
268+
.Returns("2.2.3");
269+
#pragma warning restore CS0618
275270

276271
var projectServices = new TestProjectSystemServices();
277272

@@ -294,10 +289,6 @@ public async Task GetPackageSpecsAsync_WithVersion_Succeeds(bool usePackageSpecF
294289
SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);
295290

296291
Assert.Equal("2.2.3", actualRestoreSpec.Version.ToString());
297-
298-
// Verify
299-
Mock.Get(projectAdapter)
300-
.Verify(x => x.Version, Times.AtLeastOnce);
301292
}
302293
}
303294

@@ -367,9 +358,6 @@ public async Task GetPackageSpecsAsync_ReadSettingsWithRelativePaths(string? res
367358
var specFallback = actualRestoreSpec.RestoreMetadata.FallbackFolders;
368359
var expectedFolders = fallbackFolders != null ? MSBuildStringUtility.Split(fallbackFolders).Select(e => Path.Combine(testDirectory, e)) : SettingsUtility.GetFallbackPackageFolders(settings);
369360
Assert.True(Enumerable.SequenceEqual(expectedFolders.OrderBy(t => t), specFallback.OrderBy(t => t)));
370-
371-
// Verify
372-
projectBuildProperties.VerifyAll();
373361
}
374362
}
375363

@@ -438,9 +426,6 @@ public async Task GetPackageSpecsAsync_ReadSettingsWithFullPaths(string? restore
438426
var specFallback = actualRestoreSpec.RestoreMetadata.FallbackFolders;
439427
var expectedFolders = fallbackFolders != null ? MSBuildStringUtility.Split(fallbackFolders) : SettingsUtility.GetFallbackPackageFolders(settings);
440428
Assert.True(Enumerable.SequenceEqual(expectedFolders.OrderBy(t => t), specFallback.OrderBy(t => t)));
441-
442-
// Verify
443-
projectBuildProperties.VerifyAll();
444429
}
445430
}
446431

@@ -498,9 +483,6 @@ public async Task GetPackageSpecsAsync_WithPackageTargetFallback_Succeeds(bool u
498483
NuGetFramework.Parse("dnxcore50")
499484
},
500485
((FallbackFramework)actualTfi.FrameworkName).Fallback);
501-
502-
// Verify
503-
projectBuildProperties.VerifyAll();
504486
}
505487
}
506488

@@ -551,12 +533,6 @@ public async Task GetPackageSpecsAsync_WithPackageReference_Succeeds(bool usePac
551533
Assert.NotNull(actualDependency);
552534
Assert.Equal("packageA", actualDependency.LibraryRange.Name);
553535
Assert.Equal(VersionRange.Parse("1.*"), actualDependency.LibraryRange.VersionRange);
554-
555-
// Verify
556-
Mock.Get(projectServices.ReferencesReader)
557-
.Verify(
558-
x => x.GetPackageReferencesAsync(framework, CancellationToken.None),
559-
Times.AtLeastOnce);
560536
}
561537
}
562538

@@ -570,8 +546,6 @@ public async Task GetPackageSpecsAsync_WithProjectReference_Succeeds(bool usePac
570546
// Arrange
571547
using (var randomTestFolder = TestDirectory.Create())
572548
{
573-
var framework = NuGetFramework.Parse("netstandard13");
574-
575549
var projectAdapter = CreateProjectAdapter(randomTestFolder);
576550

577551
var projectServices = new TestProjectSystemServices();
@@ -603,12 +577,6 @@ public async Task GetPackageSpecsAsync_WithProjectReference_Succeeds(bool usePac
603577
var actualDependency = actualRestoreSpec.RestoreMetadata.TargetFrameworks.Single().ProjectReferences.Single();
604578
Assert.NotNull(actualDependency);
605579
Assert.Equal("TestProjectA", actualDependency.ProjectUniqueName);
606-
607-
// Verify
608-
Mock.Get(projectServices.ReferencesReader)
609-
.Verify(
610-
x => x.GetProjectReferencesAsync(It.IsAny<Common.ILogger>(), CancellationToken.None),
611-
Times.AtLeastOnce);
612580
}
613581
}
614582

@@ -651,12 +619,6 @@ public async Task GetInstalledPackagesAsync_WhenValid_ReturnsPackageReferences(b
651619
Assert.Equal(
652620
"packageA.1.0.0",
653621
packageReference.PackageIdentity.ToString());
654-
655-
// Verify
656-
Mock.Get(projectServices.ReferencesReader)
657-
.Verify(
658-
x => x.GetPackageReferencesAsync(framework, CancellationToken.None),
659-
Times.AtLeastOnce);
660622
}
661623
}
662624

@@ -1630,9 +1592,6 @@ public async Task GetPackageSpecsAsync_WithRuntimeIdentifiers_GeneratesRuntimeGr
16301592
// Assert runtime graph
16311593
actualRestoreSpec.RuntimeGraph.Runtimes.Count.Should().Be(runtimeCount);
16321594
actualRestoreSpec.RuntimeGraph.Supports.Count.Should().Be(supportsCount);
1633-
1634-
// Verify
1635-
projectBuildProperties.VerifyAll();
16361595
}
16371596
}
16381597

@@ -1713,8 +1672,6 @@ public async Task GetPackageSpecs_WithWarningProperties(bool usePackageSpecFacto
17131672
warningProperties.WarningsNotAsErrors.Should().HaveCount(1);
17141673
warningProperties.WarningsAsErrors.Should().Contain(NuGetLogCode.NU1803);
17151674
warningProperties.WarningsAsErrors.Should().HaveCount(1);
1716-
// Verify
1717-
projectBuildProperties.VerifyAll();
17181675
}
17191676

17201677
[Theory]

test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/TestVSProjectAdapter.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using NuGet.Frameworks;
1616
using NuGet.ProjectManagement;
1717
using NuGet.VisualStudio;
18+
using VSLangProj150;
1819

1920
namespace NuGet.PackageManagement.VisualStudio.Test
2021
{
@@ -52,7 +53,23 @@ public TestVSProjectAdapter(
5253
_isCentralPackageVersionOverrideEnabled = isCentralPackageVersionOverrideEnabled;
5354
_isCentralPackageTransitivePinningEnabled = CentralPackageTransitivePinningEnabled;
5455

56+
Mock.Get(BuildProperties)
57+
.Setup(x => x.GetPropertyValue("MSBuildProjectName"))
58+
.Returns(projectNames.ShortName);
59+
60+
Mock.Get(BuildProperties)
61+
.Setup(x => x.GetPropertyValue("BuildingInsideVisualStudio"))
62+
.Returns(bool.TrueString);
63+
5564
#pragma warning disable CS0618 // Type or member is obsolete
65+
Mock.Get(BuildProperties)
66+
.Setup(x => x.GetPropertyValueWithDteFallback(It.Is<string>(x => x.Equals(ProjectBuildProperties.TargetFrameworkMoniker))))
67+
.Returns(NuGetFramework.Parse(targetFrameworkString).DotNetFrameworkName);
68+
69+
Mock.Get(BuildProperties)
70+
.Setup(x => x.GetPropertyValueWithDteFallback(It.Is<string>(x => x.Equals(ProjectBuildProperties.MSBuildProjectExtensionsPath))))
71+
.Returns(GetMSBuildProjectExtensionsPath());
72+
5673
Mock.Get(BuildProperties)
5774
.Setup(x => x.GetPropertyValueWithDteFallback(It.Is<string>(x => x.Equals(ProjectBuildProperties.ManagePackageVersionsCentrally))))
5875
.Returns(_isCPVMEnabled.ToString());
@@ -163,5 +180,10 @@ public bool IsCapabilityMatch(string capabilityExpression)
163180
{
164181
throw new NotImplementedException();
165182
}
183+
184+
public bool IsSupported(Reference6 projectReference)
185+
{
186+
return true;
187+
}
166188
}
167189
}

0 commit comments

Comments
 (0)