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

Expo release build requires .env.production and ignores .env #395

Closed
devYonz opened this issue Dec 20, 2022 · 11 comments
Closed

Expo release build requires .env.production and ignores .env #395

devYonz opened this issue Dec 20, 2022 · 11 comments
Assignees

Comments

@devYonz
Copy link

devYonz commented Dec 20, 2022

Describe the bug
My Expo application OAuth functionality started failing in release mode (eas build). After debugging for a while, I saw that a variable that is resolved from .env is no longer being captured. After adding a new file .env.production with the save value the functionality started working. The .env file should be able to work for both development and production NODE_ENV values.

If requested I can whip up a bare application and

To Reproduce
Steps to reproduce the behavior:

  1. Setup a project with a value in .env
  2. eas build --local --profile preview // Preview has all the settings of release and has apk.
  3. adb install <build.apk>
  4. Importing the variable should fail.

Expected behavior
.env file contents should be picked up and if there is an env.production file it should override those values.

Screenshots
If applicable, add screenshots to help explain your problem.

Smartphone (please complete the following information):

  • Device: Android and Iphone device tested

Additional context
Add any other context about the problem here.

babel.config.js:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      ["module:react-native-dotenv"],
      ["transform-remove-console", { "exclude": ["error", "warn"] }]
    ]
  };
};

eas.json

{
  "cli": {
    "version": ">= 2.8.0"
  },
  "build": {
    "development": {
      "channel": "development",
      "distribution": "internal",
      "developmentClient": true,
      "android": {
        "buildType": "apk"
      },
      "ios": {
        "simulator": true
      }
    },
    "preview": {
      "distribution": "internal",
      "developmentClient": false,
      "channel": "development",
      "android": {
        "buildType": "apk",
        "gradleCommand": ":app:assembleRelease"
      },
      "ios": {
        "simulator": true
      }
    },
    "production": {
      "distribution": "store",
      "channel": "stable",
      "android": {
        "buildType": "app-bundle"
      }
    }
  },
  "submit": {
    "production": {}
  }
}
@github-actions
Copy link

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/395

@goatandsheep
Copy link
Owner

api.cache(false) or upgrade to latest babel

@devYonz
Copy link
Author

devYonz commented Dec 20, 2022

How does the cache fix the issue? can you give me more info ?

    "@babel/core": "^7.20.2",
    "babel-plugin-transform-inline-environment-variables": "^0.4.4",    
    "react-native-dotenv": "^3.4.6",

@goatandsheep
Copy link
Owner

goatandsheep commented Dec 21, 2022

This library is a Babel plugin. Babel caches all non JavaScript files that are read including .env files by default

Earlier this year, I worked with the Babel team to add a feature to make exceptions. This feature was integrated, tested, and works well. Expo has not upgraded to the latest version despite my pleas and pull requests

@devYonz
Copy link
Author

devYonz commented Dec 21, 2022

I can work on reproducing this on a fresh expo project next week but incase you missed my problem, I suspect that release builds will never pickup variables in .env file. Are you confident that .env will be grabbed in production mode? I already have the latest version of the plugin and this package.

@goatandsheep
Copy link
Owner

@devYonz
Copy link
Author

devYonz commented Dec 22, 2022

By comparing delta with this project, I have found that in react-native-dotenv can pick up my local .env file when I use npx expo start but fails to do so when I run eas buid. Note that the test project is working fine (both eas build and expo start successfully pickup the file). So the issue is not about picking up .env.production but about picking up any env files when running with eas build.

As far as I know my .env is placed correctly: I have src/app/ directory structure and .env is at the root of the project (i.e at the same level as src folder)

Can you think of a reason why this might be? How does the library infer the root path? Would setting path='./' help?

@goatandsheep
Copy link
Owner

I'd recommend putting your secrets on EAS through the online portal

@devYonz
Copy link
Author

devYonz commented Dec 22, 2022

Any other suggestions? I would prefer to use .env. Here is the log output of the failure

[RUN_GRADLEW] transform[stderr]: react-native-dotenv Error: ENOENT: no such file or directory, open '.env'
[RUN_GRADLEW] transform[stderr]:     at Object.openSync (node:fs:590:3)
[RUN_GRADLEW] transform[stderr]:     at readFileSync (node:fs:458:35)
[RUN_GRADLEW] transform[stderr]:     at parseDotenvFile (/private/var/folders/12/jzrnnf0s6yzdp4f7gh1by3600000gn/T/eas-build-local-nodejs/692b0e7a-8b8c-4676-9cc9-e9a1f9b8d18d/build/spark-mobile/node_modules/react-native-dotenv/index.js:9:15)
[RUN_GRADLEW] transform[stderr]:     at module.exports (/private/var/folders/12/jzrnnf0s6yzdp4f7gh1by3600000gn/T/eas-build-local-nodejs/692b0e7a-8b8c-4676-9cc9-e9a1f9b8d18d/build/spark-mobile/node_modules/react-native-dotenv/index.js:89:18)
[RUN_GRADLEW] transform[stderr]:     at sync (/private/var/folders/12/jzrnnf0s6yzdp4f7gh1by3600000gn/T/eas-build-local-nodejs/692b0e7a-8b8c-4676-9cc9-e9a1f9b8d18d/build/spark-mobile/node_modules/@babel/core/lib/gensync-utils/async.js:33:25)
[RUN_GRADLEW] transform[stderr]:     at sync (/private/var/folders/12/jzrnnf0s6yzdp4f7gh1by3600000gn/T/eas-build-local-nodejs/692b0e7a-8b8c-4676-9cc9-e9a1f9b8d18d/build/spark-mobile/node_modules/gensync/index.js:182:19)
[RUN_GRADLEW] transform[stderr]:     at /private/var/folders/12/jzrnnf0s6yzdp4f7gh1by3600000gn/T/eas-build-local-nodejs/692b0e7a-8b8c-4676-9cc9-e9a1f9b8d18d/build/spark-mobile/node_modules/gensync/index.js:210:24

@goatandsheep
Copy link
Owner

I highly recommend against that because it is insecure but if you insist, then remove .env from your gitignore

@goatandsheep
Copy link
Owner

Feel free to continue the discussion here #388

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

No branches or pull requests

2 participants