Repository navigation
Description
Describe the bug
Under serverpod start, when a batch adds a model and code generation fails for another reason in the same batch (for example a syntax error in an endpoint), the model's generated files are written anyway. Once the error is fixed, the next cycle runs a protocol-only generation that deletes those files again, while protocol.dart still imports and exports them. The server then fails to compile a valid tree:
ERROR: lib/src/generated/protocol.dart:17:8: Error: Error when reading
'lib/src/generated/notes/note.dart': No such file or directory
It stays that way until the model file is saved again or serverpod generate is run.
The generation stamp is written only when generation succeeds:
serverpod/tools/serverpod_cli/lib/src/commands/generate.dart
Lines 324 to 333 in 60a7176
| if (result.success) { | |
| await writeGenerationStamp( | |
| config, | |
| generatedFiles: result.generatedFiles, | |
| // A full run reuses the stamps captured at enumeration; an incremental | |
| // run re-enumerates, since its affected paths is only the changed subset. | |
| sourceStats: incremental ? null : sourceStats, | |
| ); | |
| log.debug(incrementalCodeGenerationComplete); | |
| } |
A protocol-only generation keeps the model files listed in that stamp and lets cleanPreviouslyGeneratedFiles remove the other files in the generated model directories:
serverpod/tools/serverpod_cli/lib/src/generator/analyzers.dart
Lines 585 to 618 in 60a7176
| // When doing protocol-only generation, we need to preserve existing model | |
| // files from the generation stamp so they don't get cleaned up. | |
| if (!requirements.generateModels) { | |
| final previouslyGeneratedModelsDirs = [ | |
| p.joinAll(config.generatedServeModelPathParts), | |
| p.joinAll(config.generatedDartClientModelPathParts), | |
| ...config.generatedSharedModelsPaths, | |
| ]; | |
| // Keep previous model files so they don't get deleted. | |
| final previousFiles = readGenerationStamp(config); | |
| var previousModelFiles = previousFiles.where( | |
| (f) => previouslyGeneratedModelsDirs.any((dir) => p.isWithin(dir, f)), | |
| ); | |
| // When future call parameter models are regenerated, the ones | |
| // left over from the previous run are stale and should be cleaned. | |
| if (requirements.generateFutureCallModels) { | |
| previousModelFiles = previousModelFiles.where( | |
| (f) => !p.split(f).contains(futureCallModelsDirectoryName), | |
| ); | |
| } | |
| allGeneratedFiles.addAll(previousModelFiles); | |
| log.debug( | |
| 'Preserving ${previousModelFiles.length} existing model files from stamp.', | |
| ); | |
| } | |
| await ServerpodCodeGenerator.cleanPreviouslyGeneratedFiles( | |
| generatedFiles: allGeneratedFiles, | |
| protocolDefinition: protocolDefinition, | |
| config: config, | |
| ); |
A model written by the failed run is not in the stamp, so the next protocol-only run removes it. With -v, that cycle logs Preserving <n> existing model files from stamp. followed by Remove: File: '...' for the new model's server and client files. I have read this from the code and the debug log, not confirmed it with a test.
A batch like this comes from anything that writes several files within the watcher's debounce: a git merge or checkout, stash pop, Save All, a refactoring or a coding agent. Since #5825, the merge case in #5857 runs into it: after the conflict is resolved, that cycle now generates, and the model files the merge brought in are deleted.
To reproduce
-
Create a project:
serverpod create probe --template server --no-database --no-redis --no-auth cd probe/probe_server -
Run
serverpod startand wait forServer running. -
In a second terminal, add a model and break the endpoint file in one go:
mkdir -p lib/src/notes && printf 'class: Note\nfields:\n text: String\n' > lib/src/notes/note.spy.yaml; echo '}' >> lib/src/greetings/greeting_endpoint.dart
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.1s) ERROR: Code generation failed. Server not reloaded.lib/src/generated/notes/note.dartandprobe_client/lib/src/protocol/notes/note.dartnow exist, andprotocol.dartexportsnotes/note.dart. -
Remove the stray
}from the end ofgreeting_endpoint.dartand save:Generating code... Generating code done. (83ms) Compiling server... Compiling server done. (12ms) ✓ Server reloaded. [...] Compiling server... Compiling server failed. (11ms) ERROR: lib/src/generated/protocol.dart:17:8: Error: Error when reading 'lib/src/generated/notes/note.dart': No such file or directory ERROR: import 'notes/note.dart' as _iylmodbg; [...] ERROR: lib/src/generated/protocol.dart:62:24: Error: Undefined name 'Note'.Both generated
note.dartfiles are gone.protocol.dartstill imports and exportsnotes/note.dart. -
Save
lib/src/notes/note.spy.yamlagain with any change, for example an added blank line. The session generates the model, and the server compiles.
When the model is added on its own first, and the endpoint is broken and fixed afterwards, the model's files are kept.
Expected behavior
After step 4 the generated files of the model are on disk and the server compiles. A model written in a failed cycle should not be removed by the next generation while the protocol still refers to it.
Library version
Serverpod 4.0.0 (steps above) and main at 60a7176 (same behaviour with an equivalent script). Dart 3.13.5. 4.0.4 was not run, but it has the same stamp and cleanup code.
Platform information
macOS 27.0.1 (arm64).
Additional context
- The one-shot
serverpod generateruns a full generation, so it is probably not affected; only the watcher's incremental cycles are. Not checked. - Related:
serverpod start: adding a future call with parameters generates code that imports a model file that is never written #5760 had the same symptom for future call parameter models, written only when a cycle generated models; perf: Remove CLI code generation heuristics and improve performance #5825 fixed that. This case still happens onmainwith perf: Remove CLI code generation heuristics and improve performance #5825, because here the model files are written and then removed by cleanup. - Related:
serverpod start: Dart files changed together with a failed code generation are never compiled #5857 (files of a failed cycle not reaching the compiler), fixed by fix: Compile file changes that arrived with a failed code generation #5858. This one is separate: the files are compiled, but the generator has already deleted them.
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