Before get in the problem, I'd like to say that is my first time trying to a generator on Dart, say that, lets go little deeper.

Well, I was trying to follow this example: https://github.com/dart-lang/source_gen/tree/master/example Changing according what I need, by the way, I didn't got to run even the clone of repository, I'm getting an error related with LibraryDirectiveImpl, I don't know how to debug it

May this is happen because I'm using flutter pub run build_runner build instead of pub run build_runner build, actually I'd like to run this when I'm in Flutter projects.

When I run the build_runner, I got this problem on my console

You have hit a bug in build_runner
Please file an issue with reproduction steps at https://github.com/dart-lang/build/issues

type 'LibraryDirectiveImpl' is not a subtype of type 'Statement'
package:analyzer/src/summary2/ast_binary_reader.dart 1086:7       AstBinaryReader._read_labeledStatement
package:analyzer/src/summary2/ast_binary_reader.dart 1801:16      AstBinaryReader._readNode
package:analyzer/src/summary2/ast_binary_reader.dart 1914:19      AstBinaryReader._readNodeList
package:analyzer/src/summary2/ast_binary_reader.dart 357:21       AstBinaryReader._read_compilationUnit
package:analyzer/src/summary2/ast_binary_reader.dart 1699:16      AstBinaryReader._readNode
package:analyzer/src/summary2/ast_binary_reader.dart 59:14        AstBinaryReader.readNode
package:analyzer/src/summary2/linked_unit_context.dart 1040:26    LinkedUnitContext._ensureUnitWithDeclarations
package:analyzer/src/summary2/linked_unit_context.dart 91:5       LinkedUnitContext.unit_withDeclarations
package:analyzer/src/summary2/linked_element_factory.dart 307:27  _ElementRequest._createLibraryElement
package:analyzer/src/summary2/linked_element_factory.dart 147:14  _ElementRequest.elementOfReference
package:analyzer/src/summary2/linked_element_factory.dart 72:45   LinkedElementFactory.elementOfReference
package:analyzer/src/summary2/linked_element_factory.dart 104:12  LinkedElementFactory.libraryOfUri
package:analyzer/src/summary2/link.dart 213:34                    Linker._createTypeSystem
package:analyzer/src/summary2/link.dart 98:5                      Linker._buildOutlines
package:analyzer/src/summary2/link.dart 83:5                      Linker.link
package:analyzer/src/summary2/link.dart 35:10                     link
package:analyzer/src/dart/analysis/library_context.dart 318:26    LibraryContext.load2.loadBundle
dart:collection                                                   __CompactLinkedHashSet&_HashFieldBase&_HashBase&_OperatorEqualsAndHashCode&SetMixin.forEach
package:analyzer/src/dart/analysis/library_context.dart 273:32    LibraryContext.load2.loadBundle
package:analyzer/src/dart/analysis/library_context.dart 370:17    LibraryContext.load2.<fn>
package:analyzer/src/dart/analysis/performance_logger.dart 34:15  PerformanceLog.run
package:analyzer/src/dart/analysis/library_context.dart 368:12    LibraryContext.load2
package:analyzer/src/dart/analysis/library_context.dart 95:7      new LibraryContext
package:analyzer/src/dart/analysis/driver.dart 1439:29            AnalysisDriver._createLibraryContext
package:analyzer/src/dart/analysis/driver.dart 1380:28            AnalysisDriver._computeUnitElement.<fn>
package:analyzer/src/dart/analysis/performance_logger.dart 34:15  PerformanceLog.run
package:analyzer/src/dart/analysis/driver.dart 1378:20            AnalysisDriver._computeUnitElement
package:analyzer/src/dart/analysis/driver.dart 997:34             AnalysisDriver.performWork
package:analyzer/src/dart/analysis/driver.dart 1931:24            AnalysisDriverScheduler._run
package:analyzer/src/dart/analysis/driver.dart 1865:5             AnalysisDriverScheduler.start
package:build_resolvers/src/analysis_driver.dart 54:13            analysisDriver
package:build_resolvers/src/resolver.dart 138:18                  new AnalyzerResolvers
package:build_runner_core/src/generate/options.dart 193:19        BuildOptions.create
package:build_runner/src/generate/build.dart 85:36                build
package:build_runner/src/entrypoint/build.dart 28:24              BuildCommand.run
package:args/command_runner.dart 197:27                           CommandRunner.runCommand
package:args/command_runner.dart 112:25                           CommandRunner.run.<fn>
dart:async                                                        new Future.sync
package:args/command_runner.dart 112:14                           CommandRunner.run
package:build_runner/src/entrypoint/run.dart 24:31                run
.dart_tool\build\entrypoint\build.dart 31:22                      main

pub failed (1)

Flutter doctor

[√] Flutter (Channel stable, v1.7.8+hotfix.4, on Microsoft Windows [Version 10.0.17763.737], locale en-US)
    • Flutter version 1.7.8+hotfix.4 at C:\Flutter
    • Framework revision 20e59316b8 (9 weeks ago), 2019-07-18 20:04:33 -0700
    • Engine revision fee001c93f
    • Dart version 2.4.0

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at c:\Users\hj_ju\AppData\Local\Android\Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = c:\Users\hj_ju\AppData\Local\Android\Sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.

[√] Android Studio (version 3.5)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 39.0.3
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[!] IntelliJ IDEA Ultimate Edition (version 2019.1)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.dev/intellij-setup/#installing-the-plugins

[√] VS Code (version 1.38.1)
    • VS Code at C:\Users\hj_ju\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.4.1

[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.


Solution 1: luckybilly

I got the same error message when i use json_serializable today.

finally, i found a solution (downgrade the package 'analyzer' version from 0.38.3 to 0.38.2) from here, it works.

dependencies:
  //analyzer: ^0.38.2
  analyzer: 0.38.2

good luck to you!