这是indexloc提供的服务,不要输入任何密码
Skip to content

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

Merged
merged 1 commit into from
Nov 10, 2019
Merged

Add --dart-define option #44083

merged 1 commit into from
Nov 10, 2019

Conversation

yjbanov
Copy link
Contributor

@yjbanov yjbanov commented Nov 3, 2019

Add a --dart-define option that forwards the values as -D options to the underlying Dart compiler.

@fluttergithubbot
Copy link
Contributor

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.

@fluttergithubbot fluttergithubbot added the tool Affects the "flutter" command-line tool. See also t: labels. label Nov 3, 2019
@vsmenon
Copy link

vsmenon commented Nov 4, 2019

This might not work with DDC as we're building the SDK (dart_sdk.js) from ninja, and not flutter tool.

This means config options in dart:* files (which I think includes web skia) wouldn't pick this up.

If @jonahwilliams 's incremental approach builds the SDK, we might get this for free when we move to that.

fyi - @jakemac53

@yjbanov
Copy link
Contributor Author

yjbanov commented Nov 4, 2019

@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.

@jakemac53
Copy link
Contributor

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).

@jonahwilliams
Copy link
Contributor

This actually needs to allow for bool, int, double, or String values. The syntax could be something like Bar=true,baz=foo which gets turned into -DBar=true,-Dbaz=foo

@jakemac53
Copy link
Contributor

This actually needs to allow for bool, int, double, or String values.

All the actual compiler apis talk in terms of a Map<String, String> though afaik. The specialized fromEnvironment constructors handle the parsing so you can always work in terms of strings.

@jonahwilliams
Copy link
Contributor

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;
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@jonahwilliams
Copy link
Contributor

in lib/src/compile.dart, the KernelCompiler should be updated to take a list of dart defines as well. in build_system/targets/dart.dart (I know, I need to rename it), this can be passed through to the kernel compiler invocation to make this work on mobile/desktop too

@vsmenon
Copy link

vsmenon commented Nov 5, 2019

btw, @sigmundch mentioned we may have similar issues with dart2js using a pre-compiled (i.e., ninja generated) dill file for the SDK.

@yjbanov
Copy link
Contributor Author

yjbanov commented Nov 5, 2019

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.

Copy link
Contributor

@CaseyHillers CaseyHillers left a 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;
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Also, JSON

Copy link
Contributor Author

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TestFeatureFlags

Copy link
Contributor Author

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)
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. Fixing.

Copy link
Contributor Author

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.
Copy link
Contributor

@jonahwilliams jonahwilliams Nov 6, 2019

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>

Copy link
Contributor

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>

Copy link
Contributor Author

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE

Copy link
Member

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?

Copy link
Contributor

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.

@jonahwilliams
Copy link
Contributor

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

@yjbanov
Copy link
Contributor Author

yjbanov commented Nov 7, 2019

Could you also provide the defines to the dart kernel compiler?

Done: fddf1c5

@yjbanov yjbanov force-pushed the dart-defines branch 2 times, most recently from 9fd2d58 to 9c71e61 Compare November 8, 2019 05:02
@jonahwilliams
Copy link
Contributor

Could you file issues for plumbing this setting through gradle and xcode? Again, not necessary to do as part of this PR.

@yjbanov
Copy link
Contributor Author

yjbanov commented Nov 8, 2019

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.

@yjbanov
Copy link
Contributor Author

yjbanov commented Nov 8, 2019

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.

Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jonahwilliams
Copy link
Contributor

Seems like a reasonable change

@yjbanov
Copy link
Contributor Author

yjbanov commented Nov 10, 2019

Build failure is infra failure. Landing on red.

@yjbanov yjbanov merged commit e7073f9 into flutter:master Nov 10, 2019
@mdebbar mdebbar changed the title Add --dart-defines option Add --dart-define option Nov 19, 2019
@darrinps
Copy link

darrinps commented Apr 28, 2020

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:
flutter -run -t lib/main_generic.dart --dart-define=ENV=DEV

In my main_generic.dart I do this:
var env = String.fromEnvironment("ENV", defaultValue: "PROD");

The env variable is always returns PROD.

Am I doing something wrong?

@jonahwilliams
Copy link
Contributor

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

@jonahwilliams
Copy link
Contributor

please cc me and I'll take a look

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants