CaptainToTo
CaptainToTo

Reputation: 31

Source Generated Files Can't Use Namespaces In Unity

I'm trying to get a source generator working, but for some reason its failing to include the namespace it needs. The generated code looks like this:

using OwlTree; // namespace proxy factory is from
using System;

public class ProjectProxyFactory : ProxyFactory
{
    ...
}

I'm getting the error:

OwlTree.Generator\OwlTree.Generator.OwlTreeGenerator\ProjectProxyFactory.g.cs(1,7): error CS0246: The type or namespace name 'OwlTree' could not be found (are you missing a using directive or an assembly reference?)

as well a bunch of others that are similarly saying that types from the OwlTree namespace are missing.

I followed these instructions: https://docs.unity3d.com/2022.1/Documentation/Manual/roslyn-analyzers.html

And this is what my generator dll looks like in editor:

enter image description here

The cs files that contain everything in the OwlTree namespace are in my Assets folder, they aren't compiled into their own dll, and the source generator isn't in a separate assembly definition. The things I've found are:

This is the csproj file for the generator itself:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <LangVersion>9.0</LangVersion>
    <TargetFramework>netstandard2.0</TargetFramework>
    <IsPackable>false</IsPackable>
    <ImplicitUsings>disable</ImplicitUsings>
    <Nullable>disable</Nullable>
    <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
    <IsRoslynComponent>true</IsRoslynComponent>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference ExcludeAssets="runtime" Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference ExcludeAssets="runtime" Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" PrivateAssets="all" />
    <PackageReference ExcludeAssets="runtime" Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.0" />
  </ItemGroup>

  <ItemGroup>
    <None Include="$(OutputPath)\netstandard2.0\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
  </ItemGroup>

</Project>

UPDATE:

I've since tried to create a test class for the generator to be applied to:

namespace OwlTree
{
    // will have a proxy generated by the source generator
    public class Test : NetworkObject
    {

    }
}

This will make OwlTree.Test appear in ProjectProxyFactory.g.cs, and this access of OwlTree doesn't cause the error. When I try to change other accesses to use OwlTree, such as OwlTree.ProxyFactory, it still causes not found error.

Again, the file that contains the proxy factory definition is no different from my test file, in that it's just a cs file in my Assets folder. This is very strange.


UPDATE:

This is the project structure of the assets folder:

> Assets
    > Framework
        > OwlTree
            > OwlTree source code
        > OwlTree.Generator
            > generator dll
    > test file with Test class shown above

UPDATE

I fixed the bug. The issue was that the generator was being applied to more than just the main project. There were 2 other projects the generator was added (I don't know what they are). Looking into more documentation, this is apparently intended behavior. As a result, empty proxy factories were being generated for projects that didn't include OwlTree.

My solution was to only generate a factory if there are classes for the factory to generator for. Then, I added defaults in the framework to handle a non-existent factory, which were previously handled by having an empty factory.

Upvotes: 1

Views: 56

Answers (0)

Related Questions