Skip to content

Pub build/serve/run does not support defining environment variables #15806

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

Closed
DartBot opened this issue Dec 27, 2013 · 20 comments
Closed

Pub build/serve/run does not support defining environment variables #15806

DartBot opened this issue Dec 27, 2013 · 20 comments
Labels
type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Dec 27, 2013

This issue was originally filed by @butlermatt


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.

@munificent
Copy link
Member

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.

@anders-sandholm
Copy link
Contributor

Added Area-Pub label.

@munificent
Copy link
Member

Removed Priority-Unassigned label.
Added Priority-Medium label.

@nex3
Copy link
Member

nex3 commented Jan 6, 2014

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 #14664.

@munificent
Copy link
Member

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

@munificent
Copy link
Member

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 #15925.

@nex3
Copy link
Member

nex3 commented Jan 7, 2014

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.

@munificent
Copy link
Member

Issue #16370 has been merged into this issue.

@sethladd
Copy link
Contributor

Added Pub-Build label.

@sethladd
Copy link
Contributor

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.

@DartBot
Copy link
Author

DartBot commented Apr 22, 2015

This comment was originally written 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

  • just passing a pubspec.yaml like pub xxx &lt; pubspec_release.yaml
  • using shell scripts (awk/sed) cat pubspec.yaml | somescript | pub xxx
  • from a Dart script that processes a pubspec.yaml template and feeds the result into the pub 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.

@DartBot
Copy link
Author

DartBot commented Apr 22, 2015

This comment was originally written 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

@nex3
Copy link
Member

nex3 commented Apr 22, 2015

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.

@DartBot
Copy link
Author

DartBot commented Apr 22, 2015

This comment was originally written 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.

@sethladd
Copy link
Contributor

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

@DartBot
Copy link
Author

DartBot commented Apr 22, 2015

This comment was originally written 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.

@sethladd
Copy link
Contributor

Sorry I wasn't clear. I meant "we are unable to set environment variables from the command line when we use pub build"

@DartBot
Copy link
Author

DartBot commented Apr 22, 2015

This comment was originally written 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.

@sethladd
Copy link
Contributor

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

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

This issue has been moved to dart-lang/pub#798.

@DartBot DartBot closed this as completed Jun 5, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants