AArnott / CodeGeneration.Roslyn

Assists in performing Roslyn-based code generation during a build.
Microsoft Public License
408 stars 60 forks source link

Code generation yields empty code files #161

Closed Antaris closed 4 years ago

Antaris commented 4 years ago

This one might be hard to solve, so I appreciate any and all comments 👍

I'm developing a custom generator to allow me to flesh out some custom primitives we use internally for our business system.

When I develop locally, I can see my custom code being generated in my unit tests. When I ship my generator as a package, I get an (almost) empty generated file:

// ------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------

There is no actual implementation in the generated file.

My delivery mechanism is as follows:

MyBusiness.CodeGeneration.Abstractions package containing shared code generation abstractions which can be consumed by any custom generators I build. MyBusiness.CodeGeneration.Attributes package containing marker attributes MyBusiness.CodeGeneration.Generator package containing my struct generator.

In addition, I generate a meta-package, MyBusiness.CodeGeneration which pulls in my dependencies:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
    <metadata>
        <id>MyBusiness.CodeGeneration</id>
        <version>$version$</version>
        <description>Meta package containing references to code generation libraries.</description>
        <authors>MyBusiness</authors>
        <owners>MyBusiness</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>

        <tags>codegen</tags>
        <dependencies>
            <group targetFramework=".NETStandard2.0">
                <dependency id="MyBusiness.CodeGeneration.Attributes" version="$version$" />
                <dependency id="MyBusiness.CodeGeneration.Abstractions" version="$version$" />
                <dependency id="MyBusiness.CodeGeneration.Generator" version="$version$" />
                <dependency id="CodeGeneration.Roslyn.BuildTime" version="0.6.1" />
            </group>
        </dependencies>
    </metadata>
</package>

When I build this while consuming my packages (meta or direct), it only ever generates an empty file.

Things I've checked:

I'm struggling to understand why it won't run my generator. Previously when I hadn't correctly included my generator assembly, it gave my an exception, but now it is happy the assembly exists, but nothing is generated.

An example of my code:

[GenerateStruct]
public partial struct MyId { }

This was based on StronglyTypedId by Andrew Lock (https://github.com/andrewlock/StronglyTypedId) which uses your packages. The only real difference between his implementation and mine is that I am using embedded template files instead of manually generating the syntax myself, therefore I have a direct dependency on Microsoft.Extensions.FileProviders.Embedded

Other things to note:

amis92 commented 4 years ago

Analysis on my part:

Looking at https://github.com/AArnott/CodeGeneration.Roslyn/blob/246ca6a3a01b24f2793293f03ed80e99a7ba0ed1/src/CodeGeneration.Roslyn.Engine/DocumentTransform.cs#L131 and below, the reasons can be the following:

In any other case, there should be some errors. Can you try to look at verbose logs?

Corniel commented 4 years ago

I have a problem that might be related to this one. I just have a very primitive (on purpose) code generator (inspired by on of the examles of this repro). I have a public accessible pull request: https://github.com/Qowaiv/qowaiv-codegenerator/pull/1/commits/e681d63945cace1effe35ed8ef0c41cbcf71f18c

In my output I noticed the following ERROR:

MSB3073 The command "dotnet codegen "@obj\Debug\netstandard2.0\Qowaiv.CodeGenerator.Examples.csproj.dotnet-codegen.rsp"" exited with code 3.

This was after I added a global.json:

{
  "sdk": { "version": "2.2.402" },
  "msbuild-sdks": {
    "Microsoft.Build.CentralPackageVersions": "2.0.46"
  }
}

Without, I've got the following error:

Error occurred while restoring NuGet packages: Failed to retrieve information about 'dotnet-codegen' from remote source (..)
amis92 commented 4 years ago

@Corniel Your issue seems to be super different actually. Could you open up a new one?

Error 3 means an exception was caught - it should log details in the msbuild log.

amis92 commented 4 years ago

As per this specific issue, closing because of no follow-up from OP.