Issue by butlermatt
_Originally opened as dart-lang/sdk#15806_
Currently dart2js supports -D option to define environment variables. See Seth's blog post:
http://blog.sethladd.com/2013/12/compile-time-dead-code-elimination-with.html
Currently pub build will not accept (let alone pass) -D arguments to dart2js.
Comment by munificent
Yeah, this would be good to support.
Might also be nice to be able to specify these in the pubspec.
_Removed Type-Defect label._
_Added Type-Enhancement, Triaged labels._
Comment by nex3
I'm not a big fan of the ability to define arbitrary environment variables in the pubspec. It seems out-of-scope. Users have the means to define their environment variables via the OS; if a variable is specific to a project and not to a user's configuration, it's not really an environment variable anyway.
If the use case for this is just passing flags to dart2js, this should be merged into issue #694.
Comment by munificent
Users have the means to define their environment variables via the OS
These "environment variables" are an entirely unrelated concept to the OS's environment variables. They're a way of passing in constants to Dart code from outside. See:
https://api.dartlang.org/docs/channels/stable/latest/dart_core/bool.html#fromEnvironment
https://api.dartlang.org/docs/channels/stable/latest/dart_core/int.html#fromEnvironment
https://api.dartlang.org/docs/channels/stable/latest/dart_core/String.html#fromEnvironment
They are passed to the command-line VM using "-D<name> <value>" (I think? I'm not aware of any actual documentation of this feature).
Comment by munificent
Pub build and pub serve are currently focused on web applications. Dartium does not support these "environment variables" yet, so we'd be unable to support this in pub serve. Given that, we won't support it in pub build yet because that would lead to diverging behavior between the app in development and in production.
Once Dartium supports this, we can look into figuring out how we want to pass this along to it.
_Marked this as being blocked by dart-lang/sdk#15925._
Comment by nex3
These "environment variables" are an entirely unrelated concept to the OS's environment variables. They're a way of passing in constants to Dart code from outside.
Ugh, "environment variable" is not a good name for that concept. Now we have two notions of "environment variable" that refer to completely separate ways of passing in constants to Dart code from outside.
Comment by sethladd
Here's the use case:
We have a build process. We compile our app with dart2js. We set environment variable for the VERSION, via -DVERSION=1.2.3
dart2js correctly "bakes" in that VERSION value into the compiled code.
pub build is the "canonical" way to build a Dart app. However, we can't pass in the environment variable via pub build tool via command line. Often times, the build step is part of a larger workflow, driven by a script. We don't want to physically edit the pubspec.yaml file in the build workflow when we change this VERSION, we'd like to set these values from the command line.
cc @johnmccutchan.
Comment by zoechi
I think a very simple solution to all these "configurable pubspec.yaml" issues would be to add support for reading the pubspec.yaml file from stdin.
This would allow all kind of solutions
pub xxx < pubspec_release.yamlcat pubspec.yaml | somescript | pub xxxpub instance started by the script (for example by using Grinder).An ugly workaround is to rename the pubspec.yaml, save a customized file as pubspec.yaml, run pub xxx and then restore the original pubspec.yaml.
Comment by paulevans
or... you know pub could just pass on the environment to the transformers it runs :)
I cannot even get Seth's debug example to work via pub build / serve:
http://blog.sethladd.com/2013/12/compile-time-dead-code-elimination-with.html
Comment by nex3
Passing in an entire pubspec via stdin seems vastly like overkill, especially since you need to make sure that it has everything else identical to the current pubspec. At that point you might as well just write a script to temporarily modify the existing pubspec.
Comment by zoechi
It allows a lot of flexibility without adding much complexity to pub.
Temporaily modifying pubspec.yaml is IMHO much more error-prone (file permissions, concurrent file access, restore default file in case of an error, ...).
I don't see why this should be overkill. It would be simple to use a few $xxx and do shell string interpolation or similar.
I also can't imagine pubspec.yaml to grow to a size where this approach might cause problems.
Comment by sethladd
Related conversation on Twitter: https://twitter.com/paulecoyote/status/590892034900131840
This blog post talks about String.fromEnvironment() but this technique doesn't work with pub build: http://blog.sethladd.com/2013/12/compile-time-dead-code-elimination-with.html
Comment by zoechi
I'm not sure what you mean by "doesn't work with pub build". I created a simple example here https://github.com/zoechi/build_variables/blob/master/pubspec.yaml#L15 and I think it works. Maybe I misunderstood your problem.
pub build doesn't support setting it from command line but it supports it from the $dart2js transformer config.
Comment by sethladd
Sorry I wasn't clear. I meant "we are unable to set environment variables from the command line when we use pub build"
Comment by Fox32
Our current workaround is using a transformer that updates a template dart file that comes with the project. The template file defines some constants that also allow for dead code removal. Right now the transformer gets the values to insert into the template file from OS environment variables that we have to define in the current shell before running pub build.
It's not perfect but we don't have to modify our pubspec.yaml for different configurations.
I could imagine a implementation were pub can have additional build parameters that barback passes to each transformer's constructor via the BarbackSettings class. The further behavior is up to the transformer, but $dart2js could use them as environment variables.
Comment by sethladd
I haven't confirmed, but I imagine that pub run and pub serve don't support passing -D all the way through. I might be wrong, though.
_Changed the title to: "Pub build/serve/run does not support defining environment variables"._
Based on what pub build --help says and what is does, this issue seems to be resolved 馃憤.
As of Pub 1.20.1 there is no environment variables passing support.
$ DART_ENV=local pub build # Doesn't work.
const env = const String.fromEnvironment('DART_ENV') // null
See the comment before yours
-D, --define Defines an environment constant for dart2js.
You are right @zoechi. However, this issue is about supporting env vars by pub.
What I see now.
VAR=value pub build does not work.pubspec.yaml does not work either.transformers:
- $dart2js
commandLineOptions: ["-DVAR=$VAR"] # Does not work.
$ export VAR=value
$ pub build
md5-19cea93c7802d84725be30d418e80639
transformers:
- $dart2js
commandLineOptions: ["-DVAR=value"] # Does not work.
In each case const String.fromEnvironment('VAR') will be null.
@koorgoo
As far as I see it's only about supporting the -D paramter of dart2js, which is done. "Environment Variables" is just a confusing term here, it's has nothing to do with the environment variables provided by the operating system.
Better create a separate issue for the feature that you described above.
There should already be an issue about disambiguate that.
@Fox32, as a newbie I misunderstood environment variable term.
Sorry. The term is really confusing.
We are no longer supporting transformers or pub serve/build going forward (this tooling has moved elsewhere into the https://github.com/dart-lang/build repository and https://github.com/dart-lang/webdev CLI) so a fix for this is not planned.
We'll be updating the tooling section of the Dart website in the coming weeks with more formal documentation on how to get started.
Most helpful comment
Based on what
pub build --helpsays and what is does, this issue seems to be resolved 馃憤.