dart-lang / build

A build system for Dart written in Dart
https://pub.dev/packages/build
BSD 3-Clause "New" or "Revised" License
785 stars 207 forks source link

[build_runner 1.10.3] Bad state: Unable to generate package graph #2835

Closed aliyazdi75 closed 3 years ago

aliyazdi75 commented 4 years ago

build_runner: ^1.10.3 flutter packages pub run build_runner build:

Unhandled exception:
Bad state: Unable to generate package graph, no `/home/aliyazdi75/Documents/AndroidStudioProjects/koja_beram_app/.dart_tool/flutter_gen/pubspec.yaml` found.
#0      _pubspecForPath (package:build_runner_core/src/package_graph/package_graph.dart:235:5)
#1      _parsePackageDependencies (package:build_runner_core/src/package_graph/package_graph.dart:208:21)
#2      PackageGraph.forPath (package:build_runner_core/src/package_graph/package_graph.dart:97:33)
<asynchronous suspension>
#3      PackageGraph.forThisPackage (package:build_runner_core/src/package_graph/package_graph.dart:110:20)
#4      main (file:///home/aliyazdi75/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-1.10.3/bin/build_runner.dart:27:49)
#5      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#6      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
pub finished with exit code 255

flutter doctor -v:

[✓] Flutter (Channel master, 1.22.0-10.0.pre.317, on Linux, locale en_US.UTF-8)
    • Flutter version 1.22.0-10.0.pre.317 at /home/aliyazdi75/snap/flutter/common/flutter
    • Framework revision 10febea877 (7 hours ago), 2020-09-23 07:22:10 +0200
    • Engine revision 57ee3d4b32
    • Dart version 2.10.0 (build 2.10.0-156.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 (version 4.0)
    • Android Studio at /home/aliyazdi75/Application/android-studio
    • Flutter plugin version 49.0.2
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (3 available)
    • Linux (desktop)  • linux      • linux-x64      • Linux
    • Web Server (web) • web-server • web-javascript • Flutter Tools
    • Chrome (web)     • chrome     • web-javascript • Google Chrome 85.0.4183.12
jpnurmi commented 3 years ago

I'm curious, what's your reason for using generate: true together with synthetic-package: false? Isn't the former specifically for synthetic packages? With non-synthetic packages you'd run flutter gen-l10n yourself, right? Or did I miss something?

dalewking commented 3 years ago

I'm curious, what's your reason for using generate: true together with synthetic-package: false? Isn't the former specifically for synthetic packages? With non-synthetic packages you'd run flutter gen-l10n yourself, right? Or did I miss something?

Generate: true just means to generate automatically when doing a build. synthetic-package just specifies what to generate. They are completely separate and nothing tying them together. There is nothing that implies automatic generation only applies to generation of synthetic packages.

The point is that I do not want to have to run the generation manually and I want it generated in a directory where I can easily commit them to source control

jpnurmi commented 3 years ago

Ok, thanks for the explanation. So the problem is that the two are connected in the flutter tool.

If pubspec.yaml has generate: true, the flutter tool assumes that the synthetic package is generated and adds flutter_gen as a dependency to .dart_tool/package_config.json. However, if l10n.yaml has synthetic-package: false, there won't be any synthetic package generated to .dart_tool/flutter_gen. This is why adding a dummy pubspec.yaml there "fixes" the problem.

The relevant piece of code where the flutter tool adds the bogus flutter_gen dependency is here: https://github.com/flutter/flutter/blob/cd1892f04fda3b6439842a9ab875973bbc52965c/packages/flutter_tools/lib/src/dart/pub.dart#L528

It's called with generateSyntheticPackage = true from here if pubspec.yaml has generate: true: https://github.com/flutter/flutter/blob/cd1892f04fda3b6439842a9ab875973bbc52965c/packages/flutter_tools/lib/src/commands/packages.dart#L144 https://github.com/flutter/flutter/blob/cd1892f04fda3b6439842a9ab875973bbc52965c/packages/flutter_tools/lib/src/flutter_manifest.dart#L379

For what it's worth, build_runner works with the dart tool because it won't add the flutter_gen dependency.

The diff between .dart_tool/package_config.json generated by the dart tool vs. the flutter tool looks something like this:

--- dart_package_config_2.json  2021-09-16 16:59:20.822186748 +0200
+++ flutter_package_config_2.json   2021-09-16 16:59:34.518193827 +0200
@@ -540,9 +540,14 @@
       "rootUri": "../",
       "packageUri": "lib/",
       "languageVersion": "2.12"
+    },
+    {
+      "name": "flutter_gen",
+      "rootUri": "flutter_gen",
+      "languageVersion": "2.12"
     }
   ],
-  "generated": "2021-09-16T14:58:29.544473Z",
+  "generated": "2021-09-16T14:55:37.842621Z",
   "generator": "pub",
   "generatorVersion": "2.15.0-68.0.dev"
 }
parmarravi commented 2 years ago

whats the correct way to resolve this issue?

jifferon commented 2 years ago

The issue is still there

natebosch commented 2 years ago

It's hard to track down the current remaining issues in this thread.

Can somebody still facing difficulty here please open a new issue with a short but complete reproduction of the problem so that it is more clear what the current focus is?

cofirazak commented 2 years ago

Hi, For our project, we only needed to create an empty pubspec.yaml

name: test
description: A new Flutter application.
version: 1.0.0

dependencies:

dev_dependencies:

here: .dart_tool/flutter_gen/pubspec.yaml And the problem went away. Not sure what caused this issue in the first place tho :/

In my case this answer helped. If there is no pubspec.yaml at .dart_tool/flutter_gen/ i get: no .../.dart_tool/flutter_gen/pubspec.yaml found.

Albert221 commented 2 years ago

generate: true in pubspec.yaml is only required if we're using synthetic flutter_gen package from .dart_tools/flutter_gen. If you're experiencing this problem because of the flutter gen-l10n, simply stop using synthetic package by adding synthetic-package: false to your l10n.yaml

https://github.com/flutter/flutter/blob/52ae102f182afaa0524d0d01d21b2d86d15a11dc/packages/flutter_tools/lib/src/flutter_manifest.dart#L379-L388

AleksandarSavic95 commented 2 years ago

For some weird reason, running flutter packages pub get instead of flutter pub get solved my issue.

Before that I ran flutter clean and deleted pubspec.lock. I did not have to make any changes in my project manually.

appflowy commented 2 years ago

For some weird reason, running flutter packages pub get instead of flutter pub get solved my issue.

Before that I ran flutter clean and deleted pubspec.lock. I did not have to make any changes in my project manually.

flutter packages pub get works for me. Any links about the difference between flutter pub get and flutter packages pub get. I think they should be the same thing.

AleksandarSavic95 commented 2 years ago

For some weird reason, running flutter packages pub get instead of flutter pub get solved my issue.

Before that I ran flutter clean and deleted pubspec.lock. I did not have to make any changes in my project manually.

flutter packages pub get works for me. Any links about the difference between flutter pub get and flutter packages pub get. I think they should be the same thing.

I thought they are the same, based on a stackoverflow answer , but it turns out they are not..

maks commented 2 years ago

sigh I seem to have just run into this too. On Flutter stable (Flutter 2.10.3), after I changed my l10n.yaml to have synthetic-package: false (and keeping existing generate: true in pubspec.yml) the next time I went to run build_runner for json_serialisable I got the Unhandled exception: Bad state: Unable to generate package graph, error mentioned here.

Then doing a:

flutter clean
flutter pub get
flutter pub run build_runner build --delete-conflicting-outputs

Still gave the same error. Running gen-l10n also does *not8 fix the issue via https://github.com/flutter/flutter/pull/68206 as mentioned previously because I'm explicitly not using a synthetic package. What does fix it is instead is running flutter packages pub get and then running builder runner.

Like @AleksandarSavic95 I am confused as to why one works and the other doesn't as I too thought they were aliases to the same command?

@jakemac53 Could this issue please be reopened?

jakemac53 commented 2 years ago

Possibly flutter packages pub get creates the package (including pubspec) but flutter pub get does not?

In any case though I would open a new issue on the flutter repo. All packages must have a pubspec, and they must declare their used dependencies. If they aren't published on pub then the flutter tool should put publish_to: none to indicate that. But the pubspec must exist for build_runner to work properly.

dvird commented 2 years ago

Best way to solve it is manually.

create in folder ".dart_tool" in your project environment folder called "flutter_gen" then create in the folder "flutter_gen" file called "pubspec.yaml" then write in "pubspec.yaml" this text: " name: sad description: sad "

good luck

in case of more problems: add this to your project pubspec.yaml

dependency_overrides: analyzer: 3.3.0

then flutter clean, then flutter pub get

kafumi commented 2 years ago

Deleting generate: true from pubspec.yaml works for me. This change will stop automatic generation of l10n.dart but I can still generate the file by manually executing flutter gen-l10n command.

I understand generate: true in pubspec.yaml is not compatible with synthetic-package: false in l10n.yaml. So we need to change either of generate: true or synthetic-package: false.

viroth-ty commented 2 years ago

You don't need l10n, remove generate: true in `

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

`

rahmanrezaee commented 2 years ago

I just run flutter clean and then flutter pub get may u need this command also flutter pub run build_runner watch --delete-conflicting-outputs

Happy Codes :)

AlexV525 commented 1 year ago

Simply relying on flutter_gen as a dev_denpendencies will solve the issue.

ScottS2017 commented 1 year ago

April '23, it happened again to me after a clean. What fixed it was changing the sequence of commands.

When I tried to run build_runner before gen-l10n, I had the error. Swapping them in the sequence fixed it.

RyoheiKawamon commented 1 year ago

Simply relying on flutter_gen as a dev_denpendencies will solve the issue.

I THINK THIS IS THE ANSWER.

asafratzon commented 1 year ago

For me the minimal cleanup needed is .dart_tool/package_config.json, so I run the following:

rm .dart_tool/package_config.json && dart run build_runner watch --delete-conflicting-outputs

treble-maker123 commented 1 year ago

Possibly flutter packages pub get creates the package (including pubspec) but flutter pub get does not?

In any case though I would open a new issue on the flutter repo. All packages must have a pubspec, and they must declare their used dependencies. If they aren't published on pub then the flutter tool should put publish_to: none to indicate that. But the pubspec must exist for build_runner to work properly.

Replacing flutter packages get with flutter packages pub get fixed this issue for me.

ghost commented 11 months ago

I had a similar issue developing on Chrome OS Flex:

chime@penguin:~/dev/x$ dart run build_runner build
Building package executable... (5.4s)
Built build_runner:build_runner.
Unhandled exception:
Bad state: Unable to generate package graph, no `/home/chime/dev/x/.dart_tool/flutter_gen/pubspec.yaml` found.
#0      _pubspecForPath (package:build_runner_core/src/package_graph/package_graph.dart:268:5)
#1      _parsePackageDependencies (package:build_runner_core/src/package_graph/package_graph.dart:242:21)
#2      PackageGraph.forPath (package:build_runner_core/src/package_graph/package_graph.dart:120:33)
<asynchronous suspension>
#3      main (file:///home/chime/.pub-cache/hosted/pub.dev/build_runner-2.4.6/bin/build_runner.dart:27:30)
<asynchronous suspension>

I was able to fix it by changing generate from true to false in my pubspec.yaml like so:

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

Is it some issue with intl package?

natebosch commented 11 months ago

This is being discussed and tracked at https://github.com/dart-lang/sdk/issues/53696

codewithmustafa commented 8 months ago

I got the same exception but my solution was strange:

I generated localization code and files first with:

flutter gen-l10n

and then generating others wıth:

flutter pub run build_runner build

worked seamlessly.

markbeij commented 8 months ago

This is being discussed and tracked at dart-lang/sdk#53696

That issue doesn't seem to fix this issue. @jakemac53 can we reopen this issue?

natebosch commented 8 months ago

The linked issue has been reopened. Unless we suspect there is a different problem than the conflict between dart pub get and flutter pub get we should keep the conversation at https://github.com/dart-lang/sdk/issues/53696

If there is a different issue than the conflict in package resolutions it can be opened as a new issue in this repo.

WongueShin commented 5 months ago

I'm not sure why this phenomenon occurs, but I have identified the following reproduction conditions and a solution.

Reproduction conditions:

If you run pub get even once in a Flutter package that depends on both l10n and build_runner, errors will occur in all subsequent "dart run build_runner build" operations.

Solution:

Delete the pubspec.lock file right before each "dart run build_runner build". I have automated this process with a script that I am currently using.

I hope my case will be helpful.

ktul15 commented 2 months ago

If you are not using flutter_gen in your project, then simply removing that dependency from pubspec.yaml and deleting .dart_tool directory can solve it for you.