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

.env is not being loaded on eas update #2174

Closed
matart15 opened this issue Jan 11, 2024 · 15 comments
Closed

.env is not being loaded on eas update #2174

matart15 opened this issue Jan 11, 2024 · 15 comments
Assignees
Labels
needs review Issue is ready to be reviewed by a maintainer

Comments

@matart15
Copy link

matart15 commented Jan 11, 2024

Build/Submit details page URL

No response

Summary

I have .env file. it is working without problem on expo dev.

Screenshot 2024-01-11 at 11 10 55

But when i use expo update

Screenshot 2024-01-07 at 15 02 03

Managed or bare?

managed

Environment

Need to install the following packages:
[email protected]
Ok to proceed? (y)

expo-env-info 1.0.5 environment info:
System:
OS: macOS 14.1.2
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
npm: 8.19.4 - ~/workplace/github/.../node_modules/.bin/npm
Managers:
CocoaPods: 1.11.3 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 23.2, iOS 17.2, macOS 14.2, tvOS 17.2, watchOS 10.2
IDEs:
Android Studio: 2021.3 AI-213.7172.25.2113.9123335
Xcode: 15.1/15C65 - /usr/bin/xcodebuild
Expo Workflow: managed

npx expo-env-info 5.87s user 2.57s system 62% cpu 13.478 total

Error output

No response

Reproducible demo or steps to reproduce from a blank project

  1. create project
  2. set secrets
  3. create eas update
  4. add some code to see process.env ( if my understanding is correct secrets should be there )
<Pressable
              onPress={() => {
                Alert.alert("", JSON.stringify(process.env));
              }}
            >
  1. update pnpx eas-cli update --branch preview --message "update try"
@matart15 matart15 added the needs review Issue is ready to be reviewed by a maintainer label Jan 11, 2024
@szdziedzic
Copy link
Member

What Expo SDK version are you using?

@matart15
Copy link
Author

matart15 commented Jan 12, 2024

package.json

 "expo": "49.0.18",

pnpm expo -v

0.10.15

@szdziedzic
Copy link
Member

Looking at your screenshot from #2170, I see a warning about EAS CLI being installed as your project dependency. Can you remove it from your project dependencies and install the latest version globally?

@matart15
Copy link
Author

ok. removed it from package.json
image

then tried pnpx eas-cli update --branch preview --message "update try"

results are same.

Even if i try NODE_ENV=preview; pnpx eas-cli update --branch preview --message "update try"

it shows only { NODE_ENV: "production" }.

@matart15
Copy link
Author

Just in case :

❯ pnpx eas-cli -v
eas-cli/6.0.0 darwin-x64 node-v20.10.0
pnpx eas-cli -v  5.15s user 3.99s system 122% cpu 7.489 total

@quinlanj quinlanj self-assigned this Jan 12, 2024
@brentvatne
Copy link
Member

EAS Update does not pull in values from secrets. Secrets are not intended for updates! Secrets are values that will be present on an EAS Build worker temporarily to hold values like sensitive API keys to upload sourcemaps to Sentry and such things. You do not want to include these values in your update.

@matart15
Copy link
Author

matart15 commented Jan 17, 2024

@brentvatne

One thing I don't understand is

If i write app that uses process.env.MY_SECRET_API_KEY to access endpoints and eas build it will work.

Then I found small problem and runs eas update my app return undefined on process.env.MY_SECRET_API_KEY and breaks? doesn't this make eas update meaningless?

@quinlanj
Copy link
Member

quinlanj commented Jan 19, 2024

hey @matart15 , the problem is the way you are calling the environment variable. When eas update is run, we inline all your env var declarations from .env into the bundle. For example, if your code references process.env.EXPO_PUBLIC_TEST, we replace it with the value referenced in your .env file. This is why when you print out process.env, you'll get an empty object. But if you reference it using JavaScript's dot notation like process.env.EXPO_PUBLIC_TEST, you should see be able to see it.

