google / reflectable.dart

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques. The use of dynamic reflection is constrained in order to ensure that the specialized code can be generated and will have a reasonable size.
https://pub.dev/packages/reflectable
BSD 3-Clause "New" or "Revised" License
383 stars 57 forks source link

[reflectable 2.2.8] doesn't create main.reflectable #227

Closed aliyazdi75 closed 3 years ago

aliyazdi75 commented 4 years ago

After upgrading my flutter, reflectable doesn't create main.reflectable.dart. I don't know what causes this problem but I share my logs.

flutter packages pub run build_runner build: ``` [INFO] Generating build script... [INFO] Generating build script completed, took 484ms [INFO] Initializing inputs [INFO] Reading cached asset graph... [INFO] Reading cached asset graph completed, took 99ms [INFO] Checking for updates since last build... [INFO] Checking for updates since last build completed, took 1.2s [INFO] Running build... [WARNING] reflectable:reflectable on test/widget_test.dart: Your current `analyzer` version may not fully support your current SDK version. Please try upgrading to the latest `analyzer` by running `flutter packages upgrade`. Analyzer language version: 2.11.0 SDK language version: 2.12.0 If you are getting this message and have the latest analyzer please file an issue at https://github.com/dart-lang/sdk/issues/new with the title "No published analyzer available for language version 2.12.0". Please search the issue tracker first and thumbs up and/or subscribe to existing issues if present to avoid duplicates. [INFO] 1.4s elapsed, 0/1 actions completed. [INFO] 2.6s elapsed, 0/1 actions completed. [INFO] 3.7s elapsed, 0/1 actions completed. [INFO] 4.8s elapsed, 0/1 actions completed. [INFO] 10.4s elapsed, 0/1 actions completed. [INFO] 21.2s elapsed, 0/1 actions completed. [WARNING] No actions completed for 21.2s, waiting on: - reflectable:reflectable on test/widget_test.dart [INFO] 22.3s elapsed, 0/1 actions completed. [INFO] 23.3s elapsed, 0/1 actions completed. [INFO] 24.3s elapsed, 0/1 actions completed. [INFO] 25.4s elapsed, 0/1 actions completed. [INFO] 26.4s elapsed, 0/1 actions completed. [INFO] 27.5s elapsed, 0/1 actions completed. [INFO] 28.5s elapsed, 0/1 actions completed. [INFO] 29.6s elapsed, 0/1 actions completed. [INFO] Running build completed, took 30.4s [INFO] Caching finalized dependency graph... [INFO] Caching finalized dependency graph completed, took 55ms [INFO] Succeeded after 30.5s with 1 outputs (1 actions) ```
flutter doctor: ``` [✓] Flutter (Channel master, 1.24.0-8.0.pre.117, on Linux, locale en_US.UTF-8) • Flutter version 1.24.0-8.0.pre.117 at /home/aliyazdi75/snap/flutter/common/flutter • Framework revision 8cb2665118 (32 hours ago), 2020-11-06 16:02:19 +0800 • Engine revision 0693ee04d1 • Dart version 2.12.0 (build 2.12.0-21.0.dev) [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3) • Android SDK at /home/aliyazdi75/Android/Sdk • Platform android-30, build-tools 29.0.3 • Java binary at: /home/aliyazdi75/Application/android-studio/jre/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) • All Android licenses accepted. [✓] Chrome - develop for the web • Chrome at google-chrome [✓] Linux toolchain - develop for Linux desktop • clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) • cmake version 3.10.2 • ninja version 1.8.2 • pkg-config version 0.29.1 [✓] Android Studio • Android Studio at /home/aliyazdi75/Application/android-studio • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = /home/aliyazdi75/Application/android-studio • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) [✓] Connected device (4 available) • sdk gphone x86 (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator) • Linux (desktop) • linux • linux-x64 • Linux • Web Server (web) • web-server • web-javascript • Flutter Tools • Chrome (web) • chrome • web-javascript • Google Chrome 86.0.4240.183 • No issues found! ```

Mention that widget_test.reflectable.dart has been successfully created,

aliyazdi75 commented 3 years ago

It seems all generated codes are inside widget_test.reflectable.dart which located in the test folder.

eernstg commented 3 years ago

The build.yaml in reflectable.dart has

