Skip to content

Better environment handling - #619

Closed
mydea wants to merge 6 commits into
emberjs:masterfrom
mydea:better-env-handling
Closed

mydea wants to merge 6 commits into
emberjs:masterfrom
mydea:better-env-handling

Conversation

@mydea

@mydea mydea commented Apr 23, 2020

Copy link
Copy Markdown

@mehulkar

Copy link
Copy Markdown

I would like to have this also, but I also feel that environment-specific code tends to hint at other problems. E.g. a lot of Ember.testing branch code tends to be a last resort when animations or whatever cause tests to fail. The real problem is a test suite that doesn't accurately reflect the user experience, and I'd want to fix that. Dev specific code, in my experience is usually to configuration that can be done in config/environment.js or logging that can be handled by the existing debug function in @ember/debug. Maybe more pain points would be useful to see if there are other alternative solutions?

@Gaurav0

Gaurav0 commented Apr 24, 2020

Copy link
Copy Markdown
Contributor

@mydea I believe the currently recommended solution for determining if we are in testing is to use:

import config from 'my-application/config/environment';

// config contains the contents of ENV from your config/environment.js

const { environment } = config;
const isTesting = environment === 'test';

@mydea

mydea commented Apr 24, 2020

Copy link
Copy Markdown
Author

@mydea I believe the currently recommended solution for determining if we are in testing is to use:

import config from 'my-application/config/environment';

// config contains the contents of ENV from your config/environment.js

const { environment } = config;
const isTesting = environment === 'test';

Yeah, there are definitely ways, but it's not ideal, not necessarily easy to find for new developers, and harder to use e.g. from addons - there are also ways there, like getOwner(this).resolveRegistration('config:environment'), but it still feels like there should be an easier way to do this. I will add this to the text as alternatives as well.

@mydea

mydea commented Apr 24, 2020

Copy link
Copy Markdown
Author

I would like to have this also, but I also feel that environment-specific code tends to hint at other problems. E.g. a lot of Ember.testing branch code tends to be a last resort when animations or whatever cause tests to fail. The real problem is a test suite that doesn't accurately reflect the user experience, and I'd want to fix that. Dev specific code, in my experience is usually to configuration that can be done in config/environment.js or logging that can be handled by the existing debug function in @ember/debug. Maybe more pain points would be useful to see if there are other alternative solutions?

I will add some example use cases I personally came across to the motivation. Some I could think of are:

TESTING

  • Disabling polling code in tests
  • Preventing test-breaking code like window.reload()
  • Preventing log output in tests

DEBUG

  • Auto-signing in a mocked user in development environment

Overall, I am definitely positive that for all of these use cases, it is possible to find a decent workaround. However, I feel that a more agreed upon and documented solution for these is missing from the public API right now. I also think this is a need that most developers will stumble over at some point. Since I just searched for this in the last days I can attest to the fact that it is, as of now, not easily possible to find the "best"/recommended solution to these problems easily right now. I spent quite some time going through the API docs to search if, by chance, anything like that has been added (in the process, stumbling over the not-really-working anymore runInDebug()). There are multiple ways to somehow achieve these outcomes, but not really one well defined way.

@mydea
mydea force-pushed the better-env-handling branch from b128062 to 19f895e Compare April 24, 2020 06:26
@sandstrom

Copy link
Copy Markdown
Contributor

I also think what @Gaurav0 says makes sense.

If something would be added to Ember itself, perhaps there are other things we'd like to add too? For example the Ember version, or other things that are "locked in" at build-time. And have some shared name for that, e.g. import { version, environment, …etc } from '@ember/static' or whatever we'd like to call that module.

Also, why would you want to export both DEBUG and TESTING, wouldn't it be better to export the environment and then check env === 'development'?

@mehulkar

Copy link
Copy Markdown

@mydea the polling and window.reload examples are great. I really think that there should be better fixes for the root causes here. E.g. disabling polling should really happen by being able to register/inject dummy code (this is mostly possible today, but with caveats ember-learn/guides-source#1262). The reload example would be fixed by a test runner that runs in an iframe rather than in a different root div. I've had both of these problems and would love a real fix that moves ember application tests closer to selenium-like.

The dev example for logging in as a mocked user is also good, and I think that environment specific configuration for addons is the right approach in the long term.

Anyway, I totally agree that sooomething would be good, and this is a low enough level of effort to be justifiable. I'd just be worried about encouraging the antipattern of env specific code

@simonihmig

Copy link
Copy Markdown
Contributor

Anyway, I totally agree that sooomething would be good, and this is a low enough level of effort to be justifiable. I'd just be worried about encouraging the antipattern of env specific code

I agree with that, both with the concern, but also that we need something better than Ember.testing as an escape hatch. It has some considerable use today, but no proper import path suggested by RFC176, so should be addressed somehow.

Preventing test-breaking code like window.reload()

Shameless plug: https://github.com/kaliber5/ember-window-mock 😏

@mydea

mydea commented Apr 27, 2020

Copy link
Copy Markdown
Author

I basically agree with everything said here, for many things there are (or there should be) ways to handle them more gracefully, however there still needs to be an escape hatch (and be it just for addons to be able to provide these better abstractions).

And even for occurrences where using such flags might not be the ideal solution, it is probably still better to use an "approved" and clear solution than everybody using their own custom way to kind of get the same outcome.

@rwjblue rwjblue left a comment

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.

@mydea - Thanks for working on this! I'm keen to move forward here (and happy to champion this RFC as things progress).

Comment thread text/0000-better-env-handling.md Resolved
Comment thread text/0000-better-env-handling.md Outdated
import { TESTING } from "@ember/env";
```

This works like `Ember.testing`.

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 actually think this is a bit off from a design perspective:

  • Ember.testing cannot be checked in module scope, it is only set when starting an individual test and unset when the test is completed.
  • We want this value to mean something more akin to "this is a testing enabled build"

In other words, we do not want this to be the same as Ember.testing we want it to be:

  • constant (does not change / live update without a refresh)
  • true for all of the following:
    • ember test
    • ember test --environment=production
    • ember serve + /tests/
  • false for all of the following:
    • ember serve + any non /tests/ path
    • ember build --environment=production (non-testing related production build)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is the difference between this and environment === 'test' ?

@rwjblue rwjblue May 20, 2020 •

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 much from an applications perspective, but addons cannot use that style guard because they cannot import config from 'app-name-here/config/environment' (they have no clue what the app name they are consumed is is, and really shouldn't have to care).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, basically it is:

  1. import config from 'my-app/config/environment only works in host apps
  2. getOwner(this).resolveRegistration('config:environment') only works with access to the container
  3. Both of these are IMHO less explicit, also I always need to lookup the syntax for 2. as that is the only place I ever need it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We already have the ember-get-config addon.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes. However,

a) it feels weird to require an addon to solve this.
b) there is current Ember.testing which is much more convenient. However, it is not encouraged to use it anymore, without having a clear replacement.
c) Having a clear import for this is also less error prone & clearer in intent (e.g. consider config.environment === 'test' vs. config.environment === 'testing' - oops). It is easier to find and can be auto-completed by an editor.

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.

We already have the ember-get-config addon.

Sure, and that might be fine when you actually need the configuration, but determining when you should or should not include some test support code should be a built in concept.

Additionally, it is pretty important for these code paths to be stripped when TESTING is not true (e.g. ember build --environment="production"), that is not possible with ember-get-config.

Comment thread text/0000-better-env-handling.md Resolved
Comment thread text/0000-better-env-handling.md Resolved
Comment thread text/0000-better-env-handling.md Resolved
@rwjblue rwjblue self-assigned this May 20, 2020
@rwjblue rwjblue added T-testing T-Tooling RFCs that impact tooling, like Ember Inspector labels May 20, 2020
@mydea
mydea force-pushed the better-env-handling branch from 19f895e to dc1d01c Compare May 25, 2020 11:02
@mydea
mydea force-pushed the better-env-handling branch from dc1d01c to 1faed7a Compare May 25, 2020 11:03
@mydea

mydea commented May 25, 2020

Copy link
Copy Markdown
Author

I have updated the RFC content based on @rwjblue's feedback. It now proposes to just add an TESTING export to @glimmer/env, as well as adding the module to the API docs.

@rwjblue rwjblue left a comment

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.

Thanks @mydea! This looks great, I'll add to an upcoming meeting agenda to discuss with the rest of the team.

Comment thread text/0000-better-env-handling.md Resolved
Comment thread text/0000-better-env-handling.md Outdated

## Unresolved questions

Should we deprecate the older functionality immediately or leave a grace period?

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 deprecation immediately is probably fine, since the implementation of TESTING will work retroactively (basically anyone that can use ember-cli-babel@7 would be able to use it, regardless of ember-source version).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have updated this accordingly.

Finally, we should deprecate these older functions/modules:

- `Ember.testing`
- `runInDebug` from `@ember/debug`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please specifically mention these deprecations in the summary.

Please add a Transition Path section for these deprecations. It should include enough information to write a deprecation guide pull request to https://github.com/ember-learn/deprecation-app.

It should specifically include the code each of these is refactored to (I know this is obvious, but still should be included for the deprecation guide.)

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.

Please specifically mention these deprecations in the summary.

Deprecations aren't really a part of the summary for RFCs that are not explicitly deprecations RFCs. In this case, I believe they belong here in the detailed design.

Please add a Transition Path section for these deprecations. It should include enough information to write a deprecation guide pull request to ember-learn/deprecation-app.

It should specifically include the code each of these is refactored to (I know this is obvious, but still should be included for the deprecation guide.)

Ya, good call out.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I added a transition path for the deprecated functionality!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ember.testing is currently a wrapper for isTesting() and setTesting() from @ember/debug. Should those functions/exports also be affected?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

These are not public? They don't even show up as private here: https://api.emberjs.com/ember/release/classes/@ember%2Fdebug?show=inherited%2Cprotected%2Cprivate

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.

@cvx - I don't think we have module exports for those, that is just how Ember internally is organized. The deprecation here would be upon touching Ember.testing which means it would go into the getTesting method.

@ef4

ef4 commented May 29, 2020

Copy link
Copy Markdown
Contributor

We now have a merged RFC that contains a macro system that overlaps with the main idea in this proposal.

Environment checking really wants to compose with the macro system so you can strip out code per environment.

Plus the macro system is already implemented. Adding a new macro named like environment that evaluates to "development", "test", or "production" is a trivial extension to what we already have, and then it gives you everything in this RFC, plus build-time code-stripping.

So my suggestion is to reword this RFC so adds a new environment macro. Usage would look like:

import { environment } from "@ember/macros";
if (environment() === "test") {
  // ...
}

The type signature of environment would be:

function environment(): "development" | "test" | "production";

(Please note these three really are the only environments Ember has or has ever had. People sometimes think they can make their own extra environments, but that isn't really true, what they really want are separate ember-cli-deploy targets, but every target falls into one of these three environments.)

You could use environment() anywhere and get the actual value, or you can use it with the other macros to get guaranteed-build-time branch elimination:

import { macroCondition, environment } from '@ember/macros';
if (macroCondition(environment() === 'test')) {
  // ...
}

@ef4

ef4 commented May 29, 2020

Copy link
Copy Markdown
Contributor

After getting more detail from @rwjblue, my previous comment didn't account for the fact that this is trying to reduce the number of environments from three to two. That's great too, we can detangle "test vs not-test" from "dev vs prod" as separate questions, but I still advocate asking those questions via @ember/macros.

@rwjblue

rwjblue commented May 29, 2020

Copy link
Copy Markdown
Member

Ya, agree in general @ef4. I do also want to keep in mind (since I have to do a bit of work to support this) that we will want these same features in Glimmer.js / GlimmerX style apps, but that is a bit orthogonal.

@ef4

ef4 commented May 30, 2020

Copy link
Copy Markdown
Contributor

I would be fine with supporting the ember macros in glimmer apps too. They really don’t need much to integrate with a new build system (that is part of what they’re for).

@mydea

mydea commented Jun 2, 2020

Copy link
Copy Markdown
Author

(Please note these three really are the only environments Ember has or has ever had. People sometimes think they can make their own extra environments, but that isn't really true, what they really want are separate ember-cli-deploy targets, but every target falls into one of these three environments.)

I mean, I get the point that this is how it should be, but I've worked on apps that actually had more environments, and it all worked just fine (with some adjustments in the ember-cli-build.js). So I am not sure if I would agree with that statement - it is perfectly possible to set any other environment. It might make sense to actually deprecate that at some point (allowing to use a different environment), but I guess that's a different issue.

Personally, I don't really care if we import this from @ember/macrosor from @glimmer/debug - I will defer to you as you have way more insight into this than I have. However, I personally would definitely still prefer to have boolean exports/functions for this, than to have to do string comparisons. But I guess we could also have e.g.:

import { isDebug, isTesting } from '@ember/macros`

@mydea

mydea commented Jun 15, 2020

Copy link
Copy Markdown
Author

Sidenote, I just ran into this case when trying to use @embroider/macros to do some checks.

Basically, setting a macro value for tests will not work like this:

module.exports = function (defaults) {
  let env = process.env.EMBER_ENV;

  let app = new EmberApp(defaults, {
    '@embroider/macros': {
      setOwnConfig: {
        mockApi: env === 'test'
      },
    },
}

As this will be false when running ember s and visiting localhost:4200/tests.
So in order for us to be able to strip/include stuff for the test environment with the macro system, the changes proposed here are necessary.

@ef4

ef4 commented Jun 15, 2020

Copy link
Copy Markdown
Contributor

@mydea your example is nearly enough to make that scenario work. What's missing is that, when you run ember s the macros are left un-evaluated until runtime, so there's actually another opportunity to set the testing flag to true right as the test suite is about to start.

I haven't opened this up as a public API yet because I was letting it get stabilized first, but we already do this for fastboot.isRunning here:

https://github.com/embroider-build/embroider/blob/2a9802c8a99041218ac54b4d43fd82de8b61625c/packages/core/src/app.ts#L716-L720

We will do the same for an isTesting flag.

@mydea

mydea commented Jun 15, 2020

Copy link
Copy Markdown
Author

So, if this is being implemented in @embroider/macros, do we need an/this RFC? Or how does that work then?

@mydea

mydea commented Aug 10, 2020

Copy link
Copy Markdown
Author

I just saw that this is now available in the latest release of @embroider/macros. Usage:

import { isTesting, isDevelopingApp, macroCondition } from '@embroider/macros';

function myFunction() {
  if (macroCondition(isTesting())) {
    // do something test specific
  }

  if (macroCondition(isDevelopingApp())) {
    // do something development specific
  }
}

This works nicely, as far as I can see, and is properly tree-shaked. So I guess all that remains is potentially some documentation, but that affects all of @embroider/macros I guess..!

Should I close this RFC, or leave it open until the question of documentation is resolved?

@mydea

mydea commented Nov 24, 2020

Copy link
Copy Markdown
Author

I think we can close this, as this can be pretty nicely accomplished now using @embroider/macros:

import { isTesting, isDevelopingApp } from '@embroider/macros';

Works like a charm in our app, and can properly tree-shake code when used with macroCondition 👍

@mydea mydea closed this Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-testing T-Tooling RFCs that impact tooling, like Ember Inspector

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants