Skip to content

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

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 Jun 5, 2015 · 27 comments
Closed

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

DartBot opened this issue Jun 5, 2015 · 27 comments
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/1148886?v=3" align="left" width="96" height="96"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/46275?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/3276024?v=3" align="left" width="48" height="48"hspace="10"> Comment by anders-sandholm


Added Area-Pub label.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/46275?v=3" align="left" width="48" height="48"hspace="10"> Comment by munificent


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

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/46275?v=3" align="left" width="48" height="48"hspace="10"> 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).

@DartBot DartBot added type-enhancement A request for a change that isn't a bug Pub-Build labels Jun 5, 2015
@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/46275?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/46275?v=3" align="left" width="48" height="48"hspace="10"> Comment by munificent


Issue #829 has been merged into this issue.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="48" height="48"hspace="10"> Comment by sethladd


Added Pub-Build label.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/405837?v=3" align="left" width="48" height="48"hspace="10"> 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

  • 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 Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/719048?v=3" align="left" width="48" height="48"hspace="10"> 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

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/405837?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="48" height="48"hspace="10"> 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

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/405837?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="48" height="48"hspace="10"> 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"

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/648527?v=3" align="left" width="48" height="48"hspace="10"> 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.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="48" height="48"hspace="10"> 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".

@Fox32
Copy link

Fox32 commented Nov 12, 2016

Based on what pub build --help says and what is does, this issue seems to be resolved 👍.

@koorgoo
Copy link

koorgoo commented Nov 21, 2016

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

@zoechi
Copy link

zoechi commented Nov 21, 2016

See the comment before yours

-D, --define Defines an environment constant for dart2js.

@koorgoo
Copy link

koorgoo commented Nov 21, 2016

You are right @zoechi. However, this issue is about supporting env vars by pub.
What I see now.

  1. pub does not "proxying" environment variables. VAR=value pub build does not work.
  2. Proxying variables with command line options for dart2js transformer in pubspec.yaml does not work either.
transformers:
- $dart2js
  commandLineOptions: ["-DVAR=$VAR"] # Does not work.
$ export VAR=value
$ pub build
  1. Even defining environment variable for dart2js transformer explicitly fails.
transformers:
- $dart2js
  commandLineOptions: ["-DVAR=value"] # Does not work.

In each case const String.fromEnvironment('VAR') will be null.

@Fox32
Copy link

Fox32 commented Nov 21, 2016

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

@zoechi
Copy link

zoechi commented Nov 21, 2016

There should already be an issue about disambiguate that.

@koorgoo
Copy link

koorgoo commented Nov 21, 2016

@Fox32, as a newbie I misunderstood environment variable term.
Sorry. The term is really confusing.

@matanlurey matanlurey added the closed-not-planned Closed as we don't intend to take action on the reported issue label Mar 24, 2018
@matanlurey
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants