5

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?

S. Dunlap
  • 51
  • 2
  • 2
    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) – canton7 Jul 07 '21 at 13:14
  • 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. – Patrick Beynio Mar 19 '22 at 18:59
  • 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... – Ola Berntsson Jan 02 '23 at 22:42
  • @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... – Patrick Beynio Jan 03 '23 at 13:07

1 Answers1

0

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 to ever be edited by hand, and there are few reasons beyond debugging to even emit them to the project structure.

Ola Berntsson
  • 647
  • 1
  • 6
  • 12