NeVeSpl / NTypewriter

File/code generator using Scriban text templates populated with C# code metadata from Roslyn API.
https://nevespl.github.io/NTypewriter/
MIT License
117 stars 24 forks source link

SourceGenerator can't find dependent projects #107

Closed rmarskell closed 4 months ago

rmarskell commented 4 months ago

I'm trying to use the source generator to generate typescript files for several C# class libraries. I wanted to have a shared config, so I created a simple BaseConfig class in a different library project that will be shared with the others.

using NTypewriter.Editor.Config;
namespace MySharedLib;
public class BaseConfig : EditorConfig {
    public override bool AddGeneratedFilesToVSProject => false;
    // etc.
}

Then, in one of those projects, I created the project config (ProjConfig.nt.cs) that inherits from the BaseConfig like so:

using MySharedLib;
using System.Collections.Generic;
namespace Proj1.NTypewriter;
public class ProjConfig : BaseConfig {
    public override IEnumerable<string> NamespacesToBeSearched {
        get {
            yield return "Proj1.Dtos";
            yield return "Proj1.Controllers";
        }
    }
}

This all compiles fine, but when the source generator runs, it says it can't find MySharedLib.

Does the source generator not share the same dependencies as the project it's running on? Is there some way to configure this? Hoping I'm just missing something obvious. 😅

Also, on a sidenote, using System.Collections.Generic; is also required even though the editor says it's not needed and can be removed.

NeVeSpl commented 4 months ago

A source generator, in general, is run in the context of a given project, it does not know anything about other projects in the solution, nor does it have access to source files from other projects. A source generator is run as a part of a compilation, it shares dependencies with the compiler (VS/MsBuild/dotnet build), not with the project that is compiled.

So, the only way to share base config file between projects is by linking that file to the projects.

rmarskell commented 4 months ago

Thanks for the tip! I managed to get the BaseConfig compiling by adding it as a linked file and ensuring it had a .nt.cs extension (I originally just had .cs and it was giving the same error as before saying it couldn't find it). Not the best, but it's working. 👍