dart-lang / mockito

Mockito-inspired mock library for Dart
https://pub.dev/packages/mockito
Apache License 2.0
627 stars 161 forks source link

Version 5.3.1 and greater for Dart is causing type 'SimpleIdentifierImpl' is not a subtype of type 'ListLiteral?' in type cast #577

Open ricardoamador opened 1 year ago

ricardoamador commented 1 year ago

When we updated to version 5.3.2 we began seeing the following error in our builds:

Building package executable... (3.1s)
Built build_runner:build_runner.
[INFO] Generating build script completed, took 204ms
[INFO] Precompiling build script... completed, took 2.9s
[INFO] Building new asset graph completed, took 376ms
[INFO] Checking for unexpected pre-existing outputs. completed, took 2ms
[INFO] Generating SDK summary completed, took 2.3s
[WARNING] json_serializable on lib/src/model/google/token_info.dart:
The version constraint "^4.6.0" on json_annotation allows versions before 4.7.0 which is not allowed.
[SEVERE] mockito:mockBuilder on test/src/utilities/mocks.dart:

type 'SimpleIdentifierImpl' is not a subtype of type 'ListLiteral?' in type cast
[INFO] Running build completed, took 11.1s
[INFO] Caching finalized dependency graph completed, took 123ms
[SEVERE] Failed after 11.2s

If we use version 5.3.0 we do not see these errors.

I am creating mocks using the following command: dart --verbose run build_runner build --delete-conflicting-outputs

srawlins commented 1 year ago

Do you get more output if you put the --verbose after run build_runner build?

ricardoamador commented 1 year ago

Yeah here is the verbose output. Sorry about that:

[SEVERE] mockito:mockBuilder on test/src/utilities/mocks.dart (cached):

type 'SimpleIdentifierImpl' is not a subtype of type 'ListLiteral?' in type cast
package:mockito/src/builder.dart 475:24  _MockTargetGatherer._customMocksAst
package:mockito/src/builder.dart 520:11  _MockTargetGatherer._mockTargetsFromGenerateMocks
package:mockito/src/builder.dart 447:25  new _MockTargetGatherer
package:mockito/src/builder.dart 73:9    MockBuilder.build

[SEVERE] Build:
Failed after 164ms
[+3202 ms] "flutter run" took 3,431ms.
[   +5 ms] pub finished with exit code 1
[   +1 ms] 
           #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
           #1      _DefaultPub.interactively (package:flutter_tools/src/dart/pub.dart:589:7)
           <asynchronous suspension>
           #2      PackagesForwardCommand.runCommand (package:flutter_tools/src/commands/packages.dart:250:5)
           <asynchronous suspension>
           #3      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1244:27)
           <asynchronous suspension>
           #4      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #5      CommandRunner.runCommand (package:args/command_runner.dart:209:13)
           <asynchronous suspension>
           #6      FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:283:9)
           <asynchronous suspension>
           #7      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #8      FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:229:5)
           <asynchronous suspension>
           #9      run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:64:9)
           <asynchronous suspension>
           #10     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #11     main (package:flutter_tools/executable.dart:91:3)
           <asynchronous suspension>
srawlins commented 1 year ago

Ah nice stack trace. It's a bug in mockito, but your code probably does not analyze clean? It looks like you have something like GenerateMocks([], customMocks: xxx) rather than GenerateMocks([], customMocks: [...]).

srawlins commented 1 year ago

Mockito should fail nicely on bad code, rather than crash.

ricardoamador commented 1 year ago

We use the following:

@GenerateMocks(
  <Type>[
    ...
  ],
  customMocks: _mocks,
)

...

const List<MockSpec<dynamic>> _mocks = <MockSpec<dynamic>>[
  MockSpec<Cache<Uint8List>>(),
  MockSpec<GitHub>(
    fallbackGenerators: <Symbol, Function>{
      #postJSON: postJsonShim,
    },
  ),
  // MockSpec<GerritService>(fallbackGenerators: <Symbol, Function>{
  //   #authClientProvider: authClientProviderShim,
  // }),
];

Which looks to be GenerateMocks([], customMocks: [...])

srawlins commented 1 year ago

Ah I see. We may not be able to support that. Will have to look.

ricardoamador commented 1 year ago

Sure let me know. Also let me know if we are doing something wrong here as well.

srawlins commented 1 year ago

CC @yanok. I think this is caused by https://github.com/dart-lang/mockito/commit/d51cd8d9dda44e407fede1fd9d51088fb95c2775 (cl/467867637)

yanok commented 1 year ago

Yep, the mockspecs must be inlined. (That's because I have to inspect the AST before constant evaluation and I don't want to follow definitions in the AST).

But we surely need to complain about this instead of crashing.

yanok commented 1 year ago

@ricardoamador the fix would be to inline _mocks into the @GenerateMocks annotation.

ricardoamador commented 1 year ago

@yanok @srawlins that looks to be the fix. I validated with 5.3.2 and inlined the _mocks function call and looks good. Thank you for the help on this!

rhalff commented 1 year ago

I can confirm inlining the custom mocks solved the problem for me also, perhaps mentioning this in the exception would be an easy fix?