Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to 4.0.0 or 4.1.0 omits namespaces in generated code #55

Closed
realbart opened this issue Aug 12, 2024 · 12 comments
Closed

Upgrade to 4.0.0 or 4.1.0 omits namespaces in generated code #55

realbart opened this issue Aug 12, 2024 · 12 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@realbart
Copy link

This is an example of generated code in the new versions.
Not that the Azure.Core namespace is missing from "TokenCredential"

//--------------------------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;

namespace MobiManagement.Web.Api
{
    [GeneratedCode("AutomaticInterface", "")]
    public partial interface IConfigurator
    {
        /// <inheritdoc />
        void ConfigureServices(IConfiguration configuration, IServiceCollection services, TokenCredential credential);
        
        /// <inheritdoc />
        void AddApplicationInsights(IConfiguration configuration, ILoggingBuilder loggingBuilder);
        
    }
}

In the old version, namespaces were always included, which is perfectly fine (and in this case better) for generated code.
(you might even consider putting the "global::" prefix before them, preventing problems in some edge cases)

//--------------------------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;

namespace MobiManagement.Web.Api
{
    [GeneratedCode("AutomaticInterface", "")]
    public partial interface IConfigurator
    {
        /// <inheritdoc />
        void ConfigureServices(Microsoft.Extensions.Configuration.IConfiguration configuration, Microsoft.Extensions.DependencyInjection.IServiceCollection services, Azure.Core.TokenCredential credential);
        
        /// <inheritdoc />
        void AddApplicationInsights(Microsoft.Extensions.Configuration.IConfiguration configuration, Microsoft.Extensions.Logging.ILoggingBuilder loggingBuilder);
        
    }
}
@ChristianSauer
Copy link
Collaborator

Normally, it should include the used namespaces in the usings section?
I intentionally reworked that part because adding all type qualifiers made the generated code unreadable and in turn made testing annoying and hard.
Any idea?

@ChristianSauer ChristianSauer added bug Something isn't working help wanted Extra attention is needed labels Aug 13, 2024
@realbart
Copy link
Author

realbart commented Aug 13, 2024 via email

@simonmckenzie
Copy link
Contributor

This broke interface generation on one of my classes - I need to investigate further, but it looks like it's because one of the method parameter types was available through an implicit global namespace in the csproj, e.g. <Using Include="System.Text.Json" />.

@realbart
Copy link
Author

realbart commented Aug 13, 2024 via email

@simonmckenzie
Copy link
Contributor

I was able to reproduce the "broken interface" issue using inheritance:

  • Assembly 1
    • Add class with parameter resolved via a global using
  • Assembly 2
    • Add class inheriting from Project 1's class, add [GenerateAutomaticInterface]

Result: the generated interface does not include the global using or fully qualify the parameter, resulting in a compile error.

screenshot of broken interface produced by failure to resolve parent assembly's global using from consuming assembly

note, BaseClass and Inheritor are in different assemblies

I'm inclined to @realbart's opinion that all references should just be fully qualified, with no usings at all...

@simonmckenzie
Copy link
Contributor

I can see other cases that also fail, where "using aliases" break the interface generation:

screenshot of generated interface that doesn't respect the type alias, and thus can't compile

using ModelAlias = RootNamespace.Models.Model;

namespace RootNamespace
{
    namespace Models
    {
        public class Model;
    }

    [GenerateAutomaticInterface]
    public class ModelManager
    {
        public ModelAlias GetModel() => null!;
    }
}

Clearly this can be fixed, but it's just another edge case that the code would have to handle.

Thinking back to WinForms development, the .Designer.cs files also used fully qualified references, possibly for similar reasons.

@simonmckenzie
Copy link
Contributor

And one last failing case - using statements inside namespaces also aren't handled:

screenshot of interface with invalid usings due to use of using inside a namespace

namespace RootNamespace
{
    namespace Models
    {
        public class Model;
    }


    namespace ModelManager
    {
        using Models;

        [GenerateAutomaticInterface]
        public class ModelManager
        {
            public Model GetModel() => null!;
        }
    }
}

@simonmckenzie
Copy link
Contributor

I would be happy to work on a fix for this (removal of all "harvested" usings, fully qualify all type references) if you agree with the approach @ChristianSauer. I don't want to take it from you @realbart if you're keen to work on this too.

@ChristianSauer
Copy link
Collaborator

@simonmckenzie I would be very glad. I am currently busy with work and private things. Could probably work on that fix friday at the earliest

@simonmckenzie
Copy link
Contributor

simonmckenzie commented Aug 19, 2024

I will start on the change tomorrow @ChristianSauer. I expect the changes will cause conflicts with #58, so once one is merged, I will rebase the other.

@simonmckenzie
Copy link
Contributor

I have created a PR for this change @ChristianSauer. Please tell me what you think. #60

@ChristianSauer
Copy link
Collaborator

I have merged the PR as 5.0.0
Thank you all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants