question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Naming conflict with source generator

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hadashiAcommented, Mar 14, 2023

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

1reaction
spikeliu1984commented, Mar 3, 2023

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feedback from writing a source generator · Issue #44929
When generating types using S.R.E., we have the luxury of unspeakable names, ensuring that field/local names don't clash with field/local names ...
Read more >
The code generator does not take into account naming ...
when run graphql-codegen to produce code, it has produced the same name type. Expected behavior. The code generator needs to take into account ......
Read more >
Solving the source generator 'marker attribute' problem
In this post I describe how I solved a problem I've been wrestling with around source generators: where to put the 'marker attributes'...
Read more >
Should a Source Generator be in its own assembly?
1. There is at least one reason - to isolate build time dependencies (so they are not included into the output). · Looks...
Read more >
Question - Source Generator generates code multiple times
I was able to make my source generator work in Unity 2021.3, but there's one problem - it generates code multiple times -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found