Repository navigation
Description
In runtime's libraries there are different ways to reference a dependency:
- ProjectReference: If a compatible live configuration of a dependency exists, a
ProjectReferenceshould be used. Exception:$(NetCoreAppCurrent)targeting pack references from runtime pack assemblies are currently not represented asProjectReferencebutReferenceitems to minimize incremental build costs (including netstandard.dll). - Reference: If a compatible live configuration of a dependency doesn't exist and the assembly is part of the targeting pack (framework reference), a
Referenceshould be used. - PackageReference: If a compatible live configuration of a dependency doesn't exist and the assembly isn't part of the targeting pack (framework reference), a PackageReference should be used. Example: System.Memory for
netstandard2.0.
For someone who doesn't work full time on runtime's infrastructure, it's hard to reason about when to use which of the ItemTypes above. AspNetCore doesn't use ProjectReference items in the project file and dynamically construct the ProjectReference items based on Reference items.
I would like to discuss if it there's value in simplifying how references in runtime's libraries are defined. This could be accomplished by consolidating on one of the above item types or by introducing a new one i.e. DynamicReference.
Requirements
Such a system needs to work with "msbuild static graph" which we depend on to restore projects. That said, it should be fine to depend on a target, as static graph restore invokes certain targets and static graph build (from my perspective) will likely need to support that as well.
Additional information
- The
DisableImplicitFrameworkReferencesmsbuild property defines if a targeting pack is "consumed" by the project. In dotnet/runtime this property defaults to true for$(NetCoreAppCurrent)ref and src assemblies which are inbox.
@ericstj @joperezr @safern @Anipik @eerhardt and @dougbu @wtgodbe (for thoughts about what aspnetcore's infra does well and what not)
Activity
[-]Unify how assemblies and projects are referenced in libraries[/-][+]Discussion about how dependencies are referenced in libraries[/+]joperezr commentedon Feb 12, 2021
I definitely see value on adding something that abstracts this concept from most devs as at least I get asked this question relatively often. That said, it is not super common to add new references to projects (meaning most PRs don't have to do this) so one thing we could consider is simply erroring when detecting that the wrong item was used without trying to attempt to fix it. This is only a suggestion based on thinking that perhaps the cost of adding logic to detect the wrong item vs the logic of consolidating to the right one (while still making sure that it happens at the right time for restore and RAR etc) might be considerably different.
[-]Discussion about how dependencies are referenced in libraries[/-][+]Discussion about how references are defined in libraries[/+]dougbu commentedon Feb 17, 2021
dotnet/aspnetcore almost never uses
<PackageReference />or<ProjectReference />in our C# projects. Instead we use<Reference />elements and convert them to@(ProjectReference)items as part of MSBuild evaluation and to@(PackageReference)items later, in aResolveCustomReferencestarget that runs e.g. beforeResolvePackageAssets.This works relatively well but is a bit confusing for new members of our dev community because
<Reference />doesn't have its usual meaning (includes assembly names instead of assembly locations) and because those adding new projects or moving sources around need to update our mappings to project locations and package versions. We've also had to service this part of the code a few times, a downside for the build v-team. Fortunately, we don't get much pushback from the community and the code involved is pretty well commented these days. See https://github.com/dotnet/aspnetcore/blob/main/eng/targets/ResolveReferences.targets and the few files providing mappings, mostly https://github.com/dotnet/aspnetcore/blob/main/eng/Dependencies.props (notice the item group at the bottom) and https://github.com/dotnet/aspnetcore/blob/main/eng/ProjectReferences.propsThe main historical benefit was our ability to use "baseline" package references when rebuilding in servicing instead of rebuilding everything. Effectively our 2.1 branch builds primarily against the previous (say) 2.1.24 release except for the few projects we're patching in 2.1.25.
The benefits aren't as obvious in 3.1 and later. But, it still makes our
*.csprojfiles smaller and avoids having package versions spread throughout the repo.ericstj commentedon Feb 17, 2021
Being at the bottom of the stack I think it's pretty important to have the distinction between PackageReference and Reference|ProjectReference. This is an indication of whether the project expects and live-built library or a previously shipped one. I think it's important to record and track changes like that at the consumption site.
I'm less interested in the difference between Reference and ProjectReference. These differences are side-effects of how we choose to architect the build infra. Forcing developers to understand this isn't particularly meaningful. Tracking changes to those in individual projects isn't helpful either.
I do think we need to maintain granular references for things inside the shared framework. This helps us hold the line on architectural layering.
ViktorHofer commentedon Feb 18, 2021
Thanks @dougbu for the detailed response and for the links to the implementation.
PackageReferences are mainly used for
.NETStandardand.NETFrameworkconfigurations in runtime's libraries to add functionality which is often already available in.NETCoreAppinbox. We are less interested in such configurations going forward which means continue usingPackageReferenceitems makes sense.I agree with everything that @ericstj mentioned above, the distinction between live vs prebuilt is important, as well as granular references for
.NETCoreApp. Even though we already have granular references for OOB.NETCoreAppconfigurations, I'm less interested in those. Some Microsoft.Extensions.* libraries already disable granular references and just use the SDK functionality which references everything that is part of the targeting pack.ViktorHofer commentedon May 10, 2022
Closing as I don't think we need infrastructure anymore to automagically decide between
Reference,ProjectReferenceandPackageReference.PackageReferenceitems are used whenever you reference a pre-built where-asProjectReferenceitems are used whenever you want to reference a project in your tree.Source projects inside the shared framework still don't adhere to that principle as they still use
Referenceitems but I expect this to naturally go away at some point in the future when msbuild becomes fast enough by either not triggering incremental builds at all (á la fast-up-date checks inside VS) or making them so damn fast that they don't contribute much to the overall build time of a lib.