Repository navigation
Description
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:
serverpod/tools/serverpod_cli/lib/src/commands/start/watch_session.dart
Lines 354 to 358 in 99c3387
| 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:
serverpod/tools/serverpod_cli/lib/src/commands/start/watch_session.dart
Lines 497 to 503 in 99c3387
| // 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 analyzeaccepts. Agit mergethat 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 reportingError: 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
-
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_serverAdd
lib/src/greetings/prefix.dart:String prefix() => 'Hello';
In
lib/src/greetings/greeting_endpoint.dart, addimport 'prefix.dart';and change the message tomessage: '${prefix()} $name',. -
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". -
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. -
Remove the stray
}from the end ofgreeting_endpoint.dartand save:Files changed Analyzing changes... Analyzing changes done. (41ms) Generating code... Generating code done. (0.1s) Compiling server... Compiling server done. (15ms) ✓ Server reloaded. -
Run the
curlcommand again. The response still has"message":"Hello Bob", whileprefix.dartsays'Hi'. -
Save
prefix.dartagain 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
- It still reproduces with perf: Remove CLI code generation heuristics and improve performance #5825 merged into
main. That PR changes which files are sent to generation, not what happens after a failed one. - fix: Incremental compile must see all .dart files #4876 fixed an earlier case of the incremental compiler not seeing all
.dartchanges. - I have a fix with tests, which carries the batch over through
_pendingPathsthe way a rejected compile does, and can open a PR.
How experienced are you with this library?
None
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity