Skip to content

Under serverpod start, a model generated in a failed cycle is deleted by the next one #5882

Closed

Description

@BiAksoy

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:

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:

// 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

  1. Create a project:

    serverpod create probe --template server --no-database --no-redis --no-auth
    cd probe/probe_server
  2. Run serverpod start and wait for Server running.

  3. 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.dart and probe_client/lib/src/protocol/notes/note.dart now exist, and protocol.dart exports notes/note.dart.

  4. Remove the stray } from the end of greeting_endpoint.dart and 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.dart files are gone. protocol.dart still imports and exports notes/note.dart.

  5. Save lib/src/notes/note.spy.yaml again 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

How experienced are you with this library?

None

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    bug
    on Oct 21, 2026
  2. self-assigned this
    on Oct 22, 2026
  3. moved this from Inbox 📫 to In Progress 🔥 in Serverpod Frameworkson Oct 22, 2026
  4. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions