goatandsheep/react-native-dotenv

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

devYonz opened this issue · 11 comments

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": {}
  }
}

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

api.cache(false) or upgrade to latest babel

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",

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

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.

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?

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

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

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

Feel free to continue the discussion here #388