Skip to content

Discussion about how references are defined in libraries #48238

Closed

Description

@ViktorHofer

In runtime's libraries there are different ways to reference a dependency:

  • ProjectReference: If a compatible live configuration of a dependency exists, a ProjectReference should be used. Exception: $(NetCoreAppCurrent) targeting pack references from runtime pack assemblies are currently not represented as ProjectReference but Reference items 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 Reference should 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 DisableImplicitFrameworkReferences msbuild 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

  1. added this to the Future milestone on Feb 12, 2021
  2. self-assigned this
    on Feb 12, 2021
  3. ghost added
    untriaged
    on Feb 12, 2021
  4. changed the title [-]Unify how assemblies and projects are referenced in libraries[/-] [+]Discussion about how dependencies are referenced in libraries[/+] on Feb 12, 2021
  5. joperezr commented on Feb 12, 2021

    @joperezr
    Member

    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.

  6. changed the title [-]Discussion about how dependencies are referenced in libraries[/-] [+]Discussion about how references are defined in libraries[/+] on Feb 15, 2021
  7. dougbu commented on Feb 17, 2021

    @dougbu
    Contributor

    thoughts about what aspnetcore's infra does well and what not

    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 a ResolveCustomReferences target that runs e.g. before ResolvePackageAssets.

    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.props

    The 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 *.csproj files smaller and avoids having package versions spread throughout the repo.

  8. ericstj commented on Feb 17, 2021

    @ericstj
    Member

    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.

  9. modified the milestones: Future, 6.0.0 on Feb 18, 2021
  10. removed their assignment
    on Feb 18, 2021
  11. ViktorHofer commented on Feb 18, 2021

    @ViktorHofer
    MemberAuthor

    Thanks @dougbu for the detailed response and for the links to the implementation.

    PackageReferences are mainly used for .NETStandard and .NETFramework configurations in runtime's libraries to add functionality which is often already available in .NETCoreApp inbox. We are less interested in such configurations going forward which means continue using PackageReference items 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 .NETCoreApp configurations, 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.

  12. modified the milestones: 6.0.0, 7.0.0 on Jul 21, 2021
  13. removed
    untriaged
    on Jul 21, 2021
  14. ViktorHofer commented on May 10, 2022

    @ViktorHofer
    MemberAuthor

    Closing as I don't think we need infrastructure anymore to automagically decide between Reference, ProjectReference and PackageReference. PackageReference items are used whenever you reference a pre-built where-as ProjectReference items 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 Reference items 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.

  15. ghost locked as resolved and limited conversation to collaborators on Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions