Skip to content

fix: Refresh generated models that changed outside code generation - #6024

Merged
FXschwartz merged 4 commits into
serverpod:mainfrom
BiAksoy:fix/refresh-model-files-changed-outside-generation
Nov 25, 2026
Merged

FXschwartz merged 4 commits into
serverpod:mainfrom
BiAksoy:fix/refresh-model-files-changed-outside-generation

Conversation

@BiAksoy

@BiAksoy BiAksoy commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #6023.

When a pull or branch switch brings a model change together with its regenerated code, serverpod start and serverpod generate --watch keep analyzing the old generated model. An endpoint that uses the change then fails analysis ("The named parameter 'tag' isn't defined") and drops out of the generated code, on every generation until the command is restarted.

Since #5825, generation refreshes the analysis context with the model files it writes, and it does not write a file whose content is already on disk. After such a pull the regenerated model on disk already matches what generation produces, so it is not written and the context keeps the version it read before the pull. Models whose generated file imports protocol.dart are not affected, because refreshing the protocol.dart stub evicts them; leaf models are.

Analyzers now records the FileStamp (modification time and size, the same identity the generation staleness check uses) of each generated server model file as the analysis context last got it. After generating the project's models, performGenerate refreshes the written files plus every file whose stamp differs from the record, and records the stamps once the refresh has gone through. Runs that do not generate models check the stamps of the server model files already in the record and refresh those that changed. The record is seeded around the first update, where the context makes its first reads of generated files: a file in the server's or a shared model package's generated directory with the same stamp before and after that update is taken as read, or still to be read, in that state. Seeding is best effort; if a directory cannot be listed, the first model generation refreshes every model file instead. The client package is left out, since the server's analysis context does not read it. stampAll in generation_staleness.dart is made public for this.

The fix is in the analyzers rather than in the watch loops, so it covers both commands, the full generation that retryStart runs on already used analyzers, and a pull whose files reach the watcher over several runs.

Why stamps and not content hashes: the seeding and the runs that do not generate models have no file content at hand, and reading it there would mean reading every generated file at start-up and every model file on each endpoint edit. Without seeding, the first model generation would refresh every model file: about +900 ms on the first YAML edit of each watch session and +500 ms on every one-shot regenerate of the large benchmark project. A pull or checkout that rewrites a file gives it a new stamp, and a file whose stamp changes without its content changing is only refreshed once more than needed.

What it does not cover:

  • A run whose generated model is not on disk yet. If the endpoint of a pull reaches the watcher before the generated model it uses, that run fails and the run for the model recovers, as a one-shot serverpod generate on a half-written tree would.
  • A rewrite that keeps both size and modification time (an archive or sync tool that preserves or normalizes modification times, or a same-size rewrite within one timestamp tick) goes unnoticed until the file is next written, as for the generation staleness check.
  • Future call parameter models are refreshed when written, as before. Checking them as well cost about 900 ms on the first future call edit and up to a second on one-shot runs, for a rare case: a pulled parameter model whose future call does not change in the same session.

Tests: test/integration/generate/externally_regenerated_model_test.dart generates a project with one set of analyzers, primes a second set on it (as serverpod start on an up-to-date project), regenerates with a new field using a third set (the other checkout), adds an endpoint that uses the field, and then generates with the primed set, incrementally and as a full run. A third group runs the endpoint alone first, as when it reaches the watcher before the model file; it also fails when the seeding is turned off. All six tests fail on main and pass with this change. Also checked end to end in a fresh serverpod create project with git merge --ff-only under both serverpod start and serverpod generate --watch. The full tools/serverpod_cli suite passes locally except three tests in mcp_command_integration_test.dart, which fail the same way on main on this machine, and one timing-dependent test in sdk_resolver_test.dart that passed on every rerun.

Performance, dart run docs/process/generator_performance/compare.dart --baseline=b1dc2ee9a --repeats=3, macOS on an 18-core machine: no row shows a consistent difference; medians are within about 6 % in either direction, which is the spread between runs of the same baseline. For example, "First run on the up-to-date large project" (analysis, including the seeding) 10300 ms baseline and 10200 ms with this change; "Edit a YAML model in the large project" generation 6300 and 6400 ms; one-shot "Regenerate the large project after an edit" 15703 and 15296 ms.

The check in runs that do not generate models, measured against the first version of this PR (--baseline=c10ce1169 --repeats=3): "Edit an endpoint in the large project" 3444 ms before and 3422 ms after, the two future call edits 40 to 55 ms slower (wall clock medians). Rows that never reach this code move by as much in both directions: saving an unchanged plain Dart file, which does not generate, was 88 ms slower in one row and 50 ms faster in another. After the review changes, the generator suites (test/integration/generate/, test/generator/, test/commands/start/, test/analyzer/) pass, 4771 tests.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.
  • I added a changelog entry to changelog.d (dart run melos run changelog_add), or this change does not need one.

Breaking changes

None.

@FXschwartz FXschwartz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this one down @BiAksoy! Fixing it where the context gets refreshed rather than in the watch loops makes sense to me. A few questions before this goes in:

…dels

The files of a pull can reach the watcher in a run before the model files they come with, and that run analyzes endpoints and future calls against the generated models too. Stamps stay the record's identity because the seeding and these runs have no file content at hand.

@FXschwartz FXschwartz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick turnaround @BiAksoy! This all looks good to me now.

@FXschwartz
FXschwartz merged commit a30d6d0 into serverpod:main Nov 25, 2026
482 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Watch mode keeps analyzing old generated models after a pull that brings regenerated code

2 participants