Generator: builders collide with json_serializable, and Ack models cannot be nested in @JsonSerializable classes

Aperta
#189 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
dart
Ambito
build-system

Direzione di ricerca

Start with packages/ack_generator/build.yaml and lib/src/builder.dart, then review json_serializable_build_test.dart:149-157 for the existing collision workaround. Compare builder ordering, generated fromJson shapes, and .ack.dart outputs before selecting a collision strategy. Done means consumer projects can use @JsonSerializable classes containing Ack models without disabling builders or adding excludes.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

Ack's builders do not coexist cleanly with json_serializable. Two separate problems, found by building real consumer projects against main plus the unified @Schemable work.

1. Any @JsonSerializable class in an Ack project stops the build

The legacy ack_generator builder is a LibraryBuilder that writes .g.dart and is auto-applied to every dependent package (packages/ack_generator/build.yaml:2-7, lib/src/builder.dart:18-20). source_gen:combining_builder writes .g.dart too, so adding one @JsonSerializable class fails with:

Builders source_gen:combining_builder and ack_generator outputs collide

The repository's own test carries the workaround (json_serializable_build_test.dart:149-157). A consumer has to disable ack_generator:ack_generator in their build.yaml. Disabling source_gen:combining_builder instead also "works", but it turns json_serializable off for that package.

Likely fix: auto_apply: none on the legacy builder.

2. An Ack model cannot be a field of a @JsonSerializable class

@JsonSerializable()
class Order {
  final HabitDto habit; // an Ack model
}

Three things stand in the way:

  1. Ordering. ack_models does not declare runs_before: json_serializable, so the generated mixin (and its toJson) does not exist when json_serializable runs.
  2. fromJson shape. The README's static final fromJson = XSchema.fromJson; is not accepted by json_serializable, which needs a fromJson constructor or factory.
  3. Output collision. Once Ack runs first, x.ack.dart becomes an input to combining_builder, whose declared output for it is x.ack.g.dart. Ack already writes a file with that name. The build only succeeds when the consumer excludes **/*.ack.dart from combining_builder and json_serializable.

Options for the collision:

  • Rename Ack's second part (for example .ack.json.dart). Changes one part line in every model file.
  • Merge Ack's two generated parts into one. No second file, so nothing collides, and users write one part line instead of two. A larger generator change.
  • Keep the names and document the consumer-side exclude.

Workaround until this is fixed

# build.yaml in the consumer package
targets:
  $default:
    builders:
      ack_generator:ack_generator:
        enabled: false

For case 2, also exclude **/*.ack.dart from source_gen:combining_builder and json_serializable:json_serializable, and declare fromJson as a factory on the model.

Not decided

Which of the three collision options to take, and whether case 1's one-line fix ships ahead of the rest.

Lingua principale
Dart
Stelle
64
Fork
3
Merge medio
17h 32m
PR unite (30g)
33

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di btwld/ack

Tutte le issue di btwld/ack

Issue simili

Altre issue su Dart

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.