Here are more details: https://docs.expo.dev/guides/environment-variables/#how-to-read-from-environment-variables

Note: I just tested this behaviour in SDK 50 to make sure it still works, but i see you are on SDK 49. If you are still getting the same issue, please let us know!

@matart15
Copy link
Author

matart15 commented Jan 20, 2024

Woow.
I missed this line on the docs.

Alternative versions of the expression are not supported. For example, process.env['EXPO_PUBLIC_KEY'] or const {EXPO_PUBLIC_X} = process.env is invalid and will not be inlined.

Typescript linter gives me error if i use dot notation.
I ignore linter and try it again with dot notation

@MariuzM
Copy link

MariuzM commented Mar 26, 2024

I'm testing this now and also noticed that my EXPO_PUBLIC_API_URL=https://prod.api that was built with eas production was replaced with my local .env that i use EXPO_PUBLIC_API_URL=http://localhost:3000

I could see this as a problem when lets say i find a bug and i want to fix it fast but i forget to change .env file from Dev to Prod. so that means Dev URL will be pushed.

Also another issue, even if i pushed .env file with prod url i was getting issues still tho on main screen i have it printed and it does show the prod url but issue is that its not able to connect, i can't debug the production build but maybe i need create a simple example to test.

@MariuzM
Copy link

MariuzM commented Mar 26, 2024

I think i found solution

  1. Create .env.production file
  2. Only use the PROD public env's
  3. Run update like so NODE_ENV=production eas update --branch production --message "test 1" -p ios

Update: nope still having this weird issue where i can't make API requests even tho variable is PROD, doing build works fine, only update that is problem

@MariuzM
Copy link

MariuzM commented Mar 26, 2024

Update: there is some issue with Axios.

image
  • It started working, so ok i then made a build and pushed to app store, updated my phone with that all good.
  • Then i made expo update and broke again.
  • Made another expo update still nothing
  • Ok now i added same change (Alert) boom started working...
  • Removed that Alert and done Update all good still working...
  • But I'm sure if i do production build and push to test flight on the next expo update it will break i have no idea why, i feel like maybe when doing Expo Update, Axios needs to have some sort of Native component inside like this Alert? I just can't explain this sorry

My expo is configured like this
image

and then just couple interceptors but they don't do much

@redkean
Copy link

redkean commented Apr 13, 2024

Update: there is some issue with Axios.

image * It started working, so ok i then made a build and pushed to app store, updated my phone with that all good. * Then i made expo update and broke again. * Made another expo update still nothing * Ok now i added same change (Alert) boom started working... * Removed that Alert and done Update all good still working... * But I'm sure if i do production build and push to test flight on the next expo update it will break i have no idea why, i feel like maybe when doing Expo Update, Axios needs to have some sort of Native component inside like this Alert? I just can't explain this sorry

My expo is configured like this image

and then just couple interceptors but they don't do much

I'm experiencing this same issue, you can also replicate this in a local app dev (prebuild) and expo go.

In my case, we have 2 separate API url for prod and dev, if I try to run prod by adding APP_ENV=production at the start of dev server, the app loads contents from dev API. Then I tried adding console logs in my axios.ts file and it now suddenly works.

This could probably be a problem in axios. I also can't explain why.

UPDATE:

This is not a problem with axios, it's the way expo loads env variables. You need to add --clear-cache command when running eas update

@evansendra
Copy link

The --clear-cache was a lifesaver. Wasn't able to figure out why updating an EXPO_PUBLIC_ environment variable in the relevant .env file wouldn't update after pushing an update, even after trying to push a small code change as well to attempt to force the change recognition. Would it make sense to include a one liner about this in https://docs.expo.dev/eas-update/environment-variables/?

@ahmed-khlifi
Copy link

--clear-cache solved the issue, and i think it should be better for expo update to use the variables set in the production profile or in secrets instead of going with .env direclty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review Issue is ready to be reviewed by a maintainer
Projects
None yet
Development

No branches or pull requests

8 participants