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

SourceGenerator can't find dependent projects #107

Closed
rmarskell opened this issue Mar 10, 2024 · 2 comments
Closed

SourceGenerator can't find dependent projects #107

rmarskell opened this issue Mar 10, 2024 · 2 comments
Labels

Comments

@rmarskell
Copy link

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
Copy link
Owner

NeVeSpl commented Mar 10, 2024

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
Copy link
Author

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

@rmarskell rmarskell closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants