Skip to content

Latest commit

 

History

History
398 lines (272 loc) · 24.8 KB

File metadata and controls

398 lines (272 loc) · 24.8 KB

Public API for Flutter build rules

dart_format_test

load("@rules_flutter//flutter:defs.bzl", "dart_format_test")

dart_format_test(name, srcs)

Fails when any of the given Dart sources are not dart-format clean.

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
srcs Dart sources checked with dart format --set-exit-if-changed. List of labels required

dart_proto_library

load("@rules_flutter//flutter:defs.bzl", "dart_proto_library")

dart_proto_library(name, deps)

Generates Dart sources from proto_library targets using the Dart protoc plugin.

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
deps proto_library targets to generate Dart for. Generation covers the whole transitive proto closure (including well-known types such as google/protobuf/timestamp), matching what generated imports expect. List of labels required

flutter_analyze_test

load("@rules_flutter//flutter:defs.bzl", "flutter_analyze_test")

flutter_analyze_test(name, srcs, cpu, embed, extra_args, fatal_infos, fatal_warnings,
                     pub_cache_materialization)

Runs flutter analyze hermetically against a prepared flutter_library workspace.

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
srcs Additional files overlaid before analyzing (e.g. analysis_options.yaml, test sources). List of labels optional []
cpu Local CPUs to reserve for this test (execution requirement cpu:N); see flutter_test.cpu. 0 (default) declares nothing. Integer optional 0
embed flutter_library targets whose prepared workspace is analyzed. List of labels optional []
extra_args Additional arguments forwarded to flutter analyze. List of strings optional []
fatal_infos Treat info-level issues as fatal (--fatal-infos). Boolean optional False
fatal_warnings Treat warnings as fatal; set False to pass --no-fatal-warnings. Boolean optional True
pub_cache_materialization How the analyze run materializes the pub cache; see flutter_test.pub_cache_materialization. String optional "auto"

flutter_goldens

load("@rules_flutter//flutter:defs.bzl", "flutter_goldens")

flutter_goldens(name, srcs, embed, jobs, test_files, test_tags)

Regenerates Flutter golden images hermetically and writes them back to the source tree.

The build action runs flutter test --tags <test_tags> --update-goldens --run-skipped inside a prepared workspace (dart_proto_library trees and other generated_srcs already mounted, so no source-tree pre-population/refresh is required) and captures the produced **/goldens/** PNG trees as a declared output — so an unchanged embed/test/lib graph is a cache hit and re-renders nothing. bazel run copies the (cached) goldens back into the source tree for review and commit.

By default it runs only golden-tagged tests (test_tags = ["golden"]), so a golden-test failure fails the action but it is NOT a general widget-test gate — keep a separate flutter_test target for that. The scoping is deliberate: --run-skipped un-skips tests, and limiting it to the golden tag ensures tests that are skip: true for unrelated reasons are never run. Set test_tags = [] to run every test under test_files (only safe when golden is the sole skip).

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
srcs Test sources (including golden tests) to overlay into the workspace. List of labels optional []
embed flutter_library target to embed (exactly one). List of labels optional []
jobs Concurrency for flutter test -j (0 = flutter's default). Integer optional 0
test_files Test files or directories whose golden tests to (re)generate. Also bounds the write-back's clear step: only goldens under these roots are cleared before restoring the regenerated set. List of strings optional ["test/"]
test_tags Tags passed to flutter test --tags, scoping --run-skipped. Defaults to ["golden"] so only golden-tagged tests are un-skipped and rendered — a test that is skip: true for an unrelated reason (parked/flaky/platform) is never run. Set to [] to run every test under test_files (unscoped --run-skipped; only safe when golden is the sole skip). List of strings optional ["golden"]

flutter_test

load("@rules_flutter//flutter:defs.bzl", "flutter_test")

flutter_test(name, srcs, cpu, embed, jobs, pub_cache_materialization, test_files)

Runs Flutter tests using a prepared flutter_library workspace.

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
srcs Test source files to copy into the runtime workspace. List of labels optional []
cpu Local CPUs to reserve for this test (execution requirement cpu:N). 0 (default) declares nothing. Only useful together with jobs/sharding on large workers — reserving cores where none are spare just serializes tests that would otherwise overlap. Integer optional 0
embed flutter_library targets to embed for testing. List of labels optional []
jobs Concurrency passed to flutter test -j. 0 (default) keeps flutter's own default (the number of cores). Cap this when several flutter_test targets run concurrently on one worker so their internal parallelism doesn't oversubscribe it. Integer optional 0
pub_cache_materialization How the test materializes the (multi-GB) pub cache at run time. auto (default) APFS-clones it on macOS (fresh copy-on-write inodes, kept writable) and byte-copies elsewhere — behaviorally identical to copy everywhere. copy forces the historical byte copy. hardlink opts into linking the dereferenced files (near-instant, but the links share inodes with the Bazel-owned tree and stay read-only; falls back to a copy when linking isn't possible). reference skips materialization entirely and points the regenerated package config into the Bazel-provided cache read-only. String optional "auto"
test_files Test files or directories to run List of strings optional ["test/"]

DartLibraryInfo

load("@rules_flutter//flutter:defs.bzl", "DartLibraryInfo")

DartLibraryInfo(srcs, deps, import_path, pubspec, pub_deps, pub_cache, transitive_pub_caches,
                assembled_cache)

Information about a Dart library

FIELDS

Name Description
srcs Source files for this library
deps Transitive dependencies of this library
import_path Import path for this library
pubspec The pubspec.yaml file for this library (optional)
pub_deps Dependency report copied from checked-in or repository-generated pub_deps.json (optional)
pub_cache The assembled pub cache directory for this library (optional)
transitive_pub_caches Depset of pub cache directories from all transitive dependencies
assembled_cache Whether pub_cache contains the full merged dependency closure (assemble_dep_caches). Only such libraries can be embedded.

DartProtoAspectInfo

load("@rules_flutter//flutter:defs.bzl", "DartProtoAspectInfo")

DartProtoAspectInfo(trees)

Internal: per-proto_library Dart generation results, propagated along deps.

FIELDS

Name Description
trees Depset of tree artifacts laid out by proto import path.

DartProtoLibraryInfo

load("@rules_flutter//flutter:defs.bzl", "DartProtoLibraryInfo")

DartProtoLibraryInfo(sources)

Generated Dart sources produced from .proto files.

FIELDS

Name Description
sources Depset of tree artifacts, one per proto_library in the transitive closure, each laid out by proto import path (e.g. api/v1/service.pb.dart). Mount them into a package workspace with the generated_srcs attribute of flutter_library/dart_library.

FlutterLibraryInfo

load("@rules_flutter//flutter:defs.bzl", "FlutterLibraryInfo")

FlutterLibraryInfo(workspace, pub_get_log, pub_cache, pub_deps, dart_tool, pubspec, dart_sources,
                   other_sources, transitive_pub_caches, assembled_cache)

Outputs from flutter_library needed to build or test Flutter targets.

FIELDS

Name Description
workspace Prepared Flutter workspace tree artifact containing project sources and pub outputs.
pub_get_log Captured log from dependency preparation (pub deps, cache assembly, and generation commands).
pub_cache Tree artifact containing the assembled pub cache for this library.
pub_deps JSON dependency report copied from checked-in or repository-generated pub_deps.json.
dart_tool Tree artifact containing the generated .dart_tool/package_config.json.
pubspec The pubspec.yaml file for this library.
dart_sources Depset of Dart source files that make up the library.
other_sources Depset of non-Dart source files bundled with the library.
transitive_pub_caches Depset of pub cache directories from all transitive dependencies
assembled_cache Whether pub_cache contains the full merged dependency closure (assemble_dep_caches). Only such libraries can be embedded.

dart_library

load("@rules_flutter//flutter:defs.bzl", "dart_library")

dart_library(name, create_update_target, create_format_target, create_sync_target,
             update_visibility, update_tags, **kwargs)

Defines a dart_library target and optional .update/.format helpers.

PARAMETERS

Name Description Default Value
name Target name for the dart_library rule. none
create_update_target Whether to emit the runnable .update helper (only if pubspec is provided). True
create_format_target Whether to emit the runnable .format helper (only if pubspec is provided). True
create_sync_target Whether to emit the runnable .sync helper (only if generated_srcs is set). True
update_visibility Optional visibility override for the .update target. None
update_tags Optional tag list override for the .update target. None
kwargs Forwarded to the underlying dart_library rule. none

flutter_app

load("@rules_flutter//flutter:defs.bzl", "flutter_app")

flutter_app(*, name, embed, srcs, visibility, tags, testonly, dart_defines, build_args, mode, env,
            android_sdk, android_ndk, create_dev_target, dev_run_args, web, apk, appbundle, ios,
            macos, linux, windows)

Macro that defines flutter_app platform targets.

Each platform attribute (web, apk, ios, macos, linux, windows) accepts either labels for files that should be overlaid into the Flutter workspace when building for that platform, or a dict spec with any of the keys srcs, dart_defines, build_args, mode, env, android_sdk, android_ndk, android_test, build_name, build_number, and tags to customize that platform's build. A target is emitted only when the corresponding attribute is provided. Spec tags extend the macro-level tags (e.g. to mark only the mobile platforms manual).

Common dart_defines/build_args/mode/env apply to every platform; per-platform values merge over them (build_args concatenates, dicts merge with platform keys winning, mode overrides).

PARAMETERS

Name Description Default Value
name The base name for the flutter_app targets. none
embed List of flutter_library targets to embed. none
srcs Additional source files to include in the build workspace. None
visibility Visibility specification for generated targets. None
tags Tags to apply to generated targets. None
testonly Whether the targets are testonly. False
dart_defines Dict of --dart-define key/value pairs shared by all platforms. Supports select(); compose complete dicts per select() branch. None
build_args Extra flutter build arguments shared by all platforms. None
mode Build mode (release, profile, debug) shared by all platforms. None
env Extra action environment variables shared by all platforms. None
android_sdk Android SDK directory for apk/appbundle targets (e.g. rules_android's @androidsdk//:sdk_path). None
android_ndk Optional Android NDK directory (e.g. from rules_android_ndk's @androidndk). None
create_dev_target Whether to emit a runnable {name}.dev helper (when web is configured) that runs flutter run -d web-server in the source workspace with the hermetic SDK and the web dart_defines. True
dev_run_args Extra args forwarded to flutter run by the dev helper. None
web Files or dict spec for the {name}.web target. None
apk Files or dict spec for the {name}.apk target. None
appbundle Files or dict spec for the {name}.appbundle target (Android App Bundle; requires an Android SDK toolchain, see flutter.android_sdk). None
ios Files or dict spec for the {name}.ios target. None
macos Files or dict spec for the {name}.macos target. None
linux Files or dict spec for the {name}.linux target. None
windows Files or dict spec for the {name}.windows target. None

flutter_build_settings

load("@rules_flutter//flutter:defs.bzl", "flutter_build_settings")

flutter_build_settings(name, mode_default, build_number, visibility)

Emit the command-line build settings a release/multi-env app needs.

flutter_app's mode and build_number are plain attributes meant to be driven by select() on user build settings. This macro creates the usual scaffolding so you don't hand-roll it:

  • {name}_mode: a string_flag over debug/profile/release (default mode_default), plus a {name}_<mode> config_setting for each mode.
  • {name}_build_number: a string_flag (default empty) when build_number is True, so a release wrapper can inject a version code on the command line instead of rewriting pubspec.yaml.

Wire them into flutter_app, e.g.:

flutter_app(
    name = "app",
    apk = {
        "srcs": [":android_srcs"],
        "mode": select({
            ":settings_release": "release",
            "//conditions:default": "debug",
        }),
        "build_number": ":settings_build_number",
        "android_sdk": "@androidsdk//:sdk_path",
    },
    ...
)

then build with --//your/pkg:settings_mode=release --//your/pkg:settings_build_number=42.

PARAMETERS

Name Description Default Value
name Prefix for the emitted targets ({name}_mode, {name}_<mode> config_settings, {name}_build_number). none
mode_default Default build mode (debug/profile/release) for the mode flag. "release"
build_number Whether to emit the {name}_build_number string_flag. True
visibility Optional visibility applied to every emitted target. None

flutter_library

load("@rules_flutter//flutter:defs.bzl", "flutter_library")

flutter_library(name, create_update_target, create_format_target, create_sync_target,
                update_visibility, update_tags, **kwargs)

Defines a flutter_library target and optional .update/.format helpers.

PARAMETERS

Name Description Default Value
name Target name for the flutter_library rule. none
create_update_target Whether to emit the runnable .update helper. True
create_format_target Whether to emit the runnable .format helper (dart format write-back over the package source directory). True
create_sync_target Whether to emit the runnable .sync helper, which writes generated_srcs (e.g. proto outputs) back into the source tree for the IDE analyzer. Only emitted when generated_srcs is set. True
update_visibility Optional visibility override for the .update target. None
update_tags Optional tag list override for the .update target. None
kwargs Forwarded to the underlying flutter_library rule. none