Repository: flutter
Issue: Cannot reference generated code in .dart_tool in a Flutter project
Labels: c: new feature,tool,dependency: dart,has reproducible steps,P3,found in release: 2.5,found in release: 2.6,team-tool,triaged-tool

This is related to a previous issue https://github.com/flutter/flutter/issues/16603.

The conclusion seems to have been that support was added to flutter and you could add a `builders` section but this appears to have been removed and the wiki https://github.com/flutter/flutter/wiki/Code-generation-in-Flutter says that builder_runner should "just work".

I've written a custom builder that is generating code for the root and for its dependencies and according to the build_runner docs this forces the output destination to be set to `cache` rather than source. So the code I'm generating is now in `.dart_tool/build/generated/<pkg>`.

Sadly the combination of these things now means I can't use the generated code in my flutter package.
To import I used: `import 'package:getwidget/components/accordion/gf_accordion.hypi.widget.dart';`
This is the only thing Intellij doesn't complain about. Any attempt to use a relative path to `.dart_tool` says it doesn't exist, Intellij doesn't even let it auto complete like it does with `lib`, `test` etc.

The workaround suggested in #16603 no longer works. The current docs says:
` If a Builder specifies that it outputs to "source" it will never run on any package other than the root `.
My builder rarely runs on root, in most cases it operates on dependencies.

```
[+1293 ms] test/htwd_test.dart:4:8: Error: Error when reading '../../../../../.flutter/.pub-cache/hosted/pub.dartlang.org/getwidget-2.0.4/lib/components/accordion/gf_accordion.hypi.widget.dart': No such file or directory
[   +6 ms] import 'package:getwidget/components/accordion/gf_accordion.hypi.widget.dart';
[        ]        ^
[ +304 ms] test/htwd_test.dart:9:19: Error: Method not found: 'TinkerGFAccordion'.
[        ]     final g = new TinkerGFAccordion();
[        ]                   ^^^^^^^^^^^^^^^^^
```

Flutter doctor:
```
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.5.2, on macOS 11.5.1 20G80 darwin-arm, locale en-GB)
[✗] Android toolchain - develop for Android devices
    ✗ ANDROID_HOME = /usr/local/opt/android-sdk
      but Android SDK not found at this location.
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] Android Studio (version 2020.3)
[✓] Android Studio (version 2020.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2020.3.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2021.2.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2021.2.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2021.2)
[✓] VS Code (version 1.60.2)
[✓] Connected device (2 available)

! Doctor found issues in 1 category.
```

```
dev_dependencies:
  build_runner: ^2.1.2
  build_test: ^2.1.2
```

How can I import the generated files from `.dart_tool` dir?

FYI, I've seen:
https://github.com/flutter/flutter/issues/16603
https://github.com/dart-lang/build/issues/216
https://github.com/flutter/flutter/issues/27141
https://github.com/flutter/flutter/wiki/Code-generation-in-Flutter
https://github.com/flutter/flutter/issues/16603
https://github.com/dart-lang/build/tree/master/build_config#defining-builders-to-apply-to-dependents (`builder_to`)