9

Running build_runner to generate mocks using the Mockito package fails.

Terminal output:

[INFO] Generating build script... [INFO] Generating build script completed, took 2.2s

[INFO] Precompiling build script...... [WARNING] /D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/mockito-5.3.0/lib/src/builder.dart:412:19: Error: The getter 'libraryExports' isn't defined for the class 'LibraryElement'.

  • 'LibraryElement' is from 'package:analyzer/dart/element/element.dart' ('/D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'libraryExports'. ...entryLib.libraryExports, ^^^^^^^^^^^^^^ /D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/mockito-5.3.0/lib/src/builder.dart:413:19: Error: The getter 'libraryImports' isn't defined for the class 'LibraryElement'.
  • 'LibraryElement' is from 'package:analyzer/dart/element/element.dart' ('/D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'libraryImports'. ...entryLib.libraryImports, ^^^^^^^^^^^^^^ /D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/mockito-5.3.0/lib/src/builder.dart:1493:36: Error: The getter 'enclosingElement2' isn't defined for the class 'ParameterElement'.
  • 'ParameterElement' is from 'package:analyzer/dart/element/element.dart' ('/D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'enclosingElement2'. final method = parameter.enclosingElement2!; ^^^^^^^^^^^^^^^^^ /D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/mockito-5.3.0/lib/src/builder.dart:1524:30: Error: The getter 'enclosingElement2' isn't defined for the class 'ParameterElement'.
  • 'ParameterElement' is from 'package:analyzer/dart/element/element.dart' ('/D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'enclosingElement2'. final method = parameter.enclosingElement2 as MethodElement; ^^^^^^^^^^^^^^^^^ /D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/mockito-5.3.0/lib/src/builder.dart:1525:27: Error: The getter 'enclosingElement2' isn't defined for the class 'MethodElement'.
  • 'MethodElement' is from 'package:analyzer/dart/element/element.dart' ('/D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'enclosingElement2'. final class_ = method.enclosingElement2 as ClassElement; ^^^^^^^^^^^^^^^^^ /D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/mockito-5.3.0/lib/src/builder.dart:1535:28: Error: The getter 'enclosingElement2' isn't defined for the class 'ExecutableElement'.
  • 'ExecutableElement' is from 'package:analyzer/dart/element/element.dart' ('/D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'enclosingElement2'. overriddenMethod.enclosingElement2 as ClassElement, name); ^^^^^^^^^^^^^^^^^ /D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/mockito-5.3.0/lib/src/builder.dart:1934:23: Error: The getter 'enclosingElement2' isn't defined for the class 'Element'.
  • 'Element' is from 'package:analyzer/dart/element/element.dart' ('/D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'enclosingElement2'. var className = enclosingElement2!.name; ^^^^^^^^^^^^^^^^^ /D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/mockito-5.3.0/lib/src/builder.dart:1937:23: Error: The getter 'enclosingElement2' isn't defined for the class 'Element'.
  • 'Element' is from 'package:analyzer/dart/element/element.dart' ('/D:/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'enclosingElement2'. var className = enclosingElement2!.name; ^^^^^^^^^^^^^^^^^ [INFO] Precompiling build script... completed, took 22.4s

[SEVERE] Failed to precompile build script .dart_tool/build/entrypoint/build.dart. This is likely caused by a misconfigured builder definition.

I have tried overriding the analyzer dependency to 1.5.0 but it causes conflicts with other dependencies in the project.

Running flutter doctor shows the same errors in the terminal.

3
  • 1
    Got same error 2 hours after you posted this. Commented Aug 5, 2022 at 15:59
  • have you solved this? Both answers do not work for me. Commented Aug 9, 2022 at 10:30
  • I set mockito:5.2.0 analyzer: Started working fine after doing this. Commented Aug 9, 2022 at 12:22

2 Answers 2

9

This is related to upgrading to mockito v5.3.0, dart pub upgrade might solve it. if not, revert mockito version to 5.2.0 and it should work.

mockito: 5.2.0 without the ^

Sign up to request clarification or add additional context in comments.

5 Comments

both does not work for me. Also overriding code_builder: ^4.2.0 as recommended here does not work for me. github.com/dart-lang/mockito/issues/552#issuecomment-1208973838
Make sure you write 5.2.0 without ^
And try doing flutter clean if still not fixed
writing mockito: 5.2.0 without the ^did the trick, thanks! :-) Maybe you could include that explicitly into your answer.
With mockito 5.3.2 the problem was solved for me.
4

The issue is caused by analyzer package, that it's used as a dependency for mockito.

Seems like the new version 4.4.0 deprecated a few APIs.

What I did to fix it for now (until maintainers fix it on the package) is to include analyzer as a dependency on my pubspec.yaml with latest stable version 4.3.1 and it works now.

dev_dependencies:
  analyzer: 4.3.1

Hope this helps

1 Comment

Now we need to fix it to analyzer: 7.3.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.