Skip to content

fix(csharp): leave unchanged generated and ejected files unwritten - #631

Merged
dmealing merged 5 commits into
mainfrom
fm/mo-130-lane-csharp-2
Nov 21, 2026
Merged

dmealing merged 5 commits into
mainfrom
fm/mo-130-lane-csharp-2

Conversation

@dmealing

@dmealing dmealing commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

What Changed

  • CodegenRunner.WriteOne now compares content before writing on the no-manifest path too. A file already holding identical bytes returns unchanged instead of written. dotnet meta gen --fingerprint (GenFingerprint) rewrites its file only when the text changes.
  • dotnet meta eject --force routes every owned copy (generators, companion emitters, helper runtime) through a shared WriteOwnedCopy check. An identical copy is left alone and reported [unchanged].
  • Generators are split into overridable pieces. RoutesGenerator plans each file once (PlanStandardRoutes, PlanTphRoutes) and writes it through protected virtual Write* methods. Expression-indexes, report-export, requirement-tests, shared-model and api-docs follow the same shape, and ApiDocsGenerator is no longer sealed. Generated output is byte-identical to before.

Also updates CHANGELOG.md, docs/ports/csharp.md and docs/features/own-your-codegen.md to describe the unchanged-file status and the overridable generator pieces. Only the C# port changes.

Gate runs: this PR took 1 no-mistakes run.

The full sweep the gate left untested (every registered generator over the 394 loadable fixture models in four layouts, hashed before and after) was run locally before the gate, on the pre-change and post-change trees, and every hash matched.

Validation

Risk Assessment

✅ Low: Commit 1 adds compare-before-write on three paths with behavioral tests. Commit 2 is a mechanical split of RoutesGenerator into plan and Write* pieces, which I checked by a rename-normalized diff against the pre-change file: same emission order, same warning order, no logic change. The Codegen project builds with 0 warnings and 0 errors, and all newly protected signatures use public types. I did not run tests, so the byte-identical claim over the 394 fixtures is unverified by me.

Testing

Baseline scripts/ci-local.sh --affected --strict-toolchains already passed at the target commit; not re-run, no source edits here. I built the real dotnet meta CLI from base and target trees (git archive into scratch) and drove it end-to-end. Scenario 1: gen over persistence-conformance/canonical through a scaffolded codegen project wired to each tree. Base and target both wrote 287 files (227 generated, 60 docs); sha256 manifests match. Inheritance fixture also identical. Scenario 2: forced gen twice outside the project root (no-manifest path): base rewrote 227 files on run 2, target wrote 0. Scenario 3: forced eject twice over unedited copies: base replaced 17 copies (mtimes moved), target reported [unchanged] for all and left copy mtimes alone. Scenario 4 (adversarial): a hand-edited eject copy and a tampered generated file were both restored under --force; a follow-up forced run wrote 0. Ejected RoutesGenerator.cs source differs base vs target as expected from the refactor (source copy, not generated output). Scratch trees and outputs deleted. Worktree git status clean. Left running: some dotnet MSBuild/VBCSCompiler node-reuse processes from my builds; not killed (could not separate them from other sessions without name matching); they idle out on their own.

  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Generated C# output unchanged by the RoutesGenerator refactor: dotnet meta gen (built from base 9578cff and target 0e80885) over fixtures/persistence-conformance/canonical writes 287 files inc… ✅ pass live Real CLI from both trees run via codegen project handoff; diff -r and sha256 manifest match; see evidence file S1
Second generator run over the 'inheritance' fixture yields identical output on base and target (10 files) ✅ pass live gen run in scaffolded codegen project for each tree; diff -r of gen-out identical; evidence file S1
Forced regen of identical bytes writes nothing on the no-manifest gen path: second gen --force run outside the project root reports 0 written on target, while base rewrites all 227 files ✅ pass live ext-b run2 log: 227 files written; ext-t run2 log: 0 files written; evidence file S2
eject --force over unedited owned copies leaves them and their timestamps alone: target reports [unchanged] for all 17 copy lines and changes no copy mtime; base replaces 17 and rewrites them ✅ pass live eject-b/second.txt (17 [replaced]) vs eject-t/second.txt (17 [unchanged]); mtime diff shows only report files changed on target; evidence file S2b
Adversarial: a hand-edited owned copy and a tampered generated output file are still rewritten under --force (compare-before-write must not skip real changes) ✅ pass live eject routes --force -> [replaced] with reference bytes restored (cmp); gen --force -> 'written: ReportExportWriter.g.cs' with base bytes restored; following forced run 0 written; evidence file S3
Full sweep from the intent: every registered generator over the 394 loadable fixture models in four layouts hashes identical before and after ⏸️ untested no Time budget (about 10 min runbook) forced a subset. The same two-build comparison (base vs target CLI, sha256 per file) can cover the full sweep if the captain wants it before merge; the broad regress…
Evidence: C# bundle 1 live validation summary (base vs target CLI transcripts and counts)
# C# bundle 1 live validation (base 9578cff1a7 vs target 0e80885293)

## S1 generated output byte-identical (persistence-conformance/canonical, 287 files incl docs)
base run: dotnet meta gen: 287 file(s) written
target run: dotnet meta gen: 287 file(s) written
sha256 manifest match over 287 files
inheritance fixture: base 'dotnet meta gen: 10 file(s) written' target 'dotnet meta gen: 10 file(s) written', gen-out trees identical

## S2 forced re-gen of identical bytes writes nothing (outside project root => no-manifest path)
base run2: dotnet meta gen: 227 file(s) written
target run2: dotnet meta gen: 0 file(s) written

## S2b forced eject over unedited copies
--- base second eject:
replaced lines: 17
mtime-changed files base: 20 (incl. report files)
--- target second eject:
all copy lines unchanged: 17 lines
mtime-changed target: only the three report files

## S3 adversarial: edited copy/output rewritten under --force
eject --force over hand edit: [replaced], bytes restored
gen --force over tampered file: 'written: ReportExportWriter.g.cs', bytes restored; following plain forced run: 0 written

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Generated C# output unchanged by the RoutesGenerator refactor: dotnet meta gen (built from base 9578cff and target 0e80885) over fixtures/persistence-conformance/canonical writes 287 files inc… ✅ pass live Real CLI from both trees run via codegen project handoff; diff -r and sha256 manifest match; see evidence file S1
Second generator run over the 'inheritance' fixture yields identical output on base and target (10 files) ✅ pass live gen run in scaffolded codegen project for each tree; diff -r of gen-out identical; evidence file S1
Forced regen of identical bytes writes nothing on the no-manifest gen path: second gen --force run outside the project root reports 0 written on target, while base rewrites all 227 files ✅ pass live ext-b run2 log: 227 files written; ext-t run2 log: 0 files written; evidence file S2
eject --force over unedited owned copies leaves them and their timestamps alone: target reports [unchanged] for all 17 copy lines and changes no copy mtime; base replaces 17 and rewrites them ✅ pass live eject-b/second.txt (17 [replaced]) vs eject-t/second.txt (17 [unchanged]); mtime diff shows only report files changed on target; evidence file S2b
Adversarial: a hand-edited owned copy and a tampered generated output file are still rewritten under --force (compare-before-write must not skip real changes) ✅ pass live eject routes --force -> [replaced] with reference bytes restored (cmp); gen --force -> 'written: ReportExportWriter.g.cs' with base bytes restored; following forced run 0 written; evidence file S3
Full sweep from the intent: every registered generator over the 394 loadable fixture models in four layouts hashes identical before and after ⏸️ untested no Time budget (about 10 min runbook) forced a subset. The same two-build comparison (base vs target CLI, sha256 per file) can cover the full sweep if the captain wants it before merge; the broad regress…
  • scripts/ci-local.sh --affected --strict-toolchains
  • git archive 9578cff1a7 and 0e80885293 server/csharp into $DEV_SCRATCH; dotnet build -c Release -o the MetaObjects.Cli project in each (both 0 warnings, 0 errors)
  • dotnet <cli>/MetaObjects.Cli.dll init --root <proj> to scaffold a codegen project; rewrote codegen/Generators.cs with the 12 generator entries and Codegen.csproj ProjectReference to each tree's MetaObjects.Codegen
  • dotnet <cli>/MetaObjects.Cli.dll gen meta --out <dir> --namespace Mo.Fx on fixtures/persistence-conformance/canonical (base and target): 287 files written, diff -r and sha256 manifest identical including docs/csharp
  • Same gen on fixtures/codegen-conformance/inheritance input: 10 files each, trees identical
  • Two forced gen --force runs outside the project root (no-manifest path) on base and target; per-run 'written' counts and mtime capture
  • dotnet <cli>/MetaObjects.Cli.dll eject routes filter-allowlist --root <dir> then --force re-eject on base and target; status lines and mtime diff
  • Adversarial: hand-edited codegen/generators/RoutesGenerator.cs re-ejected with --force (target) -> [replaced], bytes restored; tampered ReportExportWriter.g.cs regenerated with --force -> rewritten, then follow-up forced run 0 written
⚠️ **Document** - 1 info
  • ℹ️ docs/ports/csharp.md says 'Every generator is written on it' (CsWriter). SharedModelGenerator, ApiDocsGenerator and TemplateGenerator emit JSON, Markdown or templates and do not use CsWriter. The layout ratchet (empty unconverted allowlist) does back the claim for generator source, so the sentence was left as is. Judgment call: tighten to 'every generator that emits C# source' if the author wants the literal reading.

🔧 Fix applied.
1 info still open:

  • ℹ️ docs/ports/csharp.md:332 - docs/ports/csharp.md said every generator is written on CsWriter. JSON, Markdown and template generators do not use it. Tightened the sentence to say every generator that emits C# source is written on CsWriter, and that the JSON, Markdown and template generators are not.
✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…nder eject --force

An unchanged file is never rewritten: CodegenRunner.WriteOne's no-manifest path,
every eject copy (generator, emitter companions, helper runtime) and the gen
fingerprint now read the file first and leave identical bytes alone.
…overridable pieces (ADR-0063 item 7)

W-CS-4 polish. RoutesGenerator works out a file's plan once (PlanStandardRoutes,
PlanTphRoutes) and writes it from one protected virtual method per piece: the
header, the list, item, create, update and delete routes, the M:N traversals,
the TPH polymorphic list and item routes and the sort dispatch. Its Append*
helpers become protected virtual Write*, and its other helpers and records are
protected. Expression-indexes, report-export, requirement-tests, shared-model
and api-docs get the same shape; ApiDocsGenerator is no longer sealed.

Output is byte-identical: every registered generator over the 394 loadable
fixture models in four layouts hashes the same before and after.
@dmealing
dmealing merged commit c9addb6 into main Nov 21, 2026
10 checks passed
@dmealing
dmealing deleted the fm/mo-130-lane-csharp-2 branch November 21, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant