Skip to content

Generated code standard

This page sets the rules for code that a tool writes: template output, source generators, Reqnroll code-behind, resource designers and project-specific generator scripts. It is written for engineers who change generated files, generators or templates. The development workflow already says that generated files change through their source (Development workflow); this page adds the checkable rules.

Generated files are exempt from the layout and documentation rules in File and type layout and XML documentation. How templates are built and validated is described in Contributing templates.

Rules

CS-TPL-001: Change the generator, never the generated output

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

The next run of the generator rewrites the whole output file. A hand edit in that file disappears without any build error, often in a change that has nothing to do with it. Content that only a hand edit added is lost.

Do

  • Find the generator's input (a template, a script's registration list, a .feature file, a .resx file or a template manifest), change it there, and run the generator.
  • Commit the input change and the regenerated output together.
  • When the output must differ from what the generator produces, change the generator so that it produces the difference, for example from an override setting.

Don't

  • Edit a file whose banner says it is generated.
  • Edit code that a template emits inside a generated file. Edit it in the template.

How to check

  • For each changed generated file in the diff, check that its generator or input also changed.
  • Run the generator and check that the output does not change.

CS-TPL-002: Commit Reqnroll code-behind and regenerate it after a restore

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

ConnectSoft repositories commit the *.feature.cs files that Reqnroll generates, so that the code that runs is visible in review. Reqnroll and MSBuild decide whether to regenerate by comparing file timestamps. A .feature file restored from a backup, or a file checked out with an older timestamp, can look older than its generated code. The build then keeps the old code-behind and runs a scenario that no longer matches its source. The same happens with any incrementally built source file.

Do

  • Commit each .feature file together with its regenerated .feature.cs.
  • After restoring a file by moving or copying a backup, set its timestamp to now (touch, or set LastWriteTime in PowerShell) or rebuild the project from clean.
  • When a scenario fails with a value that no source file contains, rebuild before investigating.

Don't

  • Edit a .feature.cs file by hand (CS-TPL-001).
  • Restore a file with mv or Copy-Item from a backup and run the tests without touching it.

How to check

  • In the diff, every changed .feature file has a changed .feature.cs file, and no .feature.cs file changed alone.
  • After a red-then-green check that swapped files, the restored file has a current timestamp.

CS-TPL-003: Mark generated files with a banner that names the generator

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

A reader can only follow CS-TPL-001 if they can tell that a file is generated and how to regenerate it. Analyzers also skip files that carry the standard <auto-generated> marker.

Do

  • Start every generated file with an <auto-generated> comment that names the generator and the command that regenerates it.
  • Make project-specific generator scripts write the same banner.

Don't

  • Generate a file with no banner.
  • Remove the banner from a generated file.

Example

// <auto-generated>
//     Generated by tools/generate-lookup-labels.py from Lookups.resx.
//     Do not edit. Regenerate with: python tools/generate-lookup-labels.py
// </auto-generated>

How to check

New generator scripts write a banner, and new generated files start with one.

CS-TPL-004: Pin each generator's output with a test

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: test · Status: active

Why

When a generator is run again after its output was edited by hand, the edits disappear and the build still passes. A test that asserts the set of generated items, such as the vocabularies, symbols or routes, catches the loss before it ships.

Do

  • For each project-specific generator, add a test that asserts the exact set of items the output must contain.
  • When adding an item, change the generator input, the generated output and the test's expected set together.

Don't

  • Assert only that the generated output is not empty.

How to check

A new or changed generator has a test that lists its expected output items.

CS-TPL-005: Create new repositories and projects with dotnet new

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

ConnectSoft templates apply symbols, renames and file exclusions when they generate a project. A copy of another repository that is renamed by hand keeps that repository's leftovers and misses the template's later fixes.

Do

  • Install the template with dotnet new install <template package or folder>.
  • Generate the repository or project with dotnet new <short name> -n <Name> and the documented parameters.
  • Add the files that template generation excludes on purpose, such as packaging and pipeline files, as new files afterwards.

Don't

  • Copy an existing repository or template working tree and rename it.

How to check

The pull request that creates a repository states the template, its version and the dotnet new command used.

CS-TPL-006: Never edit a base-template submodule checkout

Severity: major · Applies to: template · Enforced by: manual · Status: active

Why

The base-template folder in a derived template is a Git submodule of ConnectSoft.BaseTemplate. An edit there is lost when the submodule pointer moves, or ends up committed to the wrong repository.

Do

  • Change ConnectSoft.BaseTemplate first, then update the submodule pointer in each derived template, as described in Contributing templates.
  • Regenerate the derived template's snapshot files after a pointer bump.

Don't

  • Change files under base-template/ in a derived template repository.

How to check

The diff of a derived template contains no paths under base-template/ other than the submodule pointer.

CS-TPL-007: Write template conditionals that the template engine evaluates correctly

Severity: major · Applies to: template · Enforced by: test · Status: active

Why

The dotnet new engine processes C# conditionals before the compiler sees them, and some forms are evaluated wrongly without any error. The broken output appears only when a service is generated with a particular combination of options.

Do

  • Wrap every compound condition in parentheses: #if (UseRestApi || UseGrpc).
  • Include or exclude whole files with sources.modifiers conditions in template.json, rather than with a guard around a whole file that contains nested #if blocks.
  • Use a switch generator, not a condition on a generated symbol with replaces, when a replacement depends on an option.
  • Generate the option combinations that a change affects in the template's regression run.

Don't

  • Write #if A || B || C without parentheses.
  • Rely on line endings for a guard to be skipped correctly.

How to check

  • Search the change for #if lines that contain || or && without an opening parenthesis.
  • The template's CI generates the affected option combinations and builds them.
  • Check template.json changes for generated symbols that combine a condition with replaces. A replacement that depends on an option uses a switch generator.

CS-TPL-008: Generate, build and test every artifact kind and supported preset in template CI

Severity: major · Applies to: template, pipelines · Enforced by: manual · Status: active

Why

A template repository compiles with its default options only. Code under a non-default option, exclude lists for option combinations, and the second artifact kind of a template are never compiled unless the pipeline generates them, so their defects reach the first team that generates a service with those options.

Do

  • Keep a list of supported option presets in the template repository, including at least one non-default preset per optional feature.
  • In the template pipeline, generate each artifact kind with each preset, then build it and run its tests.
  • Keep the validation scripts the pipeline runs in the template repository.
  • Add a preset, or extend one, in the same change that adds a template conditional or an exclude entry.

Don't

  • Treat a green build of the template repository as proof that generated services build.
  • Run validation scripts from a workstation path.

How to check

Every conditional or exclude entry added by the change is covered by a preset that the pipeline generates and builds.

CS-TPL-009: Put every part of an optional feature under the same condition

Severity: major · Applies to: template · Enforced by: manual · Status: active

Why

An optional feature spans the package reference, the using directives, the source files, the registration, the settings sections and sometimes an MSBuild property. When one part is conditioned differently from the others, some option combinations generate a call without its definition, or a definition without its registration.

Do

  • Condition the package reference, the source files, the registration and the settings sections of a feature on the same template symbol.
  • Drive the matching MSBuild feature property from the same symbol, and do not let a host-specific props file override it.
  • Apply each template parameter to every file that carries its value, settings variants and props files included.
  • In a product template, force the base-template symbols that the product always needs.

Don't

  • Guard a registration but not the file that defines it, or the other way round.
  • Leave an unconditional call that depends on a package or section only some variants include.

How to check

For each symbol the change touches, the generated output with the symbol off and with it on both build and start.

CS-TPL-010: Use the full template source name in every identifier that must be renamed

Severity: minor · Applies to: template · Enforced by: manual · Status: active

Why

The template engine replaces the exact sourceName value. An identifier that contains only a fragment of it, such as a shortened prefix, keeps the template's name in every generated service.

Do

  • Spell every type, namespace and symbol that must follow the service name with the full sourceName value.
  • After adding identifiers, generate a service under another name and search the output for fragments of the template's name.

Don't

  • Abbreviate the source name in identifiers that the engine is expected to rename.

How to check

A generated service contains no fragment of the template's name.

CS-TPL-011: Regenerate committed copies of template output in CI and fail on a difference

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Why

Templates and services keep copies of generated material: composed configuration, consumer symbols, package snapshots and forked test harnesses. A copy that only people refresh drifts from its source, and the drift appears as a chain of CI failures after the next upstream change.

Do

  • Produce every committed snapshot with a script kept in the repository.
  • Run that script in CI and fail the build when its output differs from the committed copy.
  • Refresh the snapshots in the same change as the submodule bump or upstream change that alters them.

Don't

  • Edit a snapshot file by hand.
  • Keep a forked copy of generated projects when the template can generate them.

How to check

The pipeline regenerates each snapshot file and compares it with the committed copy.

CS-TPL-012: Compare a template-update merge with fresh template output

Severity: minor · Applies to: service, template · Enforced by: manual · Status: active

Why

A service updated from a newer template by a three-way merge can contain the same code twice: once where it was ported by hand earlier, and once where the template now puts it. The merge reports no conflict, and the duplicate shows up as a build error or an ambiguous step binding.

Do

  • Generate the new template version under the service's name, and compare each merged file with it.
  • Remove code that the template now provides and that was ported by hand earlier.
  • Build, and run the step-binding check, before review.

Don't

  • Treat a conflict-free merge as a correct one.

How to check

The pull request names the template version it merges, and the build and the step-binding check pass.