Skip to content

Commit 910f619

Browse files
authored
Use ProjectReferences in libs shared framework source projects (#116772)
* Use P2Ps in inbox src libs * Additional changes needed to have all references available * Set PackageId for non-packable projects to differentiate identity * React to S.Threading.AccessControl moving inbox * PR feedback
1 parent ceede7f commit 910f619

File tree

105 files changed

+888
-903
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+888
-903
lines changed

docs/coding-guidelines/project-guidelines.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,19 @@ src\<Library Name>\tests - Contains the test code for a library.
151151
## ref
152152
Reference assemblies are required for any library that has more than one implementation or uses a facade. A reference assembly is a surface-area-only assembly that represents the public API of the library. To generate a reference assembly source file you can use the [GenAPI tool](https://www.nuget.org/packages/Microsoft.DotNet.BuildTools.GenAPI). If a library is a pure portable library with a single implementation it need not use a reference assembly at all. Instructions on updating reference sources can be found [here](https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/updating-ref-source.md).
153153

154-
In the ref directory for the library there should be at most **one** `.csproj` that contains the latest API for the reference assembly for the library. That project can contain multiple entries in its `TargetFrameworks` property. Ref projects should use `<ProjectReference>` for its dependencies.
154+
In the ref directory for the library there should be at most **one** `.csproj` that contains the latest API for the reference assembly for the library. That project can contain multiple entries in its `TargetFrameworks` property.
155155

156156
### ref output
157157
All ref outputs should be under
158158

159159
`bin\$(MSBuildProjectName)\ref\$(TargetFramework)`
160160

161161
## src
162-
In the src directory for a library there should be only **one** `.csproj` file that contains any information necessary to build the library in various target frameworks. All supported target frameworks should be listed in the `TargetFrameworks` property.
162+
In the src directory for a library there should be only **one** `.csproj` file that contains any information necessary to build the library for various target frameworks. All supported target frameworks should be listed in the `TargetFramework` or `TargetFrameworks` property.
163163

164-
All libraries should use `<Reference Include="..." />` for all their references to libraries that compose the shared framework of the current .NETCoreApp. That will cause them to be resolved against the locally built targeting pack which is located at `artifacts\bin\microsoft.netcore.app.ref`. The only exception to that rule right now is for partial facades which directly reference System.Private.CoreLib and thus need to directly reference other partial facades to avoid type conflicts.
164+
Libraries should use `ProjectReference` items to reference live dependencies.
165165

166-
Other target frameworks than .NETCoreApp latest (i.e. `netstandard2.0`, `net462`, `net8.0`) should use ProjectReference items to reference dependencies.
166+
`Reference` items should only be used when targeting .NET Framework to reference prebuilt assemblies from the targeting pack and which aren't included by default (i.e. `System.DirectoryServices`). For anything else, `Reference` items should not be used as they are not supported in those frameworks.
167167

168168
### src\ILLink
169169
Contains the files used to direct the trimming tool. See [ILLink files](../workflow/trimming/ILLink-files.md).

eng/generators.targets

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
(
2626
'$(DisableImplicitFrameworkReferences)' == 'true' and
2727
(
28-
'@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))' == 'true' or
28+
'@(ProjectReference->AnyHaveMetadataValue('Filename', 'System.Runtime.InteropServices'))' == 'true' or
2929
'@(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))' == 'true'
3030
)
3131
)" />
@@ -38,7 +38,7 @@
3838
'$(MSBuildProjectExtension)' == '.csproj' and
3939
(
4040
'$(DisableImplicitFrameworkReferences)' == 'true' and
41-
'@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))' == 'true'
41+
'@(ProjectReference->AnyHaveMetadataValue('Filename', 'System.Runtime.InteropServices'))' == 'true'
4242
)" />
4343
</ItemGroup>
4444

@@ -49,7 +49,7 @@
4949
</ItemGroup>
5050

5151
<!-- Use this complex item list based filtering to add the ProjectReference to make sure dotnet/runtime stays compatible with NuGet Static Graph Restore.
52-
That is required as the EnabledGenerators condition checks on the Reference and ProjectReference items and hence can't be a property condition. -->
52+
That is required as the EnabledGenerators condition checks on the ProjectReference items and hence can't be a property condition. -->
5353
<ItemGroup Condition="'@(EnabledGenerators)' != ''">
5454
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\Microsoft.Interop.SourceGeneration\Microsoft.Interop.SourceGeneration.csproj"
5555
ReferenceOutputAssembly="false"

eng/references.targets

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<Project>
2+
23
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
34
<!--
45
Disable RAR from transitively discovering dependencies for references. This is required as we don't copy
@@ -14,34 +15,35 @@
1415
</ProjectReference>
1516
</ItemDefinitionGroup>
1617

18+
<!-- Set the corresponding CoreLib runtime configuration. -->
1719
<ItemGroup Condition="'@(ProjectReference)' != ''">
1820
<_coreLibProjectReference Include="@(ProjectReference->WithMetadataValue('Identity', '$(CoreLibProject)'))" />
19-
<ProjectReference Update="@(_coreLibProjectReference)"
20-
Private="false">
21+
<ProjectReference Update="@(_coreLibProjectReference)">
2122
<SetConfiguration Condition="'$(RuntimeFlavor)' == 'CoreCLR' and
2223
'$(Configuration)' != '$(CoreCLRConfiguration)'">Configuration=$(CoreCLRConfiguration)</SetConfiguration>
2324
<SetConfiguration Condition="'$(RuntimeFlavor)' == 'Mono' and
2425
'$(Configuration)' != '$(MonoConfiguration)'">Configuration=$(MonoConfiguration)</SetConfiguration>
2526
</ProjectReference>
26-
<!-- If a CoreLib ProjectReference is present, make all P2P assets non transitive. -->
27-
<ProjectReference Update="@(ProjectReference->WithMetadataValue('PrivateAssets', ''))"
28-
PrivateAssets="all"
29-
Condition="'$(IsSourceProject)' == 'true' and '@(_coreLibProjectReference)' != ''" />
3027
</ItemGroup>
3128

32-
<!-- Make shared framework assemblies not app-local (non private). -->
33-
<Target Name="UpdateProjectReferencesWithPrivateAttribute"
34-
AfterTargets="AssignProjectConfiguration"
35-
BeforeTargets="PrepareProjectReferences"
36-
Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and
37-
('$(IsTestProject)' == 'true' or '$(IsTestSupportProject)' == 'true') and
38-
'@(ProjectReferenceWithConfiguration)' != ''">
39-
<ItemGroup>
40-
<ProjectReferenceWithConfiguration PrivateAssets="all"
41-
Private="false"
42-
Condition="$(NetCoreAppLibrary.Contains('%(Filename);')) and '%(ProjectReferenceWithConfiguration.Private)' == ''" />
43-
</ItemGroup>
44-
</Target>
29+
<!-- Mark shared framework assemblies as non-transitive (privateassets=all) and non app-local (private=false). -->
30+
<ItemGroup Condition="'@(ProjectReference)' != '' and
31+
'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and
32+
'$(TargetFrameworkVersion)' == 'v$(NetCoreAppCurrentVersion)'">
33+
<_ProjectReferenceWithOriginalIdentity Include="@(ProjectReference)"
34+
OriginalIdentity="%(Identity)" />
35+
<_projectReferenceWithFilename Include="@(_ProjectReferenceWithOriginalIdentity->Metadata('Filename'))" />
36+
37+
<_projectReferenceExcludedWithFilename Include="@(_projectReferenceWithFilename)"
38+
Exclude="@(NetCoreAppLibrary)" />
39+
<_frameworkProjectReference Include="@(_projectReferenceWithFilename)"
40+
Exclude="@(_projectReferenceExcludedWithFilename)" />
41+
42+
<ProjectReference Update="@(_frameworkProjectReference->Metadata('OriginalIdentity'))">
43+
<PrivateAssets Condition="'%(PrivateAssets)' == ''">all</PrivateAssets>
44+
<Private Condition="'%(Private)' == ''">false</Private>
45+
</ProjectReference>
46+
</ItemGroup>
4547

4648
<Target Name="ReplaceCoreLibSrcWithRefAssemblyForCompilation"
4749
AfterTargets="FindReferenceAssembliesForReferences"
@@ -52,4 +54,5 @@
5254
<ReferencePathWithRefAssemblies Include="@(_resolvedCoreLibProjectReference->Metadata('ReferenceAssembly'))" />
5355
</ItemGroup>
5456
</Target>
57+
5558
</Project>

eng/resolveContract.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<PropertyGroup>
120120
<!-- Let GenFacades use roslyn from the toolset package as it loads sources which might require newer language features. -->
121121
<GenFacadesUseRoslynToolsetPackagePath>true</GenFacadesUseRoslynToolsetPackagePath>
122+
<GenFacadesReferencePathItemName>ReferencePathWithRefAssemblies</GenFacadesReferencePathItemName>
122123
</PropertyGroup>
123124

124125
<!-- ##### GenAPI settings ##### -->

eng/targetingpacks.targets

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
<!--
2-
The following properties need to be set for this logic to work correctly:
3-
- ProductVersion
4-
- NetCoreAppCurrent
5-
- NetCoreAppCurrentVersion
6-
- MicrosoftNetCoreAppFrameworkName
7-
- MicrosoftNetCoreAppRefPackDir
8-
- optional: MicrosoftNetCoreAppRuntimePackDir
9-
- optional: AppHostSourcePath & SingleFileHostSourcePath
10-
- optional: Crossgen2Dir
11-
-->
12-
131
<Project>
2+
<!--
3+
The following properties need to be set for this logic to work correctly:
4+
- ProductVersion
5+
- NetCoreAppCurrent
6+
- NetCoreAppCurrentVersion
7+
- MicrosoftNetCoreAppFrameworkName
8+
- MicrosoftNetCoreAppRefPackDir
9+
- optional: MicrosoftNetCoreAppRuntimePackDir
10+
- optional: AppHostSourcePath & SingleFileHostSourcePath
11+
- optional: Crossgen2Dir
12+
-->
13+
1414
<PropertyGroup>
1515
<LocalFrameworkOverrideName>$(MicrosoftNetCoreAppFrameworkName)</LocalFrameworkOverrideName>
1616
<TargetingpacksTargetsImported>true</TargetingpacksTargetsImported>
@@ -90,26 +90,6 @@
9090
Condition="'$(UseLocalAppHostPack)' == 'true' and '@(KnownAppHostPack->AnyHaveMetadataValue('TargetFramework', '$(NetCoreAppCurrent)'))' != 'true'" />
9191
</ItemGroup>
9292

93-
<!-- Simple name references will be resolved from the targeting pack folders and should never be copied to the output. -->
94-
<ItemGroup>
95-
<Reference Update="@(Reference)">
96-
<Private Condition="'%(Reference.Extension)' != '.dll'">false</Private>
97-
</Reference>
98-
</ItemGroup>
99-
100-
<!-- Add the resolved targeting pack to the assembly search path. -->
101-
<Target Name="UseTargetingPackForAssemblySearchPaths"
102-
BeforeTargets="ResolveAssemblyReferences;
103-
DesignTimeResolveAssemblyReferences"
104-
Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and
105-
'$(TargetFrameworkVersion)' == 'v$(NetCoreAppCurrentVersion)' and
106-
'$(DisableImplicitFrameworkReferences)' == 'true'">
107-
<PropertyGroup>
108-
<AssemblySearchPaths>$(AssemblySearchPaths);$(MicrosoftNetCoreAppRefPackRefDir.TrimEnd('/\'))</AssemblySearchPaths>
109-
<DesignTimeAssemblySearchPaths>$(DesignTimeAssemblySearchPaths);$(MicrosoftNetCoreAppRefPackRefDir.TrimEnd('/\'))</DesignTimeAssemblySearchPaths>
110-
</PropertyGroup>
111-
</Target>
112-
11393
<!-- Use local targeting/runtime pack for NetCoreAppCurrent. -->
11494
<Target Name="UpdateLocalTargetingAndRuntimePack"
11595
Condition="'$(UseLocalTargetingRuntimePack)' == 'true'"
@@ -197,4 +177,5 @@
197177
<IlcFrameworkNativePath>$(BootstrapRuntimePackDir)/runtimes/$(TargetRid)/native/</IlcFrameworkNativePath>
198178
<IlcSdkPath>$(BootstrapAotSdkDir)/</IlcSdkPath>
199179
</PropertyGroup>
180+
200181
</Project>

src/libraries/Directory.Build.targets

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<UseLocalTargetingRuntimePack Condition="'$(IsNETCoreAppAnalyzer)' == 'true'">false</UseLocalTargetingRuntimePack>
6060
<!-- By default, disable implicit framework references for NetCoreAppCurrent libraries. -->
6161
<DisableImplicitFrameworkReferences Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and
62-
$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '$(NETCoreAppCurrentVersion)')) and
62+
'$(TargetFrameworkVersion)' == 'v$(NetCoreAppCurrentVersion)' and
6363
('$(IsNETCoreAppRef)' == 'true' or '$(IsNETCoreAppSrc)' == 'true')">true</DisableImplicitFrameworkReferences>
6464
<!-- Enable trimming for any source project that's part of the shared framework.
6565
Don't attempt to trim PNSE assemblies which are generated from the reference source. -->
@@ -233,4 +233,14 @@
233233
</ItemGroup>
234234
</When>
235235
</Choose>
236+
237+
<!-- Add a meaningless "-project" suffix to the package id for non-packable source projects.
238+
NuGet uses PackageId regardless of whether the project is packable as the key for restore
239+
graph project nodes. This is important so that NuGet doesn't get confused when a transitive
240+
package reference and a project reference with the same key is in the graph.
241+
See https://github.com/NuGet/Home/issues/10368 for more details. -->
242+
<PropertyGroup>
243+
<PackageId Condition="'$(IsSourceProject)' == 'true' and '$(IsPackable)' != 'true'">$(MSBuildProjectName)-project</PackageId>
244+
</PropertyGroup>
245+
236246
</Project>

src/libraries/Microsoft.CSharp/src/Microsoft.CSharp.csproj

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,22 @@
239239
<Compile Include="Microsoft\CSharp\RuntimeBinder\ComInterop\VariantArray.cs" />
240240
<Compile Include="Microsoft\CSharp\RuntimeBinder\ComInterop\VariantBuilder.cs" />
241241

242-
<Reference Include="System.Reflection.Emit" />
243-
<Reference Include="System.Reflection.Emit.ILGeneration" />
244-
<Reference Include="System.Reflection.Emit.Lightweight" />
245-
<Reference Include="System.Reflection.Primitives" />
242+
<ProjectReference Include="$(LibrariesProjectRoot)System.Reflection.Emit\src\System.Reflection.Emit.csproj" />
243+
<ProjectReference Include="$(LibrariesProjectRoot)System.Reflection.Emit.ILGeneration\src\System.Reflection.Emit.ILGeneration.csproj" />
244+
<ProjectReference Include="$(LibrariesProjectRoot)System.Reflection.Emit.Lightweight\src\System.Reflection.Emit.Lightweight.csproj" />
245+
<ProjectReference Include="$(LibrariesProjectRoot)System.Reflection.Primitives\src\System.Reflection.Primitives.csproj" />
246246
</ItemGroup>
247247

248248
<ItemGroup>
249-
<Reference Include="System.Collections" />
250-
<Reference Include="System.Collections.Concurrent" />
251-
<Reference Include="System.Linq" />
252-
<Reference Include="System.Linq.Expressions" />
253-
<Reference Include="System.Memory" />
254-
<Reference Include="System.ObjectModel" />
255-
<Reference Include="System.Runtime" />
256-
<Reference Include="System.Runtime.InteropServices" />
257-
<Reference Include="System.Threading" />
249+
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections\src\System.Collections.csproj" />
250+
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections.Concurrent\src\System.Collections.Concurrent.csproj" />
251+
<ProjectReference Include="$(LibrariesProjectRoot)System.Linq\src\System.Linq.csproj" />
252+
<ProjectReference Include="$(LibrariesProjectRoot)System.Linq.Expressions\src\System.Linq.Expressions.csproj" />
253+
<ProjectReference Include="$(LibrariesProjectRoot)System.Memory\src\System.Memory.csproj" />
254+
<ProjectReference Include="$(LibrariesProjectRoot)System.ObjectModel\src\System.ObjectModel.csproj" />
255+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\src\System.Runtime.csproj" />
256+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\src\System.Runtime.InteropServices.csproj" />
257+
<ProjectReference Include="$(LibrariesProjectRoot)System.Threading\src\System.Threading.csproj" />
258258
</ItemGroup>
259259

260260
</Project>

0 commit comments

Comments
 (0)