Skip to content

[hook] hook/generate.dart 🪝 #56512

Open
@dcharkes

Description

@dcharkes

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, before dart 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 than hook/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 this generate hook is finished before running the build hook? (E.g. generate is run on-save of its dependencies, like build_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 on flutter 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 running dart 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:

Thanks @mosuem @HosseinYousefi @mkustermann @liamappelbe for the discussion! Please elaborate on things I left out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart-cliUse area-dart-cli for issues related to the 'dart' command like tool.area-sdkUse area-sdk for general purpose SDK issues (packaging, distribution, …).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions