dart-lang / pub

The pub command line tool
https://dart.dev/tools/pub/cmd
BSD 3-Clause "New" or "Revised" License
1.04k stars 226 forks source link

Error when running build runner "Null check operator used on a null value" #3314

Open balenaultra opened 2 years ago

balenaultra commented 2 years ago

After Flutter upgrade and "flutter pub run build_runner build --delete-conflicting-outputs"

Null check operator used on a null value
package:pub/src/package_graph.dart 70:53                         PackageGraph.transitiveDependencies.<fn>.<fn>
dart:core                                                        Iterable.toSet
package:pub/src/package_graph.dart 70:56                         PackageGraph.transitiveDependencies.<fn>
package:pub/src/utils.dart 622:41                                mapMap
package:pub/src/package_graph.dart 68:11                         PackageGraph.transitiveDependencies
package:pub/src/package_graph.dart 100:12                        PackageGraph.isPackageMutable
package:pub/src/executable.dart 100:33                           runExecutable
package:pub/src/command/run.dart 88:26                           RunCommand.runProtected
package:pub/src/command.dart 180:45                              PubCommand.run.<fn>
package:pub/src/command.dart 180:33                              PubCommand.run.<fn>
dart:async                                                       new Future.sync
package:pub/src/utils.dart 109:12                                captureErrors.wrappedCallback
dart:async                                                       runZonedGuarded
package:pub/src/utils.dart 126:5                                 captureErrors
package:pub/src/command.dart 180:13                              PubCommand.run
package:args/command_runner.dart 209:27                          CommandRunner.runCommand
package:pub/src/command_runner.dart 173:24                       PubCommandRunner.runCommand
package:pub/src/command_runner.dart 158:20                       PubCommandRunner.run
package:dartdev/dartdev.dart 46:56                               runDartdev
C:\b\s\w\ir\cache\builder\sdk\pkg\dartdev\bin\dartdev.dart 11:9  main
This is an unexpected error. Please run

    dart pub --trace run build_runner build --delete-conflicting-outputs

and include the logs in an issue on https://github.com/dart-lang/pub/issues/new
pub finished with exit code 1

After run "dart pub --trace run build_runner build --delete-conflicting-outputs"

Deprecated. Use `dart run` instead.
Null check operator used on a null value
package:pub/src/package_graph.dart 70:53   PackageGraph.transitiveDependencies.<fn>.<fn>
dart:core                                  Iterable.toSet
package:pub/src/package_graph.dart 70:56   PackageGraph.transitiveDependencies.<fn>
package:pub/src/utils.dart 622:41          mapMap
package:pub/src/package_graph.dart 68:11   PackageGraph.transitiveDependencies
package:pub/src/package_graph.dart 100:12  PackageGraph.isPackageMutable
package:pub/src/executable.dart 100:33     runExecutable
package:pub/src/command/run.dart 88:26     RunCommand.runProtected
This is an unexpected error. Please run

    dart pub --trace pub --trace run build_runner build --delete-conflicting-outputs

and include the logs in an issue on https://github.com/dart-lang/pub/issues/new

Flutter Doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.10.2, on Microsoft Windows [versão 10.0.19042.1526], locale pt-BR)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.9.4)   
[√] Android Studio (version 4.1)
[√] VS Code (version 1.64.2)
[√] Connected device (3 available)
[√] HTTP Host Availability

• No issues found!`
AdrielSchneider commented 2 years ago

Delete your build folder and execute flutter pub get to update your dependencies.

sigurdm commented 2 years ago

pub should not crash here. Are you able to reproduce this reliably?

balenaultra commented 2 years ago

All of my projcts have the same problem. So I deleted the build folder. Now there is another message

One or more plugins require a higher Android SDK version.
Fix this issue by adding the following to C:\dev\XXXXX\android\app\build.gradle:
android {
  compileSdkVersion 31
  ...
}

I change compileSdkVersion version to 31 on android\app\build.gradle.

After that, the new message is:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':platform_device_id:compileDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 10s

┌─ Flutter Fix ────────────────────────────────────────────────────────────────────────────────┐
│ [!] Your project requires a newer version of the Kotlin Gradle plugin.                       │
│ Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then │
│ update C:\dev\xxxxxx\android\build.gradle:                              │
│ ext.kotlin_version = '<latest-version>'                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────┘
Exception: Gradle task assembleDebug failed with exit code 1

So I changed Kotlin version to 1.6.10 on android\build.gradle and it works.

The bad news is that I have to do that on all of my projects.

Thanks

sigurdm commented 2 years ago

@balenaultra I think that now sounds like a flutter issue, and should probably be filed under https://github.com/flutter/flutter/issues/new .

epkalibbala commented 2 years ago

This worked for me:

flutter clean, flutter pub get, flutter pub run build_runner build

In that very order, in the respective terminals.

erfanegtfi commented 1 year ago

Any fix for this issue?? We have to clean project every time.

sigurdm commented 1 year ago

@erfanegtfi can you provide a reliable reproduction. That might help us finding a fix.

MuhammedKpln commented 1 year ago

Having same issue as well, tried to upgrade all packages that is using build_runner but no luck. Need to clean project every time to be able to run. running with --verbose does not provide any valuable either information about why is this happening.

Update: not only happens with build_runner it happens with other pub cli tools as well (Easy localization)

Pub log: here Project: here

Reproduction:

  1. Run flutter clean
  2. Run flutter pub get
  3. Run build runner flutter pub run build_runner build --delete-conflicting-outputs --verbose
  4. Runs fine, but then when running build runner on second time it will crash.
Flutter 3.7.12 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4d9e56e694 (7 days ago) • 2023-04-17 21:47:46 -0400
Engine • revision 1a65d409c7
Tools • Dart 2.19.6 • DevTools 2.20.1
sigurdm commented 1 year ago

Thanks for the reproduction!

The cause is that we special case the name flutter_gen (because that is the synthetic package that flutter generates for i10n), and pub cannot handle a real package with this name.

We should fix the pub client to recognize this and not crash. Most likely by only filtering the flutter_gen package if it is located inside the root package.

But this will never work well if you want to use both i10n and package:flutter_gen.

sigurdm commented 1 year ago

Can others with this problem confirm that they were using the flutter_gen package? (hoping this is the only bug 🤞) @balenaultra @AdrielSchneider @epkalibbala and others?

trongnghiagenki commented 1 year ago

Can other's with this problem confirm that they were using the flutter_gen package? (hoping this is the only bug 🤞) @balenaultra @AdrielSchneider @epkalibbala and others?

Yes, I can confirm it. Got the same error when I used flutter_gen 5.3.0.

Fixed by changing package name https://github.com/FlutterGen/flutter_gen/issues/373#issuecomment-1521903743

erfanegtfi commented 1 year ago

I'm using flutter_genpackage. I solved error after removing it from pubspec.yaml and run flutter pub run build_runner build --delete-conflicting-outputs

includelife1 commented 1 year ago

This error can occurs when have wrong path in pubspec.yaml assets part. Remove all wrong paths and try again. It worked for me.

But even after that at second build it will give error. You must run 'flutter pub get' after every build and you can build again.

sigurdm commented 1 year ago

@includelife1 what version of Flutter are you on? Can you make a reproduction?

chillbrodev commented 1 year ago

I am running Flutter 3.10.5 and have started seeing this on my Bitrise CI. I cache the flutter sdk so I do not need to install fresh each time. Haven't seen this happen locally before.

Gray-Codev-29 commented 1 year ago

any updates on this?, using flutter gen as well

sigurdm commented 1 year ago

@Gray-Codev-29 try using https://pub.dev/packages/flutter_gen_runner instead.

EArminjon commented 1 year ago

I use build_runner and generate: truebecause of l10n, generate true is used allow the flutter pub get command to reload when called automatically the translations.

I can't replace as you suggestd flutter_gen by flutter_gen_runner because of the following exception :

Unhandled exception:
Bad state: Unable to generate package graph, no `/Users/earminjon/IdeaProjects/untitled/.dart_tool/flutter_gen/pubspec.yaml` found.
#0      _pubspecForPath (package:build_runner_core/src/package_graph/package_graph.dart:232:5)
#1      _parsePackageDependencies (package:build_runner_core/src/package_graph/package_graph.dart:206:21)
#2      PackageGraph.forPath (package:build_runner_core/src/package_graph/package_graph.dart:101:33)
<asynchronous suspension>
#3      main (file:///Users/earminjon/.pub-cache/hosted/pub.dev/build_runner-2.3.3/bin/build_runner.dart:27:30)
<asynchronous suspension>
pub finished with exit code 255
sigurdm commented 1 year ago

@EArminjon that looks like build_runner not working with the flutter_gen package generated by generate: true (the stack trace is not inside pub).

This looks like: https://github.com/dart-lang/build/issues/2835

Try upgrading flutter - I believe this works better in newer versions.

EArminjon commented 1 year ago

@sigurdm More data bellow, I hope it can help analysis: Reproductible with flutter 3.7.12 and 3.10.7 (update intl for this).

pubspec.yaml

name: untitled
description: A new Flutter project.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: '>=2.19.6 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

  cupertino_icons: ^1.0.2
  intl: ^0.17.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^2.2.1
  # working
  # flutter_gen: ^5.1.0+1
  # not working
  flutter_gen_runner: ^5.1.0+1

flutter:
  uses-material-design: true
  generate: true

EDIT

Using flutter 3.10.7 allow me tu use as expected flutter_gen so I didn't need to find a way to make flutter_gen_runner working. But that's a weird bug.

anasboyka commented 8 months ago

for me i used swagger_dart_code_generator package and without the json file.. so i just remove the package and it works