Skip to content

Additional C# global imports #18825

Closed
Closed

Description

@JunTaoLuo

There were some additional discussions on what global imports should or should not be included in the base .NET SDK. Specifically, there were concerns about:

✅ = currently included
❌ = not currently included

  1. ✅ System.Net.Http: The contention is that this is used for web concerns so maybe it belongs in the Web SDK instead. However, it's also very frequently used in general, even console apps.
  2. ❌ System.Net: This is often used in combination with System.Net.Http. We should put this in where-ever we end up putting System.Net.Http
  3. ❌ System.Diagnostics: This has been used in some of the Web templates. Is this a common enough concern to be included in an SDK, be it the base .NET SDK or Web SDK?
  4. ❌ System.Text: This is another commonly used namespace that we can consider for the base .NET SDK. Historically, this has been used

In the interest of ensuring all discussion is happening in the same place, let's use this issue as much as possible to track discussions. @DamianEdwards @RussKie.

FYI @davidfowl @halter73 @KathleenDollard @pranavkm @terrajobst

Feel free to tag anyone else who would be interested in this dicsussion. I've only included folks who have had additional input on the current import list.

Activity

  1. ghost added
    untriagedRequest triage from a team member
    on Jul 9, 2021
  2. DamianEdwards commented on Jul 9, 2021

    @DamianEdwards
    Member

    Here's the PR that removes using statements from the web templates based on what's implicitly added by the SDK now. The namespaces that are left are a potential place to start WRT to what to consider for further inclusion.

  3. JunTaoLuo commented on Jul 9, 2021

    @JunTaoLuo
    ContributorAuthor

    Also, as a reference here's the currently included list:

    1. Base .NET SDK
        <Import Include="System" />
        <Import Include="System.Collections.Generic" />
        <Import Include="System.IO" />
        <Import Include="System.Linq" />
        <Import Include="System.Net.Http" />
        <Import Include="System.Threading" />
        <Import Include="System.Threading.Tasks" />
    
    1. Web SDK (also includes everying in the base .NET SDK)
        <Import Include="System.Net.Http.Json" />
        <Import Include="Microsoft.AspNetCore.Builder" />
        <Import Include="Microsoft.AspNetCore.Hosting" />
        <Import Include="Microsoft.AspNetCore.Http" />
        <Import Include="Microsoft.AspNetCore.Routing" />
        <Import Include="Microsoft.Extensions.Configuration" />
        <Import Include="Microsoft.Extensions.DependencyInjection" />
        <Import Include="Microsoft.Extensions.Hosting" />
        <Import Include="Microsoft.Extensions.Logging" />
    
    1. Worker SDK (also includes everying in the base .NET SDK)
        <Import Include="Microsoft.Extensions.Configuration" />
        <Import Include="Microsoft.Extensions.DependencyInjection" />
        <Import Include="Microsoft.Extensions.Hosting" />
        <Import Include="Microsoft.Extensions.Logging" />
    
  4. davidfowl commented on Jul 10, 2021

    @davidfowl
    Member

    Seems like we missed the Blazor SDK for default usings. This change removed usings from the blazorwasm template (Microsoft.Extensions.*) but we didn't do the default usings there. I think we should fix it in the sdk instead of putting the usings back and additionally add more default usings for blazor wasm.

    This currently is blocking SDK merges into the installer dotnet/installer#11100

    PS: I've sent a PR to add Microsoft.Extensions only. I'm sure we'll want to add more

    cc @danroth27 @SteveSandersonMS @pranavkm @javiercn

  5. pranavkm commented on Jul 12, 2021

    @pranavkm
    Contributor

    I think we should fix it in the sdk instead of putting the usings back and additionally add more default usings for blazor wasm.

    That creates some inconsistently between Server and WASM. Would we feel ok adding things to the Server's csproj to make them more consistent?

  6. davidfowl commented on Jul 12, 2021

    @davidfowl
    Member

    @pranavkm can you clarify? Blazor server uses the web sdk no? Which has these usings and more...

  7. pranavkm commented on Jul 12, 2021

    @pranavkm
    Contributor

    Do you have a list in mind that would go in the WebAssembly SDK? I'd think most of these would be from the Microsoft.AspNetCore.Components.* namepace?

  8. davidfowl commented on Jul 12, 2021

    @davidfowl
    Member

    Do you have a list in mind that would go in the WebAssembly SDK? I'd think most of these would be from the Microsoft.AspNetCore.Components.* namepace?

    I think that's better determined by the Blazor team. Also it seems like most of those things come from the package so maybe it doesn't make sense in the sdk?

  9. added this to the Backlog milestone on Jul 19, 2021
  10. marcpopMSFT commented on Jul 19, 2021

    @marcpopMSFT
    Member

    For reference, a quick GH search revealed 3M, 6M, 9M, and 9M results for items 1, 2, 3, and 4 in the original descriptions, respectively. For reference, Generic, IO, and Linq have 56M, 14M, and 36M, respectively.

    Example query: https://github.com/search?q=%22System.Collections.Generic%22

  11. 19 remaining items

  12. MichalStrehovsky commented on Aug 2, 2021

    @MichalStrehovsky
    Member

    The implicit namespace import caused a build break in crossgen2 because it has a low level implementation of LINQ (that has better perf characteristics than LINQ for people who don't do things like Where().Where().Select().Min()).

    It was very much not obvious what caused it, even for a bunch of seasoned .NET devs. dotnet/runtime#55855

  13. MichalStrehovsky commented on Aug 3, 2021

    @MichalStrehovsky
    Member

    Please consider all the current (and future!) names of types in all of the discussed namespaces, especially for things that go into the base SDK. I don't particularly care about the special-purpose SDKs - just for the base SDK. We market .NET as "A developer platform for building all your apps.". Does System.Net.Http make sense in a game? An IoT app? The default-included namespaces better make sense there or they just limit the number of available identifiers.

    The prosed namespaces have some very common names in them that are going to conflict with existing things in the ecosystem (and if the conflicts happens to be in a type name within a NuGet package, the break is going to cascade out of that). People will no longer be able to use the common names because .NET essentially "reserves" them (causing extra friction if someone wants to use the common word in their own namespace going forward).

    We have namespaces exactly because of this - to avoid conflicts.

    E.g. System.Diagnostics has a Process class. So does Chocolatey, or Microsoft Terminal, or azure pipelines agent. The name Process makes sense there.

    System.IO has a File class. Well so does Microsoft IIS Administration and countless others.

    Basically, going overboard with the number of default-included namespaces we're making it hard to use common words as class names, while lacking any context whatsoever (base SDK is included for any app any developer any platform).

    Frankly I'm finding it hard to justify including anything besides System in the base SDK. Even System is problematic as Jared would tell you, but I'm much less sympathetic to developers who name their classes String or Int32 than to those having their own Directory, Activity, Monitor, Timeout, or Task (that one is already hurting us).

  14. dsplaisted commented on Aug 3, 2021

    @dsplaisted
    Member

    @RussKie Glancing at your winforms PR, it looks like most of the updates were removing using statements for the same namespaces that were included in implicit global usings. As I understand it, you should not have needed to make those changes. I think you were probably using an older version of the Roslyn compilers. With more recent versions, those usings would not have caused errors and you wouldn't have needed to make most of those changes.

  15. davidfowl commented on Aug 3, 2021

    @davidfowl
    Member

    As I understand it, you should not have needed to make those changes. I think you were probably using an older version of the Roslyn compilers. With more recent versions, those usings would not have caused errors and you wouldn't have needed to make most of those changes.

    Yep see #18459 (comment)

  16. DamianEdwards commented on Aug 5, 2021

    @DamianEdwards
    Member

    We're planning to make some changes to the implicit usings feature in rc.1 based on the feedback we've received here and elsewhere. Check out #19521 for details.

  17. SimonDD7 commented on Aug 26, 2021

    @SimonDD7

    Those namespaces shouldn't be accessed if not asked for. IntelliSense currently propose all classes from those namespaces in empty class file. VS marks System.IO reference as to be removed. This is really confusing.

  18. davidfowl commented on Aug 26, 2021

    @davidfowl
    Member

    VS marks System.IO reference as to be removed

    Can you elaborate here?

  19. SimonDD7 commented on Aug 26, 2021

    @SimonDD7

    VS marks System.IO reference as to be removed

    Can you elaborate here?

    System.IO (as an example) is globally accessible after update to net6-preview7. So all those using directives are marked as unnecessary. Sorry, I should've been more specific.

  20. davidfowl commented on Aug 26, 2021

    @davidfowl
    Member

    Ah yes, that's by design when this feature is on.

  21. davidfowl commented on Sep 13, 2021

    @davidfowl
    Member

    I'm gonna close this issue as there were major updates made and it doesn't reflect the current state of things. Use this issue to give more feedback:

    #19521

  22. davidfowl commented on Sep 13, 2021

    @davidfowl
    Member

    It is but I would read it first then open another issue with more details if that isn't satisfying

  23. pranavkm commented on Sep 13, 2021

    @pranavkm
    Contributor

    The potential for abuse should be more than enough to limit this to an opt in feature, if this is something that you feel this strongly about

    Starting in rc1, it is a opt-in feature. The other issue goes over some of the implementation details about this.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions