Skip to content

Allow specifying environment variables in .ember-cli#7910

Merged
rwjblue merged 1 commit intoember-cli:masterfrom
astronomersiva:experiment_flags_in_ember_cli
Jul 26, 2018
Merged

Allow specifying environment variables in .ember-cli#7910
rwjblue merged 1 commit intoember-cli:masterfrom
astronomersiva:experiment_flags_in_ember_cli

Conversation

@astronomersiva
Copy link
Copy Markdown
Contributor

@astronomersiva astronomersiva commented Jul 11, 2018

Addresses #7896.

Makes it possible to specify environment variables in .ember-cli like

     {
          "environment": {
               "EMBER_CLI_MODULE_UNIFICATION": true
          }
     }

primary: path.dirname(cliPath),
});
const experimentsInDotCli = emberCli.get('experiments') || {};
Object.assign(process.env, experimentsInDotCli);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, I'd prefer to do this where we do the rest of the yam stuff (around here). I also think that if we are "just" merging with process.env we shouldn't use experiments as the key in .ember-cli (we would use environment or something else)...

Copy link
Copy Markdown
Contributor Author

@astronomersiva astronomersiva Jul 12, 2018

Choose a reason for hiding this comment

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

That was my original intention as well but there was an issue with that.

We require models/project here which requires lib/experiments internally. Due to this, the experiments are set even before .ember-cli is loaded.

Placing the Project require below the getConfig also won't help as getConfig again uses lib/experiments internally.

I agree that the key name has to be changed to environment in .ember-cli.

@astronomersiva astronomersiva force-pushed the experiment_flags_in_ember_cli branch 4 times, most recently from c9c7f6d to c8665ed Compare July 17, 2018 17:31
@astronomersiva astronomersiva changed the title [WIP] Allow specifying experiments in .ember-cli Allow specifying experiments in .ember-cli Jul 17, 2018
@astronomersiva
Copy link
Copy Markdown
Contributor Author

@rwjblue , I have made the suggested changes. Please take a look :)

@twokul
Copy link
Copy Markdown
Contributor

twokul commented Jul 17, 2018

might be a silly idea but why not teach yam to understand process.env as a fallback? that way all configuration is still handled in one place and we don't need to make changes to cli itself?

@astronomersiva astronomersiva changed the title Allow specifying experiments in .ember-cli Allow specifying environment variables in .ember-cli Jul 18, 2018
@astronomersiva
Copy link
Copy Markdown
Contributor Author

@twokul, I am not very convinced about having process.env as a fallback for the following reasons:

  • I might have certain env variables in .ember-cli's environment property. It will not be possible to temporarily and quickly try out some other value for one of those env variables by just doing SOME_VAR=SOMETHING_ELSE ember s as .ember-cli will be the primary source.
  • env variables will be present in process.env and .ember-cli's environment field. We get the config from .ember-cli itself in get-config. We will have to make Yam use the environment field in .ember-cli as the primary source and process.env itself as the secondary source.

Apologies if I have misunderstood your comment :)

@twokul
Copy link
Copy Markdown
Contributor

twokul commented Jul 19, 2018

@astronomersiva

We will have to make Yam use the environment field in .ember-cli as the primary source and process.env itself as the secondary source.

is there something wrong with that? 😄

@astronomersiva
Copy link
Copy Markdown
Contributor Author

astronomersiva commented Jul 20, 2018

@twokul , maybe I am not understanding you here properly but the whole point of this PR was to enable people to put their ENV variables in .ember-cli. Using Yam to have process.env as a secondary source for .ember-cli is not going to help with that.

is there something wrong with that?

Consider an env variable MY_ENV_VARIABLE present in both process.env and inside the environment field in .ember-cli. AFAIK, you can use Yam to read a file only. So, even if you have process.env as the secondary source, the config object provided by Yam will be like this:

let config = new Yam('ember-cli', {
    primary: project_root
    secondary: process.env // i believe this is what you are talking about
});

// config will be 
{
    MY_ENV_VARIABLE: 'some value'         // taken from process.env
    environment: {
        MY_ENV_VARIABLE: 'some_value'    // taken from .ember-cli
    }
}

We will now have to get the values present in the environment field, iterate over them and verify they are not present in process.env directly to ensure the behaviour I have written about in the first part of my previous comment.

One solution I see for this is to make Yam automatically treat values inside the environment field as special and assign those values to process.env after doing the same checks that I have done here.

@astronomersiva
Copy link
Copy Markdown
Contributor Author

@rwjblue would really appreciate if you take another look at this.

Copy link
Copy Markdown
Member

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

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

I left some questions inline, and a couple small tweaks in the test but other than that this looks really good, thank you for working on it!

lib/cli/index.js Outdated
// * env variables passed directly should have more priority over .ember-cli.
// * process.env will have only string values. Therefore, check if the value
// in .ember-cli is truthy and then assign to process.env.
if (!process.env.hasOwnProperty(key) && value) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I would do if (!(key in process.env)) {}, also why does value need to be truthy? I'm concerned about null and 0 as values, where a normal process.env.something = null would coerce to 'null'...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, thanks 👍

'.ember-cli': JSON.stringify({
environment: {
"MY_ENV_VARIABLE": "Yayy",
"SHOULDNT_BE_THERE": false,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure about this one (see question above)

});
}));

after(co.wrap(function *() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you want this to be afterEach

describe('.ember-cli environment options', function() {
let sampleApp;

before(co.wrap(function *() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems like it should be beforeEach

@astronomersiva astronomersiva force-pushed the experiment_flags_in_ember_cli branch 2 times, most recently from 133953a to 8a7a329 Compare July 25, 2018 15:14
@astronomersiva astronomersiva force-pushed the experiment_flags_in_ember_cli branch from 8a7a329 to d825561 Compare July 25, 2018 15:15
@astronomersiva
Copy link
Copy Markdown
Contributor Author

@rwjblue , thanks for your time 😄 I have made the suggested changes.

@rwjblue rwjblue merged commit cac9a1a into ember-cli:master Jul 26, 2018
@knownasilya
Copy link
Copy Markdown
Contributor

👏

@rwjblue
Copy link
Copy Markdown
Member

rwjblue commented Jul 26, 2018

Thank you @astronomersiva!

@kellyselden
Copy link
Copy Markdown
Member

Seems to work well here kellyselden/ember-cli-rollup-packager#30. Nice work!

@astronomersiva
Copy link
Copy Markdown
Contributor Author

@kellyselden , thanks :) I also noticed that it is possible to have the .ember-cli file as a JS file. Might help if you want to do some checks within the file or maybe even get rid of all the double quotes for the keys.

@gossi
Copy link
Copy Markdown

gossi commented Jul 27, 2018

Thank you so much for this ❤️

@nightire
Copy link
Copy Markdown
Contributor

@astronomersiva there is a problem, if you import config/environment in your app, config.environment will be replaced as "environment" in .ember-cli, but it used to be process.env.EMBER_ENV. It is important sometimes to determine the environment variable in applications.

before this change:

image

after this change:

image

tested this feature in ember-cli v3.3

@ef4
Copy link
Copy Markdown
Contributor

ef4 commented Aug 16, 2018

@nightire You're right and that is very surprising to me. I filed #7980 for that and an additional similar case.

@lougreenwood
Copy link
Copy Markdown

What is the recommended way to access these environment vars in Ember? Looks like I could use the environment mentioned above, but I get the impression that is a bug and will likely be reverted?

@ef4
Copy link
Copy Markdown
Contributor

ef4 commented Aug 20, 2018

@lougreenwood your config/environment.js runs during the build process (in node, not the browser) so it has access to environment variables. That’s the typical way to get an env car into your app at runtime.

(That doesn’t have anything directly to do with this new feature for automatically setting env vars from the .ember-cli file.)

@lougreenwood
Copy link
Copy Markdown

I'm not sure I follow - are you saying that any values set in the new environment object will be merged into and made available in process.env in for usual consumption in config/environment.js?

@ef4
Copy link
Copy Markdown
Contributor

ef4 commented Aug 20, 2018

Yes. That (and only that) was the intended behavior of this feature.

@lougreenwood
Copy link
Copy Markdown

Nevermind, seems it was an obvious solution for my use case where process doesn't exist for the final compiled app (e.g in your wonderful Prember add-on 😉):

const EmberCliConfig = require('../.ember-cli');

If anyone's interested in my use case (or maybe Google is...) I'm trying to define config env vars for my app which has access to process.env for local dev (ember serve), but falls back to some string config value (injected by CI build), for deployment to a server with pre-rendered static HTML (no node for process.env).

This new feature which allows me to define them in .ember-cli.js allows me to:

  • move all definitions and string replacement targets into one file (was previously replacing in every pre-rendered *.html)
  • detect and set use fallback when in CI

My config definitions are now much cleaner and more robust as a result and also improves my Cordova build pipeline :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants