AArnott / CodeGeneration.Roslyn

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

Problems migrating to 0.7 #193

Closed louthy closed 4 years ago

louthy commented 4 years ago

In the interests of trying to help get the new version released quicker, I have updated my LanguageExt.CodeGen.csproj to use version 0.7.5-alpha to see if my test code works. I have pretty complex code-generators, so it should be a pretty good proving ground for any new features.

It's not submitted to the public git, but the csproj for the LanguageExt.CodeGen project looks like this:

<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Build">

    <PropertyGroup>
        <PackageVersion>3.3.51</PackageVersion>
        <PackageId>LanguageExt.CodeGen</PackageId>
        <Title>LanguageExt.CodeGen</Title>
        <TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
        <LangVersion>8.0</LangVersion>
        <Authors>Paul Louth</Authors>
        <Summary>Functional language code-generation for C#</Summary>
        <Copyright>Copyright (c) Paul Louth. All rights reserved.</Copyright>
        <Description>Design and build-time code-gen for lenses and immutable With functions</Description>
        <PackageTags>C#, Functional, Language Extension, Lenses, Records, Monad, Option, Either, Reader, Writer, State, List, Set, Map, Queue, Memo, Memoization, Immutable, Lambda, Pattern Matching, Tuple</PackageTags>
        <PackageIconUrl>https://github.com/louthy/language-ext/blob/master/Images/lang-ext-small.png?raw=true</PackageIconUrl>
        <PackageProjectUrl>https://github.com/louthy/language-ext</PackageProjectUrl>
        <PackageLicenseUrl>https://github.com/louthy/language-ext/blob/master/LICENSE.md</PackageLicenseUrl>
        <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
        <DocumentationFile></DocumentationFile>
        <OutputType>library</OutputType>
        <AssemblyVersion>3.0.0.0</AssemblyVersion>
        <FileVersion>3.0.0.0</FileVersion>
        <NoWarn>1701;1702;1705;IDE1006;CS1591;CS1573;CS1712;CS1570;CS1711;CS1572;CS1587</NoWarn>

    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="CodeGeneration.Roslyn" Version="0.7.5-alpha" />
    </ItemGroup>

    <ItemGroup>
        <Compile Include="**\*.cs" />
        <EmbeddedResource Include="**\*.resx" />
    </ItemGroup>

    <ItemGroup>
        <Compile Remove="obj\**" />
        <EmbeddedResource Remove="obj\**" />
        <None Remove="obj\**" />
    </ItemGroup>

</Project>

The project builds OK.

I also have a scratchpad/testbed project that I use to quickly test various code-gen features. I changed the dependencies to 0.7.5-alpha from 0.6.1. It would compile no problem with 0.6.1, but now I get the following:

5>Exception in file processing: System.InvalidOperationException: Unable to find code generator: LanguageExt.CodeGen.RWSGenerator in LanguageExt.CodeGen
5>   at Validation.Verify.FailOperation(String message, Object[] args)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.GetCodeGeneratorTypeForAttribute(INamedTypeSymbol attributeType, Func`2 assemblyLoader)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.FindCodeGenerators(ImmutableArray`1 nodeAttributes, Func`2 assemblyLoader)+MoveNext()
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.TransformAsync(CSharpCompilation compilation, SyntaxTree inputDocument, String projectDirectory, Func`2 assemblyLoader, IProgress`1 progress)
5>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.GenerateAsync(IProgress`1 progress, CancellationToken cancellationToken)
5>RobotExample.cs(1,1): error CGR001: System.InvalidOperationException: Unable to find code generator: LanguageExt.CodeGen.RWSGenerator in LanguageExt.CodeGen
5>Exception in file processing: System.InvalidOperationException: Unable to find code generator: LanguageExt.CodeGen.RecordWithAndLensGenerator in LanguageExt.CodeGen
5>   at Validation.Verify.FailOperation(String message, Object[] args)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.GetCodeGeneratorTypeForAttribute(INamedTypeSymbol attributeType, Func`2 assemblyLoader)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.FindCodeGenerators(ImmutableArray`1 nodeAttributes, Func`2 assemblyLoader)+MoveNext()
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.TransformAsync(CSharpCompilation compilation, SyntaxTree inputDocument, String projectDirectory, Func`2 assemblyLoader, IProgress`1 progress)
5>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.GenerateAsync(IProgress`1 progress, CancellationToken cancellationToken)
5>AggregateException: One or more errors occurred. (Unable to find code generator: LanguageExt.CodeGen.RWSGenerator in LanguageExt.CodeGen) (Unable to find code generator: LanguageExt.CodeGen.RecordWithAndLensGenerator in LanguageExt.CodeGen)
5>System.AggregateException: One or more errors occurred. (Unable to find code generator: LanguageExt.CodeGen.RWSGenerator in LanguageExt.CodeGen) (Unable to find code generator: LanguageExt.CodeGen.RecordWithAndLensGenerator in LanguageExt.CodeGen) ---> System.InvalidOperationException: Unable to find code generator: LanguageExt.CodeGen.RWSGenerator in LanguageExt.CodeGen
5>   at Validation.Verify.FailOperation(String message, Object[] args)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.GetCodeGeneratorTypeForAttribute(INamedTypeSymbol attributeType, Func`2 assemblyLoader)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.FindCodeGenerators(ImmutableArray`1 nodeAttributes, Func`2 assemblyLoader)+MoveNext()
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.TransformAsync(CSharpCompilation compilation, SyntaxTree inputDocument, String projectDirectory, Func`2 assemblyLoader, IProgress`1 progress)
5>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.GenerateAsync(IProgress`1 progress, CancellationToken cancellationToken)
5>   --- End of inner exception stack trace ---
5>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.GenerateAsync(IProgress`1 progress, CancellationToken cancellationToken)
5>   at CodeGeneration.Roslyn.Generate.Program.Main(String[] args)
5>---> (Inner Exception #0) System.InvalidOperationException: Unable to find code generator: LanguageExt.CodeGen.RWSGenerator in LanguageExt.CodeGen
5>   at Validation.Verify.FailOperation(String message, Object[] args)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.GetCodeGeneratorTypeForAttribute(INamedTypeSymbol attributeType, Func`2 assemblyLoader)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.FindCodeGenerators(ImmutableArray`1 nodeAttributes, Func`2 assemblyLoader)+MoveNext()
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.TransformAsync(CSharpCompilation compilation, SyntaxTree inputDocument, String projectDirectory, Func`2 assemblyLoader, IProgress`1 progress)
5>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.GenerateAsync(IProgress`1 progress, CancellationToken cancellationToken)<---
5>
5>---> (Inner Exception #1) System.InvalidOperationException: Unable to find code generator: LanguageExt.CodeGen.RecordWithAndLensGenerator in LanguageExt.CodeGen
5>   at Validation.Verify.FailOperation(String message, Object[] args)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.GetCodeGeneratorTypeForAttribute(INamedTypeSymbol attributeType, Func`2 assemblyLoader)
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.FindCodeGenerators(ImmutableArray`1 nodeAttributes, Func`2 assemblyLoader)+MoveNext()
5>   at CodeGeneration.Roslyn.Engine.DocumentTransform.TransformAsync(CSharpCompilation compilation, SyntaxTree inputDocument, String projectDirectory, Func`2 assemblyLoader, IProgress`1 progress)
5>   at CodeGeneration.Roslyn.Engine.CompilationGenerator.GenerateAsync(IProgress`1 progress, CancellationToken cancellationToken)<---
5>
5>TestCodeGen.cs(1,1): error CGR001: System.InvalidOperationException: Unable to find code generator: LanguageExt.CodeGen.RecordWithAndLensGenerator in LanguageExt.CodeGen
5>C:\Users\paul\.nuget\packages\codegeneration.roslyn.buildtime\0.7.5-alpha\build\CodeGeneration.Roslyn.BuildTime.targets(107,5): error MSB3073: The command "dotnet codegen "@obj\Debug\netcoreapp3.1\TestBed.csproj.dotnet-codegen.rsp"" exited with code 3.
5>Done building project "TestBed.csproj" -- FAILED.
========== Rebuild All: 4 succeeded, 1 failed, 0 skipped ==========

The project file for the testbed looks like this (again, only changed 0.6.1 to 0.7.5-alpha):

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.0</TargetFramework>
        <LangVersion>8.0</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <DotNetCliToolReference Include="dotnet-codegen" Version="0.7.5-alpha" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="CodeGeneration.Roslyn.BuildTime" Version="0.7.5-alpha">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
        <PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
        <PackageReference Include="System.ValueTuple" Version="4.5.0" />
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\..\LanguageExt.CodeGen\LanguageExt.CodeGen.csproj" />
        <ProjectReference Include="..\..\LanguageExt.Core\LanguageExt.Core.csproj" />
        <ProjectReference Include="..\..\LanguageExt.FSharp\LanguageExt.FSharp.csproj" />
        <ProjectReference Include="..\..\LanguageExt.Parsec\LanguageExt.Parsec.csproj" />
    </ItemGroup>

</Project>

It seems the code-gen tool is running, but none of the generators are found in the LanguageExt.CodeGen project.

Any thoughts?

amis92 commented 4 years ago

So, let's start here: https://github.com/AArnott/CodeGeneration.Roslyn/wiki/Migrations

This should be linked in the changelog. If you still have trouble, follow up.

amis92 commented 4 years ago

The link should probably be more prominently placed in the changelog though, now that I've looked at it ;D

louthy commented 4 years ago

Thanks for the link. I get the same errors though.

I have committed the changes to a new branch here in case you can see any obvious issues.

(btw, did all the usual things, clean build, rebuild, delete obj/bin, restart VS, etc.)

amis92 commented 4 years ago

Hmm. Well, one obvious thing is that you set OutputItemType="ResolvedGeneratorReferencePaths", while in the wiki it's OutputItemType="CodeGenerationRoslynPlugin". Out of curiosity, where did you take ResolvedGeneratorReferencePaths from? :)

louthy commented 4 years ago

@amis92 Wow,that is bizarre! Not really sure, I must have picked it from somewhere, but can't see anything obvious, perhaps it was in the clipboard and I'd thought I'd copied the "CodeGenerationRoslynPlugin" attribute value, but hadn't ... shrugs. Clearly it's been a long day ;)

I replaced it the attribute value with the correct one. It seems there's been a breaking change in Roslyn, which causes a load of new errors (not related to the CodeGeneration.Roslyn project).

This:

    ParenthesizedLambdaExpression(body).WithParameters(ps);

Throws a parameterList is null argument exception, so I needed to change some of the generation code to use:

    ParenthesizedLambdaExpression(ps, body);

Once that was done, and I'd done a clean build, everything worked as expected.

Thanks for your help 👍 I hope this gives a bit more confidence to the state of the alpha.

louthy commented 4 years ago

Found it:

https://github.com/AArnott/CodeGeneration.Roslyn/blob/dadd6396e0a3d5968ba3de12fc2dcbb12a1fc995/samples/GeneratorInConsumerSolution/Sample.Consumer/Sample.Consumer.csproj#L19

Must have copied the commented out section by mistake.

amis92 commented 4 years ago

Good catch. This'll get removed by #194

So, aside of Roslyn doing a breaking change, are you happy to approve the 0.7? :)

louthy commented 4 years ago

@amis92 Yes, I think if it survives all my test code then the likelihood of a regression is very small. It includes a Record-type generators, Union-type generators, Reader monad generator, RWS monad generators, and several With and lens generators. These do a lot of work.

amis92 commented 4 years ago

Closing as v0.7.57-alpha was released which should solve these problems: https://github.com/AArnott/CodeGeneration.Roslyn/issues/191#issuecomment-610570160