builders:
  reflectable:
    ...
    defaults:
      generate_for:
        exclude:
        - lib/**.dart
        include:
        ...
        - lib/main.dart
        ...

which should enable code generation for lib/main.dart, yielding lib/main.reflectable.dart. Did you specify your own build.yaml with different settings? If so then it should be enough to include the above setting for lib/main.dart.

aliyazdi75 commented 3 years ago

No, I didn't specify build.yaml. I found this code in .dart_tool/build/entrypoint:

  _i1.apply('reflectable:reflectable', [_i6.reflectableBuilder], _i1.toRoot(),
      hideOutput: false,
      defaultGenerateFor: const _i3.InputSet(include: [
        'benchmark/**.dart',
        'bin/**.dart',
        'example/**.dart',
        'lib/main.dart',
        'test/**.dart',
        'tool/**.dart',
        'web/**.dart'
      ], exclude: [
        'lib/**.dart'
      ])),
eernstg commented 3 years ago

Looks like the generation of .dart_tool/build/entrypoint/build.dart uses the expected configuration, and it does include 'lib/main.dart'. So are you talking about a main.dart which is not lib/main.dart?

aliyazdi75 commented 3 years ago

No my main.dart is in lib folder. As I said before I didn't have a problem before some previous versions of Flutter and it was created in lib folder. I don't know what it caused this problem, since there is no failure logs.

eernstg commented 3 years ago

I can't see a reason at this point. Do you get any further information with --verbose on the invocation of build_runner?

aliyazdi75 commented 3 years ago
[ +164 ms] executing: [/home/aliyazdi75/snap/flutter/common/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[  +51 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[   +1 ms] f04b3d917f1f8c913fc019fa26f9ac961cab155b
[        ] executing: [/home/aliyazdi75/snap/flutter/common/flutter/] git tag --points-at HEAD
[  +17 ms] Exit code 0 from: git tag --points-at HEAD
[   +2 ms] executing: [/home/aliyazdi75/snap/flutter/common/flutter/] git describe --match *.*.* --long --tags
[  +44 ms] Exit code 0 from: git describe --match *.*.* --long --tags
[        ] 1.24.0-7.0.pre-147-gf04b3d917f
[  +13 ms] executing: [/home/aliyazdi75/snap/flutter/common/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[   +5 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/master
[        ] executing: [/home/aliyazdi75/snap/flutter/common/flutter/] git ls-remote --get-url origin
[   +8 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[  +82 ms] executing: [/home/aliyazdi75/snap/flutter/common/flutter/] git rev-parse --abbrev-ref HEAD
[   +6 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] master
[  +83 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[   +3 ms] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[   +5 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[  +17 ms] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +172 ms] Artifact Instance of 'MaterialFonts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'GradleWrapper' is not required, skipping update.
[        ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[        ] Artifact Instance of 'FlutterSdk' is not required, skipping update.
[        ] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[   +7 ms] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'FontSubsetArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'PubDependencies' is not required, skipping update.
[  +46 ms] Using /home/aliyazdi75/snap/flutter/common/flutter/.pub-cache for the pub cache.
[        ] executing: /home/aliyazdi75/snap/flutter/common/flutter/bin/cache/dart-sdk/bin/pub run build_runner build --delete-conflicting-outputs --verbose
[INFO] Generating build script...
[INFO] Generating build script completed, took 538ms

[INFO] BuildDefinition:Initializing inputs
[INFO] BuildDefinition:Reading cached asset graph...
[INFO] BuildDefinition:Reading cached asset graph completed, took 126ms

[INFO] BuildDefinition:Checking for updates since last build...
[INFO] BuildDefinition:Checking for updates since last build completed, took 1.8s

[INFO] Build:Running build...
[WARNING] reflectable:reflectable on test/widget_test.dart:
Your current `analyzer` version may not fully support your current SDK version.

Please try upgrading to the latest `analyzer` by running `flutter packages upgrade`.

Analyzer language version: 2.11.0
SDK language version: 2.12.0

If you are getting this message and have the latest analyzer please file
an issue at https://github.com/dart-lang/sdk/issues/new with the title
"No published analyzer available for language version 2.12.0".
Please search the issue tracker first and thumbs up and/or subscribe to
existing issues if present to avoid duplicates.

[INFO] Heartbeat:1.6s elapsed, 0/1 actions completed.
[INFO] Heartbeat:2.8s elapsed, 0/1 actions completed.
[INFO] Heartbeat:3.8s elapsed, 0/1 actions completed.
[INFO] Heartbeat:4.8s elapsed, 0/1 actions completed.
[INFO] Heartbeat:5.8s elapsed, 0/1 actions completed.
[INFO] Heartbeat:6.9s elapsed, 0/1 actions completed.
[INFO] Heartbeat:14.7s elapsed, 0/1 actions completed.
[INFO] Heartbeat:38.6s elapsed, 0/1 actions completed.
[WARNING] Heartbeat:
No actions completed for 38.6s, waiting on:
  - reflectable:reflectable on test/widget_test.dart

[INFO] Heartbeat:39.7s elapsed, 0/1 actions completed.
[INFO] Heartbeat:40.8s elapsed, 0/1 actions completed.
[INFO] Heartbeat:41.9s elapsed, 0/1 actions completed.
[INFO] Heartbeat:43.0s elapsed, 0/1 actions completed.
[INFO] Heartbeat:44.0s elapsed, 0/1 actions completed.
[INFO] Heartbeat:45.1s elapsed, 0/1 actions completed.
[INFO] Heartbeat:46.2s elapsed, 0/1 actions completed.
[INFO] Heartbeat:47.2s elapsed, 0/1 actions completed.
[INFO] Build:Running build completed, took 47.3s

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

[INFO] Build:Succeeded after 47.4s with 1 outputs (1 actions)

[+53335 ms] "flutter pub" took 53,613ms.
[ +262 ms] ensureAnalyticsSent: 251ms
[   +5 ms] Running shutdown hooks
[        ] Shutdown hooks complete
[        ] exiting with code 0
starteNCS commented 3 years ago

I do encounter this issue as well. Did you find any way to resolve this @aliyazdi75 ?

aliyazdi75 commented 3 years ago

@starteNCS If you have widget_test.reflectable.dart in your test folder, copy that file and use it as main.reflectable.dart.

eernstg commented 3 years ago

If you have widget_test.reflectable.dart in your test folder, copy that file and use it as main.reflectable.dart.

That should work if widget_test.dart doesn't use any relative imports (otherwise an import may be specified as relative in widget_test.reflectable.dart, and then it won't work if moved from test to lib), and ifwidget_test.dartimports the same set of libraries aslib/main.dart(this is concerned with directly as well as indirectly imported libraries, i.e., it's the transitive closure of imports fromlib/main.dart`).

If they import a different set of libraries then the generated reflection data may be different, and that could create various issues: The generated library might import additional libraries that shouldn't be imported in the program specified by lib/main.dart; or the generated code might fail to support reflection on certain types, because those types are not imported by widget_test.dart.

Clearly, it would be better to solve the actual problem. However, I still haven't seen anything that documents how this problem arises, and I haven't seen the problem locally. Do you have a snippet (or a repo) of code which demonstrates the problem and is available to the public?

aliyazdi75 commented 3 years ago

I'm using dart_json_mapper and here's the simple code that demonstrates this issue: pubspec.yml:

dependencies:
  dart_json_mapper: ^1.7.2
  reflectable: ^2.2.8
  flutter:
    sdk: flutter

dev_dependencies:
  build_runner: any
  flutter_test:
    sdk: flutter

user.dart:

import 'package:dart_json_mapper/dart_json_mapper.dart';

@jsonSerializable
class User {
  User({
    this.firstName,
    this.lastName,
    this.fullName,
    this.cityId,
  });

  String firstName;
  String lastName;
  String fullName;
  int cityId;
}

main.dart:

import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'main.reflectable.dart';

void main() {
  initializeReflectable();
  JsonMapper().useAdapter(
    JsonMapperAdapter(
      valueDecorators: {},
    ),
  );
  runApp(MyApp());
}

widget_test.dart:

// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:flutter_app/main.dart';

void main() {
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(MyApp());

    // Verify that our counter starts at 0.
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);

    // Tap the '+' icon and trigger a frame.
    await tester.tap(find.byIcon(Icons.add));
    await tester.pump();

    // Verify that our counter has incremented.
    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
  });
}

After running flutter packages pub run build_runner build: widget_test.reflectable.dart:

// This file has been generated by the reflectable package.
// https://github.com/dart-lang/reflectable.

import 'dart:core';
import 'package:dart_json_mapper/src/model/annotations.dart' as prefix0;

// ignore_for_file: prefer_adjacent_string_concatenation
// ignore_for_file: prefer_collection_literals
// ignore_for_file: unnecessary_const
// ignore_for_file: implementation_imports

// ignore:unused_import
import 'package:reflectable/mirrors.dart' as m;
// ignore:unused_import
import 'package:reflectable/src/reflectable_builder_based.dart' as r;
// ignore:unused_import
import 'package:reflectable/reflectable.dart' as r show Reflectable;

final _data = <r.Reflectable, r.ReflectorData>{
  const prefix0.JsonSerializable(): r.ReflectorData(
      <m.TypeMirror>[],
      <m.DeclarationMirror>[],
      <m.ParameterMirror>[],
      <Type>[],
      0,
      {},
      {},
      null,
      [])
};

final _memberSymbolMap = null;

void initializeReflectable() {
  r.data = _data;
  r.memberSymbolMap = _memberSymbolMap;
}
eernstg commented 3 years ago

Thanks! I can see that the reflectable code generator is never invoked on lib/main.dart (and there are no error messages indicating that there is anything wrong with that library), so there is something in the configuration of the build_runner that makes it skip that library.

eernstg commented 3 years ago

Cf. https://github.com/dart-lang/build/issues/2924.

eernstg commented 3 years ago

Landed #228 which fixes this issue (and a few other things), published as reflectable 2.2.9.

aliyazdi75 commented 3 years ago

@eernstg Thank you so much.