Skip to main content

Generated source

Most IDEs allow you to view the source code generated by Mapperly. Usually you can jump to the implementation via the partial mapper method. If your IDE doesn't easily allow this or if you want to check in the generated source into source control, you can emit the generated files.

To emit the generated files to disk set the EmitCompilerGeneratedFiles property:

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

By default the files are written to {BaseIntermediateOutpath}/generated/{Assembly}/Riok.Mapperly/{GeneratedFile}. With BaseIntermediateOutpath for example being obj/Debug/net9.0. By convention, generated C# source code files will have the file extension .g.cs.

The output path can be customized via the CompilerGeneratedFilesOutputPath property. This can be used to check the generated files into VCS. If the project path is inside the project directory structure, the files are picked up by the compiler additionally to the in-memory source generator output. Therefore the files need to be explicitly removed from the compilation.

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
<!-- Exclude the output of source generators from the compilation, show it in the IDE -->
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
<None Include="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>
info

Mapperly only emits updated source code on build. You won't see the updated mapper code or mapper diagnostics until you perform a build.

This is done for performance reasons, otherwise the IDE could become laggy.

Identifying generated code

Many developers and businesses use static code analysis and security analysis tooling to identify the quality of their code/applications. For example linting, code coverage, code smells, performance problems, vulnerabilities, and so on. However, code generators (like Mapperly) do not always align with the guidelines/best practices of each individual project and thus you may want to apply a different set of rules or ignore the generated code completely.

The code generated by Mapperly can be identified in several ways:

  • the path/location where the source code is saved (see previous section)
  • each file has a .g.cs file extension
  • each file starts with a commented out line: // <auto-generated />
  • generated member types and classes are annotated with the GeneratedCodeAttribute:
    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "1.0.0.0")]

    Note: the version 1.0.0.0 will be the version of the NuGet package of Mapperly that you are using.

Using these identification points, you can configure most tooling to apply a different set of rules to the generated source.