Skip to content

serverpod start: Dart files changed together with a failed code generation are never compiled #5857

Closed

Description

@BiAksoy

Describe the bug

Under serverpod start, when code generation fails for a batch of file changes, the Dart files in that batch are never passed to the incremental compiler, in that cycle or in a later one. Once the error is fixed the session compiles and prints ✓ Server reloaded., but the server keeps running the previous content of those files until each of them is saved again or the session is restarted.

WatchSession._handleFileChange returns as soon as generation fails:

final genResult = await _generate(affectedPaths, genRequirements);
if (!genResult.success) {
log.error('Code generation failed. Server not reloaded.');
return;
}

The code that hands the batch to the compiler comes after that return, and nothing remembers the paths. The next cycle invalidates only its own files, so the Frontend Server keeps the old content of the earlier ones. The session already carries paths over when a compile is rejected, through _pendingPaths and _pendingPackageConfig, but a failed generation does not use them:

// Compile changes. Merge any paths carried over from prior rejected
// compiles so the FES re-invalidates them (reject rolls back its state).
final changedPaths = {..._pendingPaths, ...dartFiles};
// Likewise carry a package_config change forward if a prior compile that
// should have invalidated it failed.
final invalidatePackageConfig =
packageConfigChanged || _pendingPackageConfig;

What it looks like depends on the files that were dropped:

  • Old behaviour after a successful reload, as in the steps below.
  • Compile errors on a tree that dart analyze accepts. A git merge that stops on a conflict in a file an endpoint imports gets there: the merge brings new generated files in the same batch, generation fails on the conflict markers, and after the conflict is resolved the session keeps reporting Error: Type 'Purchase' not found. for a model whose generated file is on disk.

Several files land in one batch whenever they are written within the watcher's 100 ms debounce: Save All in an IDE, a refactoring, a coding agent, git checkout, merge or stash pop.

To reproduce

  1. Create a project and give the endpoint a plain Dart file to import:

    serverpod create probe --template server --no-database --no-redis --no-auth
    cd probe/probe_server

    Add lib/src/greetings/prefix.dart:

    String prefix() => 'Hello';

    In lib/src/greetings/greeting_endpoint.dart, add import 'prefix.dart'; and change the message to message: '${prefix()} $name',.

  2. Run serverpod start. In a second terminal:

    curl -X POST localhost:8080/greeting/hello -H 'content-type: application/json' -d '{"name":"Bob"}'

    The response has "message":"Hello Bob".

  3. Change the helper and break the endpoint file in one go:

    printf "String prefix() => 'Hi';\n" > lib/src/greetings/prefix.dart; echo '}' >> lib/src/greetings/greeting_endpoint.dart
    Files changed
    Analyzing changes...
    Analyzing changes done. (56ms)
    Generating code...
    ERROR: Found 1 issue.
    ERROR: Endpoint analysis skipped due to invalid Dart syntax. Please review and
    correct the syntax errors.
    [...]
    Generating code failed. (0.2s)
    ERROR: Code generation failed. Server not reloaded.
    
  4. Remove the stray } from the end of greeting_endpoint.dart and save:

    Files changed
    Analyzing changes...
    Analyzing changes done. (41ms)
    Generating code...
    Generating code done. (0.1s)
    Compiling server...
    Compiling server done. (15ms)
    ✓ Server reloaded.
    
  5. Run the curl command again. The response still has "message":"Hello Bob", while prefix.dart says 'Hi'.

  6. Save prefix.dart again with any change, for example an added blank line. Now the response has "message":"Hi Bob".

Expected behavior

After step 4 the server runs the code that is on disk, so step 5 returns "message":"Hi Bob". A file that changed in a cycle where generation failed should reach the compiler on the next compile.

Library version

Serverpod 4.0.0 and 4.0.4, and main at 99c3387 (4.1.0-beta.3). Dart 3.13.4.

Platform information

macOS 27.0.1 (arm64).

Additional context

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