Skip to content

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

Done

Description

@BiAksoy

Describe the bug

With serverpod start or serverpod generate --watch running, a git pull (or a branch switch) that brings a model change together with its regenerated code and an endpoint that uses the change makes generation fail:

Endpoint analysis skipped due to invalid Dart syntax. Please review and correct the syntax errors.
.../lib/src/notes/note_endpoint.dart Error: The named parameter 'tag' isn't defined.

The generated files are rewritten without that endpoint. Every later generation in the same session fails the same way, even after the generated files are restored from git. Restarting the command fixes it, and a one-shot serverpod generate on the same tree succeeds.

To reproduce

  1. In a project created with serverpod create, add lib/src/notes/note.spy.yaml:
    class: Note
    fields:
      text: String
    and lib/src/notes/note_endpoint.dart:
    import 'package:serverpod/serverpod.dart';
    import '../generated/protocol.dart';
    
    class NoteEndpoint extends Endpoint {
      Future<Note> get(Session session) async => Note(text: 'a');
    }
    Run serverpod generate and commit everything, generated files included.
  2. On a second branch, add tag: String? to the model, change the endpoint to Note(text: 'a', tag: 'b'), run serverpod generate and commit everything.
  3. Check out the first commit and run serverpod start (or serverpod generate --watch).
  4. Once it is watching, git merge --ff-only the second branch.

Generation fails with the error above, and endpoints.dart, protocol.yaml and the client's client.dart no longer contain the note endpoint. Editing a comment in note_endpoint.dart triggers another generation, which fails the same way.

If the second commit leaves out the generated files, the same steps work.

Expected behavior

After the pull, watch mode generates from the tree as it is on disk, the same as a one-shot serverpod generate.

Library version

serverpod 4.1.2 (CLI and packages), also on main at b1dc2ee. Dart 3.14.0.

Platform information

macOS 27.2.1 (arm64).

Additional context

From reading the code:

  • Since perf: Remove CLI code generation heuristics and improve performance #5825, generation refreshes the analysis context only with the generated model files it writes, and it does not write a file whose content is already on disk.
  • After the pull, the regenerated model on disk already matches what generation produces, so it is not written, and the analyzers keep the version they read before the pull.
  • Models whose generated file imports protocol.dart escape this, because refreshing the protocol.dart stub evicts them. Leaf models like Note above do not.
  • The watch loops pass only source and model files to generation, so a changed generated file does not reach the analyzers that way either.

A fix that compares the generated model files with what the analysis context was last given, and refreshes the ones that differ, covers both commands. I have one ready with a test and benchmark numbers.

How experienced are you with this library?

None

Are you interested in working on a PR for this?

  • I want to work on this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions