-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Add --dart-define option #44083
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
Add --dart-define option #44083
Conversation
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie. Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
This might not work with DDC as we're building the SDK ( This means config options in If @jonahwilliams 's incremental approach builds the SDK, we might get this for free when we move to that. fyi - @jakemac53 |
@vsmenon I'm adding it as an advanced hidden option for users who know what they are doing. So I'm not too worried that this doesn't work consistently. The bar is much lower than that used for standard documented options. |
This likely will work with dart2js since that compiles the whole world including the SDK. But it won't work in DDC for variables that are used inside of the sdk since dart_sdk.js is precompiled without these options. It doesn't look like this was trying to plumb anything through for DDC anyways though (at least not via build_runner). |
This actually needs to allow for bool, int, double, or String values. The syntax could be something like |
All the actual compiler apis talk in terms of a |
Yeah, on the command line and tool it should be strings, but as written this only allows specifying which defines you want to be true. See https://github.com/flutter/flutter/pull/44083/files#diff-6edde590c56c73e35381fb97d37eb862R153 |
@@ -369,6 +372,8 @@ class Environment { | |||
/// which prevents the config from leaking into different builds. | |||
final Map<String, String> defines; | |||
|
|||
final List<String> dartDefines; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a key under defines
so that rebuilds are correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
in |
btw, @sigmundch mentioned we may have similar issues with dart2js using a pre-compiled (i.e., ninja generated) dill file for the SDK. |
PTAL, but heads up, I'm unable to run all tools tests locally for some reason. So if there are test failures, it may require a few Cirrus runs to make everything green. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some small nits
@@ -111,6 +113,7 @@ class WebFs { | |||
final String _target; | |||
final BuildInfo _buildInfo; | |||
final bool _initializePlatform; | |||
final List<String> dartDefines; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make dartDefines
private here to match with everything else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Done.
@@ -130,6 +132,13 @@ class Dart2JSTarget extends Target { | |||
? PackageMap.globalGeneratedPackagesPath | |||
: PackageMap.globalPackagesPath; | |||
final File outputFile = environment.buildDir.childFile('main.dart.js'); | |||
|
|||
// Dart defines are encoded as a JSON array, so we need to parse it. Also JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Also, JSON
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -263,3 +330,39 @@ class FakeDevice extends Fake implements Device { | |||
return null; | |||
} | |||
} | |||
|
|||
class MockFeatureFlags extends Mock implements FeatureFlags { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use TestFeatureFlags
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL! Done.
|
||
// Dart defines are encoded as a JSON array, so we need to parse it. Also JSON | ||
// parser returns List<dynamic>, so we need to cast it. | ||
final List<String> dartDefines = environment.defines.containsKey(kDartDefines) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
catch the parse exception and rethrow with an error message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A parser exception is not possible, is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flutter assemble -dDartDefines=[23,
would throw I believe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha. Fixing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -130,6 +131,13 @@ class Dart2JSTarget extends Target { | |||
? PackageMap.globalGeneratedPackagesPath | |||
: PackageMap.globalPackagesPath; | |||
final File outputFile = environment.buildDir.childFile('main.dart.js'); | |||
|
|||
// Dart defines are encoded as a JSON array, so we need to parse it. Also JSON | |||
// parser returns List<dynamic>, so we need to cast it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't actually need to cast it, since you are only calling toString treat it as a List<Object>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edited: github hit my <Object>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
try { | ||
dartDefines = jsonDecode(dartDefinesJson); | ||
} on FormatException catch (_) { | ||
throw Exception( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NICE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the build system define a more specific exception type that this could be translated to instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is only used in a single place, I would leave it as is for now.
Could you also provide the defines to the dart kernel compiler? It might not be plumbed through gradle or xcode yet, but that would be a much smaller change. Otherwise document that it only works for web release builds |
Done: fddf1c5 |
9fd2d58
to
9c71e61
Compare
Could you file issues for plumbing this setting through gradle and xcode? Again, not necessary to do as part of this PR. |
Done: #44483 LMK what I should do next with this PR. |
BTW, take a look at this change: https://github.com/flutter/flutter/pull/44083/files#diff-7924f19d4eafce28aee7d180926625a0 I did it because when an integration test failed due to a command error the test would get stuck forever. The change catches premature process exits and fails the test immediately. I hope I didn't mess anything up. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Seems like a reasonable change |
Build failure is infra failure. Landing on red. |
Don't mean to violate protocol commenting on a closed issue but this seemed like the best place to do so. As this was merged in, I would expect it to be working in at least dev, but I am not able to get it to work as expected. I set the channel to dev (flutter channel dev), then did an upgrade (flutter upgrade). Then I issued the following: In my main_generic.dart I do this: The env variable is always returns PROD. Am I doing something wrong? |
A closed PR is not the right place for a bug report. If you file an issue with all of the relevant details (flutter doctor -v please) then we might be able to help |
please cc me and I'll take a look |
Add a
--dart-define
option that forwards the values as-D
options to the underlying Dart compiler.