Skip to content

fix(native-dart): format generated clients - #1631

Open
cadivus wants to merge 4 commits into
mainfrom
fix/dart-codegen-format-output
Open

cadivus wants to merge 4 commits into
mainfrom
fix/dart-codegen-format-output

Conversation

@cadivus

@cadivus cadivus commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Problem

blocks_codegen 0.2.0 returns raw Dart source from the direct generator API,
build_runner, and the CLI. A generated client can be reproduced and rejected by
the SDK formatter with:

dart run blocks_codegen --spec blocks.spec.json --output lib/blocks.blocks.dart
dart format --output=none --set-exit-if-changed lib/blocks.blocks.dart

Before this fix, the second command exits nonzero because the generated file is
not canonical Dart formatter output.

Issue #, if available:

Changes

  • Format the complete generated compilation unit once in
    DartCodeGenerator.generate() after generated-name validation.
  • Add a first-line // dart format width=80 marker and a deterministic Dart
    3.11 fallback for direct generator calls.
  • Resolve the target package language version from package_config for
    build_runner and CLI output or stdout contexts, using APIs shared by
    package_config 2.x and 3.x.
  • Preserve malformed package-config and formatter failures as concise Error:
    diagnostics without writing output.
  • Add direct dart_style, package_config, and pub_semver dependencies.
  • Regenerate all 43 Dart fixture goldens with canonical formatting. No Swift or
    Kotlin fixture changed.
  • Add generator, resolver, CLI, scratch-package, all-fixture idempotence, and
    semantic assertion tests that retain token and record-comma distinctions.
  • Document generated formatting behavior and add the manual Dart changelog
    entry.

Review note: most fixture changes are generated-only canonical whitespace and
formatter-managed trailing commas. This branch is based on origin/main at
e1e4844 and includes #1573's generated namespace naming semantics. PR #1481
may still conflict in the Dart generator and goldens; rebase and rerun Dart-only
regeneration if it merges first.

Validation

  • Red/green regression: dart test test/generated_code_compiles_test.dart
    failed before the fix because dart format changed both scratch clients, then
    passed after the fix and golden regeneration.
  • All 455 tests passed on default Dart 3.13, exact Dart 3.11.0,
    build 5.0.0/package_config 3.0.0, and
    build 4.0.11/package_config 2.2.0.
  • Before rebasing onto fix(native-dart)!: don't double Api in generated namespace class names #1573, the then-current suite passed at the lowest
    compatible resolution: build 4.0.8, dart_style 3.1.11,
    package_config 2.2.0, and pub_semver 2.1.5.
  • All 43 Dart goldens analyzed without errors and were formatter-idempotent on
    Dart 3.11.
  • Raw CLI output for a Flutter 3.47/Dart 3.13 consumer required 0 formatter
    changes and had no analyzer issues.
  • dart format --output=none --set-exit-if-changed .,
    dart analyze --no-fatal-warnings ., npm run build,
    npm run lint:deps, and npm run test:e2e:local passed.
  • dart pub publish --dry-run passed with 0 warnings. Nothing was published.

Three broader failures reproduce on clean origin/main and are unrelated to this
change: npm test hits sandbox process/EPERM and workspace detection failures;
native/dart/run-e2e.sh cannot resolve hosted versus path blocks_runtime;
and tests after dart pub downgrade cannot load through
frontend_server_client.

Checklist

  • PR description included
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5eaf0c6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ekjotmultani

Copy link
Copy Markdown
Contributor

Hi @cadivus, we'd like this formatting change in Dart 1.0.0, so to save you the rebase I opened #1663 into your branch. It merges main and regenerates the Dart goldens with your formatter. Since your last push, #1574, #1578, #1481 and #1599 landed and touched the same goldens, so this one ended up with 43 conflicts.

What #1663 changes, on top of your commit:

  • 58feeaf: merges main. The goldens take main's side and are regenerated with your formatter (44 Dart goldens). Your CHANGELOG bullet stays first under ### Other changes.
  • b244937: moves the sink-guard pin to the toString line's new position. The old entry matched no site.
  • 8b1ddea: open_union_test.dart and open_enum_test.dart (from feat(native-codegen): decode enum values and union arms a client doesn't know #1481) use the formatter-tolerant contains from dart_source_matchers.dart, because the formatter splits the one-line snippets they checked.
  • 4027e1d: the dependency review failed on dart_style and pub_semver, whose license GitHub reports as lowercase bsd-3-clause. This adds both to allow_dependencies_licenses, the same exception the repo already has for build.

All checks on #1663 are green, and Swift and Kotlin goldens are unchanged. You can merge #1663 into your branch as is, or pull any local changes first. Feel free to drop or reword anything.

@cadivus
cadivus force-pushed the fix/dart-codegen-format-output branch from db9a94e to b397b92 Compare November 19, 2026 13:03
@ekjotmultani

Copy link
Copy Markdown
Contributor

Thanks for rebasing. Since you did, #1663 targets your old head and can be closed. Two checks still fail on b397b92:

  • Native Dart Analysis, at "Check formatting": test/open_enum_test.dart and test/open_union_test.dart need dart format.
  • Dependency Review: GitHub reports the license of dart_style and pub_semver as lowercase bsd-3-clause, which the action rejects as an invalid SPDX id. Changing the version range doesn't help. Commit 4027e1d in Merge main and update for 1.0.0 (into #1631) #1663 has the fix, the same exception the repo already makes for build. It adds this to allow_dependencies_licenses in .github/dependency-review-config.yml:
# dart_style and pub_semver are verifiably BSD-3-Clause:
# https://pub.dev/packages/dart_style/license
# https://pub.dev/packages/pub_semver/license
#
  - "pkg:pub/dart_style"
  - "pkg:pub/pub_semver"

You can cherry-pick it with git cherry-pick 4027e1de7.

@ekjotmultani

Copy link
Copy Markdown
Contributor

One more: the sink-guard pin in scripts/sink-guards/codegen.test.mjs:37 points at generator.dart:2171, but the guarded buf.writeln(" String toString() => '$label($props)';") line is at 2178 on b397b92. It should be generator.dart:2178, or the sink-guard test in PR Checks will fail.

cadivus and others added 4 commits November 19, 2026 15:01
The generator returned raw StringBuffer output from every sink, so
generated clients failed `dart format`.

generate() now formats the complete unit with dart_style using the target
package language version from package_config for build_runner and CLI,
with Dart 3.11 fallback. It prepends `// dart format width=80` so output
is stable regardless of consumer page_width. Dart goldens were regenerated.
…format layout

The rebase reformatted both files with Dart 3.11's formatter; CI checks
formatting with stable (3.14), which keeps main's layout. Restore it,
keeping only the switch to the Dart-source contains matcher.

This branch was previously deployed

1 inactive deployment
e2e — 919b4ce3 Deployed Nov 19, 2026 by cadivus via Dart E2E (sandbox) #7358
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants