Skip to content

Commit f1c92d6

Browse files
committed
Handle name conflict between project name and referenced package in GenerateDepsFile
1 parent 497f334 commit f1c92d6

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,6 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
262262

263263
List<ModifiableRuntimeLibrary> runtimeLibraries = new();
264264

265-
if (_includeMainProjectInDepsFile)
266-
{
267-
runtimeLibraries.Add(GetProjectRuntimeLibrary());
268-
}
269-
270265
runtimeLibraries.AddRange(GetRuntimePackLibraries());
271266

272267
foreach (var library in _dependencyLibraries.Values
@@ -304,6 +299,11 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
304299
runtimeLibraries.Add(runtimeLibrary);
305300
}
306301

302+
if (_includeMainProjectInDepsFile)
303+
{
304+
runtimeLibraries.Add(GetProjectRuntimeLibrary());
305+
}
306+
307307
/*
308308
* We now need to modify runtimeLibraries to eliminate those that don't have any runtime assets. We follow the following steps:
309309
* 0. Construct a reverse dependencies list: all runtimeLibraries that depend on this one
@@ -518,7 +518,7 @@ private ModifiableRuntimeLibrary GetProjectRuntimeLibrary()
518518

519519
return new ModifiableRuntimeLibrary(new RuntimeLibrary(
520520
type: "project",
521-
name: _mainProjectInfo.Name,
521+
name: GetUniqueLibraryName(_mainProjectInfo.Name, "Project"),
522522
version: _mainProjectInfo.Version,
523523
hash: string.Empty,
524524
runtimeAssemblyGroups: runtimeAssemblyGroups,
@@ -918,7 +918,7 @@ private string GetReferenceLibraryName(ReferenceInfo reference)
918918
{
919919
// Reference names can conflict with PackageReference names, so
920920
// ensure that the Reference names are unique when creating libraries
921-
name = GetUniqueReferenceName(reference.Name);
921+
name = GetUniqueLibraryName(reference.Name);
922922

923923
ReferenceLibraryNames.Add(reference, name);
924924
_usedLibraryNames.Add(name);
@@ -927,11 +927,11 @@ private string GetReferenceLibraryName(ReferenceInfo reference)
927927
return name;
928928
}
929929

930-
private string GetUniqueReferenceName(string name)
930+
private string GetUniqueLibraryName(string name, string qualifier = "Reference")
931931
{
932932
if (_usedLibraryNames.Contains(name))
933933
{
934-
string startingName = $"{name}.Reference";
934+
string startingName = $"{name}.{qualifier}";
935935
name = startingName;
936936

937937
int suffix = 1;

test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildADesktopLibrary.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#nullable disable
55

6+
using System.Security.Cryptography;
67
using Microsoft.Build.Utilities;
78
using Microsoft.Extensions.DependencyModel;
89

@@ -156,6 +157,24 @@ public void PackageWithoutAssets_ShouldNotShowUpInDepsJson()
156157
}
157158
}
158159

160+
[Fact]
161+
public void ProjectNameCanMatchPackageReferenceName()
162+
{
163+
var testProject = new TestProject()
164+
{
165+
Name = "Newtonsoft.Json",
166+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
167+
};
168+
169+
testProject.PackageReferences.Add(new TestPackageReference("Newtonsoft.Json", ToolsetInfo.GetNewtonsoftJsonPackageVersion()));
170+
testProject.AdditionalProperties["PackageId"] = "Newtonsoft.Json*";
171+
172+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
173+
174+
var buildCommand = new BuildCommand(testAsset);
175+
buildCommand.Execute().Should().Pass();
176+
}
177+
159178
[WindowsOnlyFact]
160179
public void It_can_reference_a_netstandard2_library_and_exchange_types()
161180
{

0 commit comments

Comments
 (0)