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

Naming conflict with source generator #479

Closed
chikacc opened this issue Feb 24, 2023 · 5 comments
Closed

Naming conflict with source generator #479

chikacc opened this issue Feb 24, 2023 · 5 comments

Comments

@chikacc
Copy link
Contributor

chikacc commented Feb 24, 2023

Description

  1. Create unity project and add VContainer package
  2. Add VContainer.SourceGenerator
  3. Create script as following:
using VContainer;

public class Class1 {
    readonly IObjectResolver _resolver;

    public Class1(IObjectResolver resolver) => _resolver = resolver;
}

Expected Behaviour

All clean, everything is happy.

Current Behaviour

Generated Class1GeneratedInjector.g.cs:

 using System;
 using System.Collections.Generic;
 using VContainer;

 class Class1GeneratedInjector : IInjector
 {
     public object CreateInstance(IObjectResolver resolver, IReadOnlyList<IInjectParameter> parameters)
     {
         var resolver = resolver.ResolveOrParameter(typeof(global::VContainer.IObjectResolver), "resolver", parameters);
         var instance = new Class1((global::VContainer.IObjectResolver)resolver);
         Inject(instance, resolver, parameters);
         return instance;
     }

     public void Inject(object instance, IObjectResolver resolver, IReadOnlyList<IInjectParameter> parameters)
     {
         return;
     }
 }

Unity console shown as follows:

VContainer.SourceGenerator\VContainer.SourceGenerator.VContainerSourceGenerator\Class1GeneratedInjector.g.cs(9,14): error CS0136: A local or parameter named 'resolver' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
VContainer.SourceGenerator\VContainer.SourceGenerator.VContainerSourceGenerator\Class1GeneratedInjector.g.cs(9,25): error CS0841: Cannot use local variable 'resolver' before it is declared
VContainer.SourceGenerator\VContainer.SourceGenerator.VContainerSourceGenerator\Class1GeneratedInjector.g.cs(11,27): error CS1503: Argument 2: cannot convert from 'object' to 'VContainer.IObjectResolver'

Environment

  • VContainer 1.13.0
  • Unity 2021.3.16f1
  • Windows 11 21H2
@hadashiA
Copy link
Owner

Thanks for the report. I'll fix it.

@spikeliu1984
Copy link

VContainer.SourceGenerator\VContainer.SourceGenerator.VContainerSourceGenerator\MessagePipe.VContainer.ObjectResolverProxyGeneratedInjector.g.cs(11,18): error CS0136: A local or parameter named 'resolver' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter VContainer.SourceGenerator\VContainer.SourceGenerator.VContainerSourceGenerator\MessagePipe.VContainer.ObjectResolverProxyGeneratedInjector.g.cs(11,29): error CS0841: Cannot use local variable 'resolver' before it is declared VContainer.SourceGenerator\VContainer.SourceGenerator.VContainerSourceGenerator\MessagePipe.VContainer.ObjectResolverProxyGeneratedInjector.g.cs(13,31): error CS1503: Argument 2: cannot convert from 'object' to 'VContainer.IObjectResolver'
Not sure if it's the same problem. I'm seeing the same error when MessagePipe is imported.

Environment
VContainer 1.13.0
Unity 2022.2.8f1

@th3d0g
Copy link

th3d0g commented Mar 7, 2023

I'm also having this issue related to MessagePipe and InGameDebugger.

I followed the steps here https://vcontainer.hadashikick.jp/optimization/source-generator.

VContainer.SourceGenerator\VContainer.SourceGenerator.VContainerSourceGenerator\MessagePipe.VContainer.ObjectResolverProxyGeneratedInjector.g.cs(11,18): error CS0136: A local or parameter named 'resolver' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

VContainer.SourceGenerator\VContainer.SourceGenerator.VContainerSourceGenerator\IngameDebugConsole.ConsoleMethodInfoGeneratedInjector.g.cs(11,111): error CS0841: Cannot use local variable 'parameters' before it is declared

VContainer 1.13.0
Unity 2021.3.13f1

@hadashiA How do I utilise #480 in my Unity project?

Please advise.

@chikacc
Copy link
Contributor Author

chikacc commented Mar 8, 2023

@spikeliu1984 @th3d0g

I have a solution to avoid similar problems until the pull request #480 is released. You can place the source generator in a separate assembly definition, for example, VContainer.Generators. This way, MessagePipe or any definition assembly will not trigger the source generator unless you add VContainer.Generators to the reference.

Alternatively, you can install the latest version of VContainer from the Git URL according to the instructions on the Install via UPM (using Git URL) page and do not specify the version number. Then you can get a sneak peek at features that have not yet been released.

@hadashiA
Copy link
Owner

@chikacc @spikeliu1984 @th3d0g Sorry for the late response.

I've released a fix. https://github.com/hadashiA/VContainer/releases/tag/1.13.1
If you still have problems, please reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants