Json_serializable.dart: Json_serializable is not generating *.g.dart files

Created on 26 Apr 2020  路  24Comments  路  Source: google/json_serializable.dart

Given this class:

import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:json_annotation/json_annotation.dart';

part 'entities.g.dart';

@JsonSerializable(nullable: false)
class User {
  final String uid;
  final String email;
  final String displayName;
  final String profilePicture;
  final String accountType;

  User(
      {@required this.uid,
      @required this.email,
      @required this.displayName,
      @required this.profilePicture,
      this.accountType});

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);

  Map<String, dynamic> toJson() => _$UserToJson(this);
}

image
(Image so you can see what my IDE is highlighting)

This is inside a dart file called entities.dart
image

This is my pubspec.yaml

name: <...>
description: <...>

version: 1.0.0+1

environment:
  sdk: ">=2.6.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  google_sign_in: ^4.4.3
  firebase_auth: ^0.16.0
  firebase_core: ^0.4.4+3
  cloud_firestore: ^0.13.5
  google_fonts: ^1.0.0
  json_annotation: ^3.0.1
  provider: ^4.0.5
  url_launcher: ^5.4.5
  image_picker: ^0.6.5+2
  image_picker_web: ^1.0.6
  string_validator: ^0.1.4
  intl_phone_number_input: ^0.3.0
  currency_pickers: ^1.0.6
  i18n_extension: ^1.3.5
  photo_view: ^0.9.2

dev_dependencies:
  build_runner: ^1.9.0
  json_serializable: ^3.3.0
  flutter_test:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

flutter:
  uses-material-design: true
  assets:
    - assets/

I am running the following command:
flutter packages pub run build_runner build

And this is the console output:

[INFO] Generating build script...
[INFO] Generating build script completed, took 384ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 75ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 741ms

[INFO] Running build...
[INFO] Running build completed, took 18ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 51ms

[INFO] Succeeded after 88ms with 0 outputs (0 actions)

I've tried many variations of the class, it simply wont generate anything.

I've tried a non-optional constructor, removing @required, changing imports, renaming the dart file, invalidating caches and restarting several times, renaming the class, etc...

Most helpful comment

Try on line 6

part 'User.g.dart';

and rename the file to User.dart.

user.dart 鈥撀燼lways use lower case file names!

All 24 comments

Try on line 6

part 'User.g.dart';

and rename the file to User.dart.

Try on line 6

part 'User.g.dart';

and rename the file to User.dart.

user.dart 鈥撀燼lways use lower case file names!

Where is this file located in your package?

@kevmoo
image

Try deleting .dart_tool/build first then running build again...

Can you look at the .dart_tool/build/entrypoint/build.dart and see if there is an .apply call for the json serializable and source_gen builders?

@natebosch
This is my .dart_tool/build/entrypoint/build.dart

// ignore_for_file: directives_ordering

import 'package:build_runner_core/build_runner_core.dart' as _i1;
import 'package:json_serializable/builder.dart' as _i2;
import 'package:source_gen/builder.dart' as _i3;
import 'package:build_config/build_config.dart' as _i4;
import 'dart:isolate' as _i5;
import 'package:build_runner/build_runner.dart' as _i6;
import 'dart:io' as _i7;

final _builders = <_i1.BuilderApplication>[
  _i1.apply('json_serializable:json_serializable', [_i2.jsonSerializable],
      _i1.toDependentsOf('json_serializable'),
      hideOutput: true, appliesBuilders: ['source_gen:combining_builder']),
  _i1.apply('source_gen:combining_builder', [_i3.combiningBuilder],
      _i1.toNoneByDefault(),
      hideOutput: false, appliesBuilders: ['source_gen:part_cleanup']),
  _i1.applyPostProcess('source_gen:part_cleanup', _i3.partCleanup,
      defaultGenerateFor: const _i4.InputSet())
];
void main(List<String> args, [_i5.SendPort sendPort]) async {
  var result = await _i6.run(args, _builders);
  sendPort?.send(result);
  _i7.exitCode = result;
}

@kevmoo
Deleted .dart_tool/build Rerun the build method, it did not fix the issue.

image

[INFO] Generating build script...
[INFO] Generating build script completed, took 443ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 86ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 861ms

[INFO] Running build...
[INFO] Running build completed, took 19ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 64ms

[INFO] Succeeded after 93ms with 0 outputs (0 actions)

What is your project layout? Is api/entities.dart under lib/ somewhere? It should be...

@natebosch https://github.com/dart-lang/json_serializable/issues/636#issuecomment-620789621

I answered this already above. lib/api/entities.

Deleted .dart_tool/build Rerun the build method, it did not fix the issue.

The output you pasted isn't from the attempt immediately after deleting this directory. Could you show us that output?

@natebosch What? It was the attempt directly after...
Step by step: i deleted the /build folder, I showed you a screenshot of the directory with /build missing, then I ran the same command (flutter packages pub run build_runner build) and copied the output from my console and pasted it here.
I even invalidated caches in intellij and tried again with the exact same output.

Again, here is a screenshot of .dart_tool in my project folder as well as the entire project structure (blurred project name):
image

And here is the output that you see in the screenshot:

[INFO] Generating build script...
[INFO] Generating build script completed, took 405ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 81ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 766ms

[INFO] Running build...
[INFO] Running build completed, took 19ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 62ms

[INFO] Succeeded after 91ms with 0 outputs (0 actions)

Can you try running flutter packages pub run build_runner clean? The build above was a cached build.

Then if you can flutter packages pub run build_runner build --verbose that might help although it will be... noisy ;).

@jakemac53

<...>flutter packages pub run build_runner clean
[WARNING] Deleting cache and generated source files.
This shouldn't be necessary for most applications, unless you have made intentional edits to generated files (i.e. for testing). Consider filing a bug at https://github.com/dart-lang/build/issues/new if you are using this to work around an apparent (and reproducibl
e) bug.
[INFO] Cleaning up source outputs...
[INFO] Cleaning up source outputs completed, took 158ms

[INFO] Cleaning up cache directory...
[INFO] Cleaning up cache directory completed, took 6ms


<...>flutter packages pub run build_runner build --verbose
[INFO] Generating build script...
[INFO] Generating build script completed, took 395ms

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 12.1s

[INFO] BuildDefinition:Initializing inputs
[INFO] BuildDefinition:Building new asset graph...
[INFO] BuildDefinition:Building new asset graph completed, took 711ms

[INFO] BuildDefinition:Checking for unexpected pre-existing outputs....
[INFO] BuildDefinition:Checking for unexpected pre-existing outputs. completed, took 1ms

[INFO] Build:Running build...
[INFO] Heartbeat:1.1s elapsed, 0/7 actions completed.
[INFO] Heartbeat:2.2s elapsed, 0/7 actions completed.
[INFO] Heartbeat:3.3s elapsed, 0/7 actions completed.
[INFO] Heartbeat:6.2s elapsed, 0/7 actions completed.
[INFO] Heartbeat:7.2s elapsed, 0/7 actions completed.
[FINE] json_serializable:json_serializable on lib/api/entities.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/api/entities.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/theme_mode_notifier.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/theme_mode_notifier.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/api/firebase.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/api/firebase.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/generated_plugin_registrant.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/generated_plugin_registrant.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/main.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/main.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/user_entry_page.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/user_entry_page.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/widget_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/widget_test.dart:Running JsonLiteralGenerator - 2 of 2
[INFO] Build:Running build completed, took 13.5s

[INFO] Build:Caching finalized dependency graph...
[INFO] Build:Caching finalized dependency graph completed, took 38ms

[INFO] Build:Succeeded after 13.6s with 2 outputs (15 actions)

No difference:
image

Ok - so this time it did at least run on the file as you can see in the logs - and there was some output (2 files).

If you look under .dart_tool/build/generated/<your-package>/lib/api/ do you see any files? Or do you not have any .dart_tool/build dir at all somehow?

@jakemac53 as you can see in the screenshot, .dart_tool/build does not exist

Here's the file explorer screenshot as proof that it is not my IDE screwing it up
image

Are you sure that the file explorer doesn't need to be refreshed? I don't know about intellij but in vscode sometimes you have to do that.

Oh nvm I see this last screenshot is from a different file explorer.

That is extremely confusing because .dart_tool/build is where everything the build system creates is put. So, I have no idea where it could be if it is not there. Even the generated build script itself is there.

what path does flutter packages pub run build_runner generate-build-script print out? Does it match your current package directory?

And to confirm after you run that command the file does or does not exist?

Hmm, I think it's a different path. I will debug and reply later with my findings. I'll be a while, not going to do it now

Ok, what it sounds like to me is the the current working directory (at least as reported to build_runner by the Dart vm), is not the root of the package you think you are running from. It is building some different package somewhere.

Did you try to have matching class name, filename and part name?
Some people reported success on stack exchange and I faced the same problem and doing so solved it.

User.dart

part 'User.g.dart';

@JsonSerializable(nullable: false)
class User {
  final String uid;
  final String email;
  final String displayName;
  final String profilePicture;
  final String accountType;

  User(
      {@required this.uid,
      @required this.email,
      @required this.displayName,
      @required this.profilePicture,
      this.accountType});

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);

  Map<String, dynamic> toJson() => _$UserToJson(this);
}

It seems intellij's built in terminal was using a different directory than the current one. So that was the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

broofus picture broofus  路  6Comments

ollydixon picture ollydixon  路  6Comments

AlexBigCheese picture AlexBigCheese  路  3Comments

4BooM04 picture 4BooM04  路  3Comments

HosseinArabbeigi picture HosseinArabbeigi  路  3Comments