Skip to content

Commit 2d65e9d

Browse files
committed
Implicitly add PackageReference for NETStandard.Library if targeting .NET Standard
1 parent e7bd0b9 commit 2d65e9d

File tree

9 files changed

+37
-30
lines changed

9 files changed

+37
-30
lines changed

TestAssets/TestProjects/AppWithLibrary/TestLibrary/TestLibrary.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,4 @@
88
<Compile Include="**\*.cs" />
99
<EmbeddedResource Include="**\*.resx" />
1010
</ItemGroup>
11-
<ItemGroup>
12-
<PackageReference Include="NETStandard.Library">
13-
<Version>1.6.0</Version>
14-
</PackageReference>
15-
</ItemGroup>
1611
</Project>

TestAssets/TestProjects/CompilationContext/TestLibrary/TestLibrary.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,4 @@
77
<Compile Include="**\*.cs" />
88
<EmbeddedResource Include="**\*.resx" />
99
</ItemGroup>
10-
<ItemGroup>
11-
<PackageReference Include="NETStandard.Library">
12-
<Version>1.6.0</Version>
13-
</PackageReference>
14-
</ItemGroup>
1510
</Project>

TestAssets/TestProjects/CrossTargeting/DesktopAndNetStandard/DesktopAndNetStandard.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
</PackageReference>
2424
</ItemGroup>
2525
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.5'">
26-
<PackageReference Include="NETStandard.Library">
27-
<Version>1.6.0</Version>
28-
</PackageReference>
2926
<PackageReference Include="Newtonsoft.Json">
3027
<Version>9.0.1</Version>
3128
</PackageReference>

TestAssets/TestProjects/CrossTargeting/NetStandardAndNetCoreApp/NetStandardAndNetCoreApp.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
</PackageReference>
2121
</ItemGroup>
2222
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.5'">
23-
<PackageReference Include="NETStandard.Library">
24-
<Version>1.6.0</Version>
25-
</PackageReference>
2623
<PackageReference Include="Newtonsoft.Json">
2724
<Version>9.0.1</Version>
2825
</PackageReference>

TestAssets/TestProjects/KitchenSink/TestLibrary/TestLibrary.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
</Content>
1313
</ItemGroup>
1414
<ItemGroup>
15-
<PackageReference Include="NETStandard.Library">
16-
<Version>1.6.0</Version>
17-
</PackageReference>
1815
<PackageReference Include="Newtonsoft.Json">
1916
<Version>9.0.1</Version>
2017
</PackageReference>

src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.BeforeCommon.targets

+15
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ Copyright (c) .NET Foundation. All rights reserved.
9292
<Reference Include="System.Xml"/>
9393
</ItemGroup>
9494

95+
<ItemGroup>
96+
<!-- Do a double-exclude dance to filter PackageReference items down to just NETStandard.Library if it exists -->
97+
<_PackageReferenceExceptNETStandardLibrary
98+
Include="@(PackageReference)"
99+
Exclude="NETStandard.Library"/>
100+
<_NETStandardLibraryPackageReference
101+
Include="@(PackageReference)"
102+
Exclude="@(_PackageReferenceExceptNETStandardLibrary)"/>
103+
</ItemGroup>
104+
105+
<!-- Automatically reference NETStandard.Library package if target framework is .NETStandard and the package isn't already referenced -->
106+
<ItemGroup Condition=" '$(DisableImplicitFrameworkReferences)' != 'true' and '$(TargetFrameworkIdentifier)' == '.NETStandard' and '@(_NETStandardLibraryPackageReference)' == ''">
107+
<PackageReference Include="NETStandard.Library" Version="1.6.0"/>
108+
</ItemGroup>
109+
95110
<!-- Add conditional compilation symbols for the target framework (for example NET461, NETSTANDARD2_0, NETCOREAPP1_0) -->
96111
<PropertyGroup Condition=" '$(DisableImplicitFrameworkDefines)' != 'true' and '$(TargetFrameworkIdentifier)' != '.NETPortable'">
97112
<_FrameworkIdentifierForImplicitDefine>$(TargetFrameworkIdentifier.Replace('.', '').ToUpperInvariant())</_FrameworkIdentifierForImplicitDefine>

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

+14
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,20 @@ public void It_implicitly_defines_compilation_constants_for_the_target_framework
455455
shouldCompile = true;
456456
targetFrameworkProperties.Single().SetValue(targetFramework);
457457
}
458+
459+
if (targetFramework == "netcoreapp1.0")
460+
{
461+
var itemGroup = new XElement(ns + "ItemGroup");
462+
project.Root.Add(itemGroup);
463+
464+
XElement packageReference = new XElement(ns + "PackageReference",
465+
new XAttribute("Include", "Microsoft.NETCore.App"),
466+
new XAttribute("Version", "1.0.1")
467+
);
468+
469+
itemGroup.Add(packageReference);
470+
}
471+
458472
})
459473
.Restore(relativePath: "TestLibrary");
460474

test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPreserveCompilationContext.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public void It_publishes_the_project_with_a_refs_folder_and_correct_deps_file()
3434
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").FirstOrDefault();
3535
propertyGroup.Should().NotBeNull();
3636

37-
propertyGroup.Add(new XElement(ns + "DisableImplicitFrameworkReferences", "true"));
37+
propertyGroup.Add(new XElement(ns + "DisableImplicitFrameworkReferences", "true",
38+
new XAttribute("Condition", "'$(TargetFramework)' == 'net46'")));
3839
});
3940

4041
testAsset.Restore("TestApp");

test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,14 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)
127127
// If there are any targets that aren't .NET Framework, add appropriate package reference
128128
if (targetFrameworks.Any(identifier => !GetShortTargetFrameworkIdentifier(identifier).Equals("net", StringComparison.OrdinalIgnoreCase)))
129129
{
130-
XElement packageReference;
130+
XElement packageReference = null;
131131
if (this.IsExe)
132132
{
133133
packageReference = new XElement(ns + "PackageReference",
134134
new XAttribute("Include", "Microsoft.NETCore.App"),
135135
new XAttribute("Version", "1.0.1")
136136
);
137137
}
138-
else
139-
{
140-
packageReference = new XElement(ns + "PackageReference",
141-
new XAttribute("Include", "NETStandard.Library"),
142-
new XAttribute("Version", "1.6.0")
143-
);
144-
}
145138

146139
if (targetFrameworks.Any(identifier => GetShortTargetFrameworkIdentifier(identifier).Equals("net", StringComparison.OrdinalIgnoreCase)))
147140
{
@@ -164,10 +157,13 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)
164157
}
165158
}
166159

167-
packageReference.SetAttributeValue("Condition", condition);
160+
packageReference?.SetAttributeValue("Condition", condition);
168161
}
169162

170-
packageReferenceItemGroup.Add(packageReference);
163+
if (packageReference != null)
164+
{
165+
packageReferenceItemGroup.Add(packageReference);
166+
}
171167
}
172168

173169
if (this.IsExe && targetFrameworks.Any(identifier => GetShortTargetFrameworkIdentifier(identifier).Equals("net", StringComparison.OrdinalIgnoreCase)))

0 commit comments

Comments
 (0)