Open Andreas02-dev opened 5 months ago
I believe I have found the issue. As @maximoffua pointed out in https://github.com/maximoffua/flutter.nix/issues/19#issuecomment-2194391641, flutter pub run ...
did work.
This lead me to look further into the Flutter source code, where we see that flutter pub run
uses PackagesForwardCommand
under the hood https://github.com/flutter/flutter/blob/ef34436402beaab4822a7306d9ffca17ebbdd2e3/packages/flutter_tools/lib/src/commands/packages.dart#L40C19-L40C41, with the description of running the pub tool in a Flutter context (https://github.com/flutter/flutter/blob/ef34436402beaab4822a7306d9ffca17ebbdd2e3/packages/flutter_tools/lib/src/commands/packages.dart#L124). Drilling deeper down, we see that the runCommand
method calls pub.interactively
. Looking at the default implementation reveals that it utilizes _computePubCommand
which uses Cache.flutterRoot
(https://github.com/flutter/flutter/blob/ef34436402beaab4822a7306d9ffca17ebbdd2e3/packages/flutter_tools/lib/src/dart/pub.dart#L548).
Putting 2 and 2 together, we find a patch in @maximoffua 's repository (https://github.com/maximoffua/flutter.nix/blob/main/pkgs/flutter/patches/flutter-pub-dart-override.patch) that modifies this part of Flutter in order for the flutter pub
command to get the correct (containing the Flutter SDK) Dart SDK without using FlutterRoot.
Now that we know what makes dart run
or dart pub run
different from flutter pub run
, it's trivial to find a solution.
For this, we look at https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/cache.dart, which shows us we can allow Cache.flutterRoot to find the correct Flutter root path by setting the appropriately named environment variable FLUTTER_ROOT
to point to "${pkgs.flutter}".
After setting this (https://github.com/Andreas02-dev/nixpkgs_flutter322_regression/blob/807dc5919b897e2f060726ae03ce05a8e64bee4a/fix/flake.nix#L24), we can now use dart run build_runner doctor -v
in Flutter 3.22.
Now that we know what's happening and how we could solve it, we should figure out the best way to solve this: do we document that setting FLUTTER_ROOT
is required for the dart run
command to work with packages requiring the Flutter SDK, or do we try to patch this in somehow? I'm curious on what @maximoffua and the nixpkgs maintainers @babariviere @ericdallo @mkg20001 @RossComputerGuy @FlafyDev @hacker1024 think.
We can discuss this here, or create a thread. If we create a thread, I would appreciate it if I could be kept in the loop.
Looking forward to hearing from you, Andreas
Thank you, @Andreas02-dev for this great investigation!
I think, it is sane to add the following to the flutter derivation:
# ...
shellHook = ''
export FLUTTER_ROOT="${pkgs.flutter}"
'';
Will this work? I have never tried shellHook
yet, however I have seen that some packages set extra env vars when they are used in profile/shell. Another question is how to reference self in this hook...
Hi - I just ran into this and can confirm that the suggested fix above does indeed work. Please see a minimal dev shell below that works!
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
inputs:
inputs.flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import inputs.nixpkgs {
inherit system;
config = {
allowUnfree = true;
};
};
devShells.default = pkgs.mkShell
{
buildInputs = with pkgs;
[
flutter
];
shellHook = ''
export CHROME_EXECUTABLE=/run/current-system/sw/bin/google-chrome-stable
export PATH="$PATH":"$HOME/.pub-cache/bin"
export FLUTTER_ROOT="${pkgs.flutter}"
fish
'';
};
in
{
inherit devShells;
}
);
}
Describe the bug
The Flutter SDK cannot be found by
build_runner
and potentially other tools in the latestflutter
version in nixpkgs (currently 3.22.0). In the previous nixpkgs Flutter version, which can now be found underflutter319
, this worked correctly.Steps To Reproduce
Steps to reproduce the behavior:
dart run build_runner doctor -v
(or any other command of build_runner, such as watch or build).I have also created a reproduction repository at https://github.com/Andreas02-dev/nixpkgs_flutter322_regression which also contains the reproduction steps for a working and failing example, where the only difference is using Flutter 3.19 (working) or Flutter 3.22 (failing).
Expected behavior
build_runner
does not fail:Actual behavior
build_runner
fails with the following output:Screenshots
If applicable, add screenshots to help explain your problem.
Additional context
Add any other context about the problem here.
Notify maintainers
@babariviere @ericdallo @mkg20001 @RossComputerGuy @FlafyDev @hacker1024
Metadata
Please run
nix-shell -p nix-info --run "nix-info -m"
and paste the result.Add a :+1: reaction to issues you find important.