Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ internal class VsManagedLanguagesProjectSystemServices

static VsManagedLanguagesProjectSystemServices()
{
_referenceMetadata = Array.CreateInstance(typeof(string), 4);
_referenceMetadata = Array.CreateInstance(typeof(string), 5);
_referenceMetadata.SetValue(ProjectItemProperties.IncludeAssets, 0);
_referenceMetadata.SetValue(ProjectItemProperties.ExcludeAssets, 1);
_referenceMetadata.SetValue(ProjectItemProperties.PrivateAssets, 2);
_referenceMetadata.SetValue(ProjectItemProperties.NoWarn, 3);
_referenceMetadata.SetValue(ProjectItemProperties.GeneratePathProperty, 4);
}

public VsManagedLanguagesProjectSystemServices(
Expand Down Expand Up @@ -194,6 +195,8 @@ private static LibraryDependency ToPackageLibraryDependency(PackageReference ref
{
var dependency = new LibraryDependency
{
AutoReferenced = MSBuildStringUtility.IsTrue(GetReferenceMetadataValue(reference, ProjectItemProperties.IsImplicitlyDefined)),
GeneratePathProperty = MSBuildStringUtility.IsTrue(GetReferenceMetadataValue(reference, ProjectItemProperties.GeneratePathProperty)),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to update _referenceMetadata in order to populate reference instance with GeneratePathProperty metadata.

And please add tests to consume this property inside VS for PackageReference based projects (both SDK/ classic csproj), otherwise we might end up in broken state for commandLine vs VS.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I don't see anywhere that your tests are verifying that PackageReference metadata is read correctly in Visual Studio. Where should I add these tests?

I found LegacyPackageReferenceProjectTests but it doesn't seem related to VsManagedLanguagesProjectSystemServices

Is VsManagedLanguagesProjectSystemServices pulled into a test via MEF and I can't find it?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll have to add End-to-end tests (Apex Tests) to cover this scenario inside Visual Studio. May be @nkolev92 can help you with that...

I'm also adding func tests for LegacyPackageReferenceProject which should add the relevant infrastructure to test these projects inside VS but that will also mock VsManagedLanguagesProjectSystemServices since it calls into Project System.

LibraryRange = new LibraryRange(
name: reference.Name,
versionRange: VersionRange.Parse(reference.Version),
Expand All @@ -206,7 +209,6 @@ private static LibraryDependency ToPackageLibraryDependency(PackageReference ref
GetReferenceMetadataValue(reference, ProjectItemProperties.ExcludeAssets),
GetReferenceMetadataValue(reference, ProjectItemProperties.PrivateAssets));

dependency.AutoReferenced = MSBuildStringUtility.IsTrue(GetReferenceMetadataValue(reference, ProjectItemProperties.IsImplicitlyDefined));

// Add warning suppressions
foreach (var code in MSBuildStringUtility.GetNuGetLogCodes(GetReferenceMetadataValue(reference, ProjectItemProperties.NoWarn)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ private static LibraryDependency ToPackageLibraryDependency(IVsReferenceItem ite

// Mark packages coming from the SDK as AutoReferenced
AutoReferenced = GetPropertyBoolOrFalse(item, "IsImplicitlyDefined"),

GeneratePathProperty = GetPropertyBoolOrFalse(item, "GeneratePathProperty")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also create NET core sdk team to pass this new property GeneratePathProperty in nomination?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mentioned this is a metadata, so you can ignore this comment.

};

// Add warning suppressions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -68,6 +68,7 @@ public override bool Execute()
BuildTasksUtility.CopyPropertyIfExists(msbuildItem, properties, "PrivateAssets");
BuildTasksUtility.CopyPropertyIfExists(msbuildItem, properties, "NoWarn");
BuildTasksUtility.CopyPropertyIfExists(msbuildItem, properties, "IsImplicitlyDefined");
BuildTasksUtility.CopyPropertyIfExists(msbuildItem, properties, "GeneratePathProperty");

entries.Add(new TaskItem(Guid.NewGuid().ToString(), properties));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,19 @@ private static LockFileLibrary CreateLockFileLibrary(LocalPackageInfo package, s

foreach (var file in package.Files)
{
if (!lockFileLib.HasTools && HasTools(file))
{
lockFileLib.HasTools = true;
}
lockFileLib.Files.Add(file);
}

return lockFileLib;
}

private static bool HasTools(string file)
{
return file.StartsWith("tools/", StringComparison.OrdinalIgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ public static List<MSBuildOutputFile> GetMSBuildOutputFiles(PackageSpec project,
var ridlessTargets = assetsFile.Targets
.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier));

var packagesWithTools = new HashSet<string>(assetsFile.Libraries.Where(i => i.HasTools).Select(i => i.Name));

foreach (var ridlessTarget in ridlessTargets)
{
// There could be multiple string matches from the MSBuild project.
Expand Down Expand Up @@ -493,6 +495,31 @@ public static List<MSBuildOutputFile> GetMSBuildOutputFiles(PackageSpec project,

props.AddRange(GenerateGroupsWithConditions(buildPropsGroup, isMultiTargeting, frameworkConditions));

// Create an empty PropertyGroup for package properties
var packagePathsPropertyGroup = MSBuildRestoreItemGroup.Create("PropertyGroup", Enumerable.Empty<XElement>(), 1000, isMultiTargeting ? frameworkConditions : Enumerable.Empty<string>());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything preventing you from using the same pattern as the build props code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is a little different in that its a property group with conditions vs an import group with no conditions containing imports with conditions. But if you feel strongly I can try to follow a pattern

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand correctly but we already add PropertyGroup with condition in nuget.g.props file. wouldn't this be similar to that? We can have a dict of packages path and add them in one shot.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are you adding PropertyGroup condition for ExcludeRestorePackageImports?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are added on line 573.


var projectGraph = targetGraph.Graphs.FirstOrDefault();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the flattened graph?
targetGraph.Flattened?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under the debugger, I found that the first graph was the project itself which has its direct dependencies. Would targetGraph.Flattened give all transitive dependencies as well? Technically the GeneratePathProperty wouldn't be set on any transitive dependencies.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under the debugger, I found that the first graph was the project itself which has its direct dependencies.

I'm not sure if that's the right statement, I'd double check it. Or filter it based on GraphItem


// Distinct union of tool packages and packages with GeneratePathProperty=true
var packageIdsToCreatePropertiesFor = packagesWithTools.Union(projectGraph.Item.Data.Dependencies.Where(i => i.GeneratePathProperty).Select(i => i.Name)).Distinct(StringComparer.OrdinalIgnoreCase);

var localPackages = sortedPackages.Select(e => e.Value);

// Find the packages with matching IDs in the list of sorted packages, filtering out ones that there was no match for or that don't exist
var packagePathProperties = localPackages
.Where(pkg => pkg?.Value?.Package != null && packageIdsToCreatePropertiesFor.Contains(pkg.Value.Package.Id) && pkg.Exists())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hit a case-sensitivity issue here. I believe this contains call is the cause since I don't see how it handles if the winner of the distinct call above had different case than the value in Package.Id.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've hit this too but wasn't able to figure out the problem. I think you're right that its casing. I'll submit a PR

.Select(pkg => pkg.Value.Package)
// Get the property
.Select(GeneratePackagePathProperty);

packagePathsPropertyGroup.Items.AddRange(packagePathProperties);

// Don't bother adding the PropertyGroup if there were no properties added
if (packagePathsPropertyGroup.Items.Any())
{
props.Add(packagePathsPropertyGroup);
}

if (isMultiTargeting)
{
// buildMultiTargeting/ {packageId}.targets
Expand Down Expand Up @@ -720,5 +747,10 @@ private static HashSet<PackageDependencyInfo> ConvertToPackageDependencyInfo(

return result;
}

private static XElement GeneratePackagePathProperty(LocalPackageInfo localPackageInfo)
{
return GenerateProperty($"Pkg{localPackageInfo.Id.Replace(".", "_")}", localPackageInfo.ExpandedPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,17 @@ private static void AddPackageReferences(PackageSpec spec, IEnumerable<IMSBuildI
{
foreach (var item in GetItemByType(items, "Dependency"))
{
var dependency = new LibraryDependency();
var dependency = new LibraryDependency
{
LibraryRange = new LibraryRange(
name: item.GetProperty("Id"),
versionRange: GetVersionRange(item),
typeConstraint: LibraryDependencyTarget.Package),

dependency.LibraryRange = new LibraryRange(
name: item.GetProperty("Id"),
versionRange: GetVersionRange(item),
typeConstraint: LibraryDependencyTarget.Package);
AutoReferenced = IsPropertyTrue(item, "IsImplicitlyDefined"),

dependency.AutoReferenced = IsPropertyTrue(item, "IsImplicitlyDefined");
GeneratePathProperty = IsPropertyTrue(item, "GeneratePathProperty")
};

// Add warning suppressions
foreach (var code in MSBuildStringUtility.GetNuGetLogCodes(item.GetProperty("NoWarn")))
Expand Down
14 changes: 10 additions & 4 deletions src/NuGet.Core/NuGet.LibraryModel/LibraryDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public class LibraryDependency : IEquatable<LibraryDependency>
public IList<NuGetLogCode> NoWarn { get; set; } = new List<NuGetLogCode>();

public string Name => LibraryRange.Name;

/// <summary>
/// True if the PackageReference is added by the SDK and not the user.
/// </summary>
public bool AutoReferenced { get; set; }

public bool GeneratePathProperty { get; set; }

public LibraryDependency() { }

public LibraryDependency(
Expand All @@ -36,14 +38,16 @@ public LibraryDependency(
LibraryIncludeFlags includeType,
LibraryIncludeFlags suppressParent,
IList<NuGetLogCode> noWarn,
bool autoReferenced)
bool autoReferenced,
bool generatePathProperty)
{
LibraryRange = libraryRange;
Type = type;
IncludeType = includeType;
SuppressParent = suppressParent;
NoWarn = noWarn;
AutoReferenced = autoReferenced;
GeneratePathProperty = generatePathProperty;
}

public override string ToString()
Expand Down Expand Up @@ -75,6 +79,7 @@ public override int GetHashCode()
hashCode.AddObject(SuppressParent);
hashCode.AddObject(AutoReferenced);
hashCode.AddSequence(NoWarn);
hashCode.AddObject(GeneratePathProperty);

return hashCode.CombinedHash;
}
Expand All @@ -101,15 +106,16 @@ public bool Equals(LibraryDependency other)
EqualityUtility.EqualsWithNullCheck(Type, other.Type) &&
IncludeType == other.IncludeType &&
SuppressParent == other.SuppressParent &&
NoWarn.SequenceEqualWithNullCheck(other.NoWarn);
NoWarn.SequenceEqualWithNullCheck(other.NoWarn) &&
GeneratePathProperty == other.GeneratePathProperty;
}

public LibraryDependency Clone()
{
var clonedLibraryRange = new LibraryRange(LibraryRange.Name, LibraryRange.VersionRange, LibraryRange.TypeConstraint);
var clonedNoWarn = new List<NuGetLogCode>(NoWarn);

return new LibraryDependency(clonedLibraryRange, Type, IncludeType, SuppressParent, clonedNoWarn, AutoReferenced);
return new LibraryDependency(clonedLibraryRange, Type, IncludeType, SuppressParent, clonedNoWarn, AutoReferenced, GeneratePathProperty);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.


Expand All @@ -14,5 +14,6 @@ public static class ProjectItemProperties
public const string PrivateAssets = "PrivateAssets";
public const string IsImplicitlyDefined = nameof(IsImplicitlyDefined);
public const string NoWarn = nameof(NoWarn);
public const string GeneratePathProperty = "GeneratePathProperty";
}
}
8 changes: 6 additions & 2 deletions src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ private static void PopulateDependencies(
var dependencyExcludeFlagsValue = LibraryIncludeFlags.None;
var suppressParentFlagsValue = LibraryIncludeFlagUtils.DefaultSuppressParent;
var noWarn = new List<NuGetLogCode>();

// This method handles both the dependencies and framework assembly sections.
// Framework references should be limited to references.
// Dependencies should allow everything but framework references.
Expand All @@ -472,6 +472,7 @@ private static void PopulateDependencies(
: LibraryDependencyTarget.All & ~LibraryDependencyTarget.Reference;

var autoReferenced = false;
var generatePathProperty = false;

string dependencyVersionValue = null;
var dependencyVersionToken = dependencyValue;
Expand Down Expand Up @@ -553,6 +554,8 @@ private static void PopulateDependencies(
}

autoReferenced = GetBoolOrFalse(dependencyValue, "autoReferenced", packageSpecPath);

generatePathProperty = GetBoolOrFalse(dependencyValue, "generatePathProperty", packageSpecPath);
}

VersionRange dependencyVersionRange = null;
Expand Down Expand Up @@ -604,7 +607,8 @@ private static void PopulateDependencies(
IncludeType = includeFlags,
SuppressParent = suppressParentFlagsValue,
AutoReferenced = autoReferenced,
NoWarn = noWarn.ToList()
NoWarn = noWarn.ToList(),
GeneratePathProperty = generatePathProperty
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class LockFileFormat
private const string ServicableProperty = "servicable";
private const string Sha512Property = "sha512";
private const string FilesProperty = "files";
private const string HasToolsProperty = "hasTools";
private const string DependenciesProperty = "dependencies";
private const string FrameworkAssembliesProperty = "frameworkAssemblies";
private const string RuntimeProperty = "runtime";
Expand Down Expand Up @@ -240,6 +241,7 @@ private static LockFileLibrary ReadLibrary(string property, JToken json)
library.IsServiceable = ReadBool(json, ServicableProperty, defaultValue: false);
library.Files = ReadPathArray(json[FilesProperty] as JArray, ReadString);

library.HasTools = ReadBool(json, HasToolsProperty, defaultValue: false);
return library;
}

Expand Down Expand Up @@ -268,6 +270,11 @@ private static JProperty WriteLibrary(LockFileLibrary library)
json[MSBuildProjectProperty] = WriteString(library.MSBuildProject);
}

if (library.HasTools)
{
WriteBool(json, HasToolsProperty, library.HasTools);
}

WritePathArray(json, FilesProperty, library.Files, WriteString);
return new JProperty(
library.Name + "/" + library.Version.ToNormalizedString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class LockFileLibrary : IEquatable<LockFileLibrary>
/// </summary>
public string MSBuildProject { get; set; }

public bool HasTools { get; set; }

public bool Equals(LockFileLibrary other)
{
if (other == null)
Expand All @@ -50,6 +52,7 @@ public bool Equals(LockFileLibrary other)
&& string.Equals(Path, other.Path, StringComparison.Ordinal)
&& string.Equals(MSBuildProject, other.MSBuildProject, StringComparison.Ordinal)
&& IsServiceable == other.IsServiceable
&& HasTools == other.HasTools
&& string.Equals(Sha512, other.Sha512, StringComparison.Ordinal)
&& Version == other.Version)
{
Expand All @@ -72,6 +75,7 @@ public override int GetHashCode()
combiner.AddStringIgnoreCase(Type);
combiner.AddObject(Sha512);
combiner.AddObject(IsServiceable);
combiner.AddObject(HasTools);
combiner.AddObject(Version);
combiner.AddObject(Path);
combiner.AddObject(MSBuildProject);
Expand All @@ -96,6 +100,7 @@ public LockFileLibrary Clone()
Type = Type,
Version = Version,
IsServiceable = IsServiceable,
HasTools = HasTools,
Sha512 = Sha512,
Files = Files != null ? new List<string>(Files) : null,
Path = Path,
Expand Down
2 changes: 2 additions & 0 deletions src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ private static void SetDependencies(IObjectWriter writer, string name, IEnumerab
.Where(s => !string.IsNullOrEmpty(s)));
}

SetValueIfTrue(writer, "generatePathProperty", dependency.GeneratePathProperty);

writer.WriteObjectEnd();
}
else
Expand Down
Loading