/SourceGen

A repository to try to solve the problem of generating folders at compile time

Primary LanguageC#

The problem is when trying to generate the code in a file but inside a folder

in ConsoleApp\SourceGenerator\HelloSourceGenerator.cs in HelloSourceGenerator class in Execute method

var relativePath = Path.Combine("folder", $"{typeName}.g.cs");

context.AddSource(relativePath, source)

in VS 17.6.4 & VS 17.6.2 the problem :

I presented earlier that I am working on to try to generate a folder by name "folder" and contain "program.g.cs" file.

the problem of generating FOLDERS is still not resolved. The changes that occurred were generated an apparently empty file with the name: "folder/program.g.cs"

files name with slash

But the strange thing is that the resulting file, although it is apparently empty, may contain the generated code in one way or another, but it is not visible.

like empty sg out

How did I know that?

In the program.cs file there is a declaration of a method called "HelloFrom". This method we considered partial because we will use the code generator to write the definition for us, which in turn will contain the implementation of this method.

program

Without the output of the code generator, the method will not be executed as required, but the method was executed as if the output of the code generator existed, even though the resulting file was empty.

output as if SG work true

Solution for VS 17.6.2:

Based on the following GitHub comment : jan created an issue to track the bug: #68489. Note that this is IDE-only problem, it should not prevent you from using directories in source-generated files in normal builds, such files just aren't displayed correctly in Visual Studio.

=>so we will move to this issue: Source Generator files with directories are not displayed correctly in IDE


in VS 17.5.2 the problem :

the build output interface will show this exception when you Build your solution (Ctrl + Shift + B):

2>CSC : warning CS8785: Generator 'HelloSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'folder/Program.g.cs' contains an invalid character '/' at position 6.

Same proplem for this : \\ and \.

Solution for VS 17.5.2 :

Based on the following GitHub conversation: Allow directories in source generator outputs , the solution to this problem is not in VS 17.5.2, so you will have to use VS 17.6 Preview