Open
Description
We'd like a standardized way to generate code in Dart packages that does not require a user step.
- Macro's can only take Dart code as input and output.
package:build_runner
requires users to do a manual extra steps
In some hook/generate.dart
this would be a package:build_runner
equivalent, but it would be aware of the Dart/Flutter SDK and have no race conditions between saving files and running dart
and flutter
commands (see below).
In #54334, we've discussed multiple aspects w.r.t. code generators. Today we discussed some more requirements offline:
- Automatically running code generators before
pub publish
to ensure no files are outdated. - Automatically running code generators after editing dependencies. (Or having the IDE prompt to rerun them.)
- Automatically running code generators after
pub get
, beforedart analyze
. (Only needed if the generated code is not checked in.)
Assumptions:
- The generated files are checked in. (Maybe we should aim for not checking generated files in? E.g. it would work more like macros and you'd be able to resolve to the generated files in the IDE, but they are not checked in.)
- Code generators can be heavy to run (definitely longer-running than macros).
Some open questions:
Should it be a different hook thanhook/build.dart
? Or a mode for the build hook?If it's the build hook, when do we run this hook in which modes.If it's not the build hook, how do we ensure thisgenerate
hook is finished before running the build hook? (E.g. generate is run on-save of its dependencies, likebuild_runner
. But then if you hot-restart in Flutter, you want that on-save action to be fully done first.)- This should be a different hook from
hook/build.dart
, it's run in a different phase in the developer workflow. Build hooks are run onflutter build
/dart build
when you want to make an application bundle for a specific target (target OS, target architecture, target OS API levels, specific flavor, etc.) The generate hook should be run after dependencies change (and before runningdart analyze
, to prevent dart analysis errors showing up when both the source and target is Dart.
Use cases:
- FFIgen, JNIgen, Swiftgen interop
- translation messages (JSON -> Dart API)
- All
build_runner
setups
Background knowledge:
build.rs
enables generating of Rust files. https://doc.rust-lang.org/cargo/reference/build-script-examples.html#code-generation- (They don't mention how they solve IDE issues where code gen has not been run yet.)
Thanks @mosuem @HosseinYousefi @mkustermann @liamappelbe for the discussion! Please elaborate on things I left out.