When using the C#9 Source Generator, how to ensure files are generated to specific location/file path?
Asked Answered
C

1

7

I'm working on a project utilizing C# 9's Source Generator, but when the code is generated, I'm wanting different files of generated code to be emitted to specific file paths/locations within the existing project where the code is being generated.

Is this possible? I know I can specify the folder they are all placed into using:

<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>

and changing 'Generated' to be the folder I want the generated files to be inside of. So like if I already have a folder called 'Models,' I understand that I would change 'Generated' to 'Models' and then it would be put there.

However, once it's inside of that file path, it's put inside of a folder with the name of the source generator's project name, and then another folder with the namespace.generatorclassname.

So in this instance, what I have happening is:

  • 'Generated' is the specified folder path in the project where the files are being generated
  • 'SourceGenerator' is the name of project that houses the actual generator
  • 'Generator' is the name of the class that actually is the legitimate Generator
  • The 'CustomerTest' files are the actual files being generated

Here's a screenshot of the above scenario that I have currently happening: Screenshot of Generated Folder Structure

However, what if I want each of these files to be generated to a different location within the pre-existing folder structure of the destination project? Is that possible? Any ideas?

Cabrilla answered 7/7, 2021 at 13:11 Comment(5)
No. You don't even have control over the file name of the file (you can give a hint, but Roslyn is free to ignore it if it wants)Loveless
source generators are not made to do that, but you can just use the System.IO namespace to write files, keep in mind tho, that if the file is picked up in the compilation, that will trigger another run of the generator and you might end up creating and endless loop.Falgoust
Official guidelines prohibits using any I/O during source generation, as it will mess with the Roslyn cache somehow. So while what @PatrickBeynio says might "work" it's certainly not recommended...Sander
@OlaBerntsson old non incremental SGs are not cached. and it depends on what's meant with "destination project", if it's neither the SG consuming project nor one of its dependencies, the compiler shouldn't care, since it can't see them while compiling the consuming project...Falgoust
Anyone stumbling on this today. You can setup a debug project like this. Note that you need to have the .NET Compiler Platform SDK installed in order to debug your generatorsRaveaux
S
2

As @canton7 commented, this is probably not possible per-se with today's generators. However, if you are really desperate you might be able to set up a post-build script to copy/move the emitted files to specific locations... This seems like a maintenance nightmare though and I would rather avoid it if I could at all.

The real question is why these files must reside at specific locations at all - they are typically not ever to be edited by hand, and there are few reasons beyond debugging to even emit them to the project structure.

Sander answered 2/1, 2023 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.