-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
--dart-define-from-file not available in build_runner #3574
Comments
Can you elaborate a bit more on exactly why you need this? In general we don't support environment defines, and doing so in a proper way (for invalidation) would be very challenging and a lot of work. |
Can these use cases be moved to read from builder options defined in the build config? https://pub.dev/packages/build_config#configuring-builders-applied-to-your-package |
Thanks for having a look at my use case :) I'm using a few build_runner libraries, including riverpod and ferry_generator. The problems I have are as a consumer of these libraries, intersecting with my own tooling. I have a custom Assert annotation to make it a compiler error to forget to add an api key. Perhaps there is an variable I can use to ignore this on a build_runner compile? Here is the assert class: class Assert {
const Assert(bool test, [Object? message]) : assert(test, message);
} And the api key that causes a compiler error with build_runner: @Assert(STREAM_CHAT_API_KEY.length != 0, "STREAM_CHAT_API_KEY is empty")
const String STREAM_CHAT_API_KEY =
String.fromEnvironment("STREAM_CHAT_API_KEY"); |
The builders you use would have to be the ones to provide that configuration I think? I am curious exactly how the error surfaces. I haven't seen a situation like this before where an annotation has an assert in its constructor. |
The Assert annotation has the value of failing if I've forgotten to add the key in CI/CD. I had the problem once of setting up a new build on iOS and forgetting to give it the env vars. Ended up pushing a prod version that couldn't access the api, not very good! Heres the failing log output for build_runner:
|
Yeah I get what you are doing with it, and it's a creative solution. I just haven't seen it before :). I am a bit surprised it doesn't show up as an analysis error when you run the analyzer normally as well, but maybe you are providing environment variables to the analyzer somehow?
Thanks, this was enough to get me closer to the actual issue. It looks like this is the There is a general option Would you mind running the same build with We likely want to add a way to configure this, or possibly even change the default. cc @natebosch We could also look into actually supporting environment variables but I don't personally have a lot of time to spend on that and it would be fairly involved. |
Yea I just found it online somewhere, I'm don't know how dart lets you extend from a function but I'm not complaining! I'm running vscode, I set my env vars for my editor in .vscode/settings.json. Possibly the analyzer is picking it up there? {
"dart.flutterRunAdditionalArgs": [
"--web-port", "8080",
"--web-hostname", "0.0.0.0",
"--web-launch-url", "http://localhost:8080/",
"--dart-define-from-file", ".env.json"
],
} And here's the whole output with --verbose:
Yea I was hoping that env vars would be something that would come for free with dart stuff, it's a pretty edge use case so don't think its worth that much time. |
Ah, so in this case freezed/riverpod do use the |
Yeah - I think we'd have to get the config through to the analyzer, which would necessitate a public API for that in
It might be a good idea - I don't know if we should consider it breaking. Turning an error into a non-error probably won't cause much churn for existing usage. It looks like the current default was the backwards compatible one, and I don't see any discussion about whether it made more sense as the default. I can imagine that the throwing default behavior was most useful when the unresolved annotation is the one that the builder wants to read. When we rolled that out it was typical to have only the annotations for one type of codegen applied to that library, and it would be more confusing if nothing was output than to see the error. It might be more common now to have multiple annotations for different purposes - and it might not make sense for one unresolved annotation to interfere with the others. |
I was looking for a method to enforce the presence of environment variables and validate their values in my Dart program during compilation (interestingly, arrived at the same solution). I encountered the same problem as the OP.
|
Problem
In some of my code generation, I use environment variables. Those environment variables are stored in an env.json, as per flutter recommendations. I can't load my environment variables into build runner, which is causing problems. What should I do?
The text was updated successfully, but these errors were encountered: