Errors and the generated files
An error in your sources never removes or changes a *.spring.dart file that already exists. The generator writes the existing content again, unchanged, and reports the error.
Why
build_runner deletes an output that its build step does not write. Without care, every syntax error while you type would delete the generated file under build_runner watch, and every file that imports it would turn red, only to be regenerated on the next keystroke. The editor would flicker between hundreds of errors and none.
So the generator keeps the last good file until the sources are right again. Your editor shows the one real error, in the file you are editing.
What happens, case by case
| Situation | The generated file | The build | What you see |
|---|---|---|---|
| A scanned library has syntax errors, a generated file exists | written again unchanged | succeeds | one warning per output, until the next successful generation |
| The generator reports an error (a check fails, see Build-time checks), a generated file exists | written again unchanged | fails | the error, introduced by ... is kept unchanged until this error is fixed: |
| Either error, and no generated file exists yet (a cold build) | none | fails | the error |
| The sources are right again | generated | succeeds | the warning stops; a later syntax error warns again |
Syntax errors
While a library has syntax errors (you are in the middle of typing), the builder skips generation and logs one warning per output:
lib/spring_gen/src/beans.spring.dart is kept unchanged until the sources parse
again; lib/src/service.dart has syntax errors:
...
The warning is logged once. Further builds with the file still broken stay silent. After the next successful generation, a new syntax error warns again.
Errors of the generator
A failed check, or a crash on the element model of a library with errors, keeps the file too. The error is reported as the build error, so build_runner build fails, but the file stays and the code that imports it keeps compiling:
lib/spring_gen/src/beans.spring.dart is kept unchanged until this error is
fixed:
Bean class _Greeter must be public: the generated module cannot reference a
private class of package:app/src/greeter.dart. Make it public, or move it out
of the scanned directory.
A cold build
Without an existing output there is nothing to keep: a syntax error or a generator error fails the build, and no file is written.
Where the kept content comes from
- Under
watch, the builder remembers the last content it generated for each output in this process, and uses it. - After a fresh start, it reads the file from disk. Outputs are built to source, so the file is at its path in the directory of the output's package.
Workspace builds
build_runner build --workspace builds every package of a pub workspace from one working directory. The file to keep is always read from the directory of the package being built, found through the workspace's .dart_tool/package_config.json, never from the working directory. Otherwise another package's file of the same path could be written as this package's output.
When the built package has no such file, there is nothing to keep and the build fails as a cold build does. The same lookup is used for the Flutter assets the build checks (@Value('asset:...')): an asset is looked for in the built package's directory, not in another package's.
Tests: the spring_test builder
The test builder keeps its output too. While a test file with @SpringBootTest or @DataTest classes does not parse, its test/spring_gen/<name>.test.spring.dart is written again as it is, read from the test package's directory (also in a workspace build), and a warning says so:
test/checkout_test.dart has syntax errors; test/spring_gen/checkout.test.spring.dart
is kept as it is until it parses again:
...
So the test's import of the generated file keeps working while you edit. When there is no generated file yet, the warning is logged and nothing is written. See Tests: @SpringBootTest and @DataTest.