Skip to content
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

AOT - incorrect environment #2673

Closed
deebloo opened this issue Oct 12, 2016 · 14 comments
Closed

AOT - incorrect environment #2673

deebloo opened this issue Oct 12, 2016 · 14 comments
Assignees
Labels
P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful type: bug/fix

Comments

@deebloo
Copy link
Contributor

deebloo commented Oct 12, 2016

OS?

OSX El Capitan

Versions.

angular-cli: 1.0.0-beta.17
node: 6.7.0
os: darwin x64

Repro steps.

-- create a new app with ng new
-- in src/app/app.module.ts

import { environment } from '../environments/environment';

-- then in the NgModule add

@NgModule({
  imports: [
    ...
  ],
  declarations: [
    ...
  ],
  providers: [{ provide: AppConfig, useValue: environment }],
  bootstrap: [AppComponent]
})
export class AppModule { }

-- run ng build --prod --aot

If you inject AppConfig into a component or service you will get the incorrect environment.
I am pretty sure aot is breaking the environment switching. when statically analyzing the app module aot pulls in the initial config if passing it to module. The only way I have managed to work around is to copy the file with a script before build.

@deebloo deebloo changed the title AOT - environment AOT - incorrect environment Oct 12, 2016
@deebloo
Copy link
Contributor Author

deebloo commented Oct 13, 2016

for those needing a workaround you can use a script to copy the file to environment.ts BEFORE running build and aot. This is the one I use on my CI server.

echo "HACK!!!"
__dirname="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cp $__dirname/src/environments/environment.$1.ts $__dirname/src/environments/environment.ts
echo "Copied environment.$2.ts"

@filipesilva filipesilva added type: bug/fix command: build P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful labels Oct 14, 2016
@SimonKunz
Copy link

I can confirm this behavior. It only occurs when building with the --aot flag.

@a-legrand
Copy link

I try this, with success !

export function env() {
    return environment;
}

@NgModule({
  imports: [
    ...
  ],
  declarations: [
    ...
  ],
  providers: [{ provide: AppConfig, useFactory: env }],
  bootstrap: [AppComponent]
})
export class AppModule { }

@deebloo
Copy link
Contributor Author

deebloo commented Dec 12, 2016

That is a much better workaround then what I was doing! Solid idea. Totally makes sense as well.

@cortopy
Copy link

cortopy commented Dec 12, 2016

Experiencing same behaviour, except that workaround only works if AppConfig is within the environment file. Conditionally adding config within other files will not work

@dabele91
Copy link

dabele91 commented Jan 2, 2017

Same issue here. I try to get my firebase config from the environment, but i always receive the default environment file.

@deebloo
Copy link
Contributor Author

deebloo commented Jan 2, 2017

Like @a-legrand mentioned. A workaround right now is to useFactory instead of useValue

@Toxicable
Copy link

Also experiencing this issue, attempted to use a factory but for some reason the result of the factory wasn't being injected, instead the actual function was. I was most likely doing something wrong but this issue is still the root of it.

@dmattsson
Copy link

dmattsson commented Jan 16, 2017

Just want to bump this. I added --aot to our prod build and thus exposed our DEV database to the public. Thankfully there was no big harm this time, but this could potentially have some really serious implications!

@filipesilva
Copy link
Contributor

@deebloo I'm having some trouble reproducing this. Can you provide me with a repo where this happens, or tell me what's the definition of AppConfig in your example?

@deebloo
Copy link
Contributor Author

deebloo commented Feb 6, 2017

@filipesilva ill create a small repo with the latest cli

@deebloo
Copy link
Contributor Author

deebloo commented Feb 6, 2017

@filipesilva https://github.com/deebloo/env-error if you look at the console. "production" from directly importing the environment file will be true. "production" when grabbing it from injected service will always be false.

@filipesilva
Copy link
Contributor

filipesilva commented Feb 9, 2017

Fixed by #4475

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful type: bug/fix
Projects
None yet
Development

No branches or pull requests

8 participants