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

ENVFILE=.env.xxx has no effect on iOS unless also touching BuildDotenvConfig.rb #698

Open
smallsaucepan opened this issue Nov 10, 2022 · 20 comments

Comments

@smallsaucepan
Copy link

Using react-native-config from the command line with the ENVFILE=.env.xxx syntax doesn't change the config available in the app. The workaround listed in #692 fixes the problem, though that issue suggests the workaround hasn't always been required.

Haven't done any setup in Xcode with schemes or anything. Just trying it out via the command line. From looking at the documentation this should work, so raising an issue.

Example commands used (Mac shell):

ENVFILE=.env.xxx npx react-native run-ios
(wrong config in app)
touch node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb
ENVFILE=.env.xxx npx react-native run-ios
(correct config in app)

Notable versions in the environment are:

react-native 0.69.5
react-native-config 1.4.11

Happy to provide any other details if useful.

@justin-tay
Copy link

This is actually because of the 0ac5511 commit which added output_files to the podspec. A side-effect of not having the output_files is that the BuildDotenvConfig.rb script would be run on all builds (including incremental builds). Having the output_files set meant that XCode would do dependency analysis to determine when it should rebuild and trigger the script which means that it's only going to run if it detects BuildDotenvConfig.rb changing.

Note that a similar regression has been fixed once before in #458.

I think the tricky part of solving this is in keeping everyone with all the different setups happy because that commit was due to issue #683 where they are still using GeneratedInfoPlistDotEnv.h preprocessor method instead of the xcconfig method for modifying the Info.plist.

@philippeauriach
Copy link

Adding $ENVFILE in the "input_files" worked for me, and should not trigger a build unless the .env file changed :

diff --git a/node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m b/node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m
new file mode 100644
index 0000000..04a2f3d
--- /dev/null
+++ b/node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m
@@ -0,0 +1 @@
+  #define DOT_ENV @{  };
diff --git a/node_modules/react-native-config/react-native-config.podspec b/node_modules/react-native-config/react-native-config.podspec
index 854bfe7..7d17275 100644
--- a/node_modules/react-native-config/react-native-config.podspec
+++ b/node_modules/react-native-config/react-native-config.podspec
@@ -25,7 +25,7 @@ HOST_PATH="$SRCROOT/../.."
 "${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig/BuildDotenvConfig.rb" "$HOST_PATH" "${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig"
 ),
     execution_position: :before_compile,
-    input_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/BuildDotenvConfig.rb'],
+    input_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/BuildDotenvConfig.rb', '$ENVFILE'],
     output_files: ['$BUILD_DIR/GeneratedInfoPlistDotEnv.h']
   }
 

@cnixbtc
Copy link

cnixbtc commented Dec 5, 2022

@justin-tay The workaround with touching the BuildDotenvConfig.rb file does work -- even if it feels a bit awkward to do this every time. I'm guessing that eventually this issue will be resolved and the workaround not needed anymore. However, until then it should probably be mention in the readme to make it obvious for anyone from the get go. What do you think?

@quiringk
Copy link

quiringk commented Jan 24, 2023

Just wanted to chime in here. I went through tutorials (https://www.youtube.com/watch?v=rhdOWYqc-Cg&t=1551s&ab_channel=JASACADAMY) and set up schemes + targets for my different envs. I was running into the issue where the .env would get overwritten successfully by the correct .env.[environment] file but the actual variables in the code weren't changing (when you used them). Occasionally they would change after multiple clear builds, etc but even then there was no consistency and most of the time that didn't work. Eventually I added everything I could find in the issues here + deleting the builds on the simulator so that they had to be reinstalled and all the sudden switching back and forth between schemes started working and changing the variables every time. So here is what my pre-actions script looks like for each environment:

cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env"

echo “.env.dev” > /tmp/envfile

touch "${PROJECT_DIR}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb"

@marquina-abreu
Copy link

Thank you very much bro @quiringk I also had the same problem you had... I changed schemas but kept taking another .env that was not the one that corresponded... and with your solution with those two more lines of code, it worked for me too!

@lucaslucenagithub
Copy link

lucaslucenagithub commented Feb 24, 2023

Just wanted to chime in here. I went through tutorials (https://www.youtube.com/watch?v=rhdOWYqc-Cg&t=1551s&ab_channel=JASACADAMY) and set up schemes + targets for my different envs. I was running into the issue where the .env would get overwritten successfully by the correct .env.[environment] file but the actual variables in the code weren't changing (when you used them). Occasionally they would change after multiple clear builds, etc but even then there was no consistency and most of the time that didn't work. Eventually I added everything I could find in the issues here + deleting the builds on the simulator so that they had to be reinstalled and all the sudden switching back and forth between schemes started working and changing the variables every time. So here is what my pre-actions script looks like for each environment:

cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env"

echo “.env.dev” > /tmp/envfile

touch "${PROJECT_DIR}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb"

Totatly worked for me man, thanks! Thats what my code looks like now:

"scripts": {
		"android:staging": "cross-env ENVFILE=.env.staging react-native run-android",
		"ios:staging": "cp .env.staging .env && echo \".env.staging\" > /tmp/envfile && touch \"node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb\" && cross-env ENVFILE=.env.staging react-native run-ios",
		"android:production": "cross-env ENVFILE=.env.production react-native run-android",
		"ios:production": "cp .env.production .env && echo \".env.production\" > /tmp/envfile && touch \"node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb\" && cross-env ENVFILE=.env.production react-native run-ios",
		"bundle:staging": "cross-env ENVFILE=.env.staging react-native bundle",
		"bundle:production": "cross-env ENVFILE=.env.production react-native bundle",
		"test": "jest",
		"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
		"pod:install": "npx pod-install ios",
		"android:build": "cd ./android && cross-env ENVFILE=.env.production ./gradlew clean && ./gradlew assembleRelease",
		"android:bundle": "cd ./android && cross-env ENVFILE=.env.production ./gradlew clean && ./gradlew bundleRelease"
	},

@LeQuy-123
Copy link

Just wanted to chime in here. I went through tutorials (https://www.youtube.com/watch?v=rhdOWYqc-Cg&t=1551s&ab_channel=JASACADAMY) and set up schemes + targets for my different envs. I was running into the issue where the .env would get overwritten successfully by the correct .env.[environment] file but the actual variables in the code weren't changing (when you used them). Occasionally they would change after multiple clear builds, etc but even then there was no consistency and most of the time that didn't work. Eventually I added everything I could find in the issues here + deleting the builds on the simulator so that they had to be reinstalled and all the sudden switching back and forth between schemes started working and changing the variables every time. So here is what my pre-actions script looks like for each environment:

cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env"

echo “.env.dev” > /tmp/envfile

touch "${PROJECT_DIR}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb"

It work
I think dev team should update readme file base on this

@AhteshamKhanCoder
Copy link
Contributor

AhteshamKhanCoder commented May 11, 2023

@LeQuy-123
Try using this on your package.json

"scripts": {
    ... 
    "runDev": "react-native run-ios --scheme your_dev_scheme_name ",
    "runStage": "react-native run-ios --scheme your_stage_scheme_name",
    "runProd": "react-native run-ios --scheme your_prod_scheme_name",
 }

@billnbell
Copy link
Contributor

billnbell commented Jun 10, 2023

I put this in my "clean" package.json command:

rm -f node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m

And it works for IOS - what about Android?

@AhteshamKhanCoder
Copy link
Contributor

AhteshamKhanCoder commented Jun 10, 2023 via email

@ilyakar
Copy link

ilyakar commented Jun 29, 2023

Just wanted to chime in here. I went through tutorials (https://www.youtube.com/watch?v=rhdOWYqc-Cg&t=1551s&ab_channel=JASACADAMY) and set up schemes + targets for my different envs. I was running into the issue where the .env would get overwritten successfully by the correct .env.[environment] file but the actual variables in the code weren't changing (when you used them). Occasionally they would change after multiple clear builds, etc but even then there was no consistency and most of the time that didn't work. Eventually I added everything I could find in the issues here + deleting the builds on the simulator so that they had to be reinstalled and all the sudden switching back and forth between schemes started working and changing the variables every time. So here is what my pre-actions script looks like for each environment:

cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env"

echo “.env.dev” > /tmp/envfile

touch "${PROJECT_DIR}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb"

Overall, this was indeed the temporary workaround! But, I'm just sharing a cleaner code, since a part here is redundant, and quotes in the original answer used which will cause a failure. This script should work for everyone:

// cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env" <- Not necessary
echo ".env.dev" > /tmp/envfile

touch "${PROJECT_DIR}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb" // <- This is the hack that fixes the issue.

@Biplovkumar
Copy link

rm -f node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m

It is a temporary fix for env variable. But it needs to run every time. Which is not good.

@manikandanb24
Copy link

@LeQuy-123 Try using this on your package.json

"scripts": {
    ... 
    "runDev": "react-native run-ios --scheme your_dev_scheme_name ",
    "runStage": "react-native run-ios --scheme your_stage_scheme_name",
    "runProd": "react-native run-ios --scheme your_prod_scheme_name",
 }
"runDev": "react-native run-ios --scheme 'your_dev_scheme_name'",

Worked with adding the schema name within single quotes.

@ToshKoevoets
Copy link

ToshKoevoets commented Aug 26, 2023

cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env"

echo “.env.dev” > /tmp/envfile

touch "${PROJECT_DIR}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb"

This is pretty complicated, why not the basic overwrite .env then? Something like this?

cp .env.development .env && react-native run-ios
"scripts": {
    ... 
    "dev": "cp .env.development .env &&  react-native run-ios ",
    "prod": "cp .env.prod .env &&  react-native run-ios",
    "client1": "cp .env.client1 .env &&  react-native run-ios",
 }

Would be great if there is a stable fix possible; but the cp is workable

@Biplovkumar
Copy link

I put this in my "clean" package.json command:

rm -f node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m

And it works for IOS - what about Android?

this is working fine but it is not a good solution. cause of need to delete every time. Without deleting .env not updating in ios. Please resolve.

@Biplovkumar
Copy link

Please fix this issue (.env not updating without deleting GeneratedDotEnv.m) and Update it in to latest version.

@qretsar
Copy link

qretsar commented Sep 24, 2023

For me, the issue started happening after I wanted to change something and I followed the tutorial from: https://www.youtube.com/watch?v=rhdOWYqc-Cg&t=1551s&ab_channel=JASACADAMY

  • Before the tutorial, I was just using this line in build preactions :
    cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env"
    and it worked like a charm
  • https://youtu.be/rhdOWYqc-Cg?t=1162 (timestamp) > This is what I added and afterward, I couldn't change the env file.

Solution, as someone mentioned above
go to Scheme > Edit Scheme > Build > Pre-actions > + New Script Run Action and create 3 run scripts and put the code below according to the script number.

  1. cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env"
  2. echo “.env.dev” > /tmp/envfile
  3. touch "${PROJECT_DIR}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb"

@jliukai
Copy link

jliukai commented Oct 11, 2023

how did you get below to work?

cp "${PROJECT_DIR}/../.env.prod" "${PROJECT_DIR}/../.env"
echo “.env.prod” > /tmp/envfile
touch "${PROJECT_DIR}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb"

My folder structure
myproject/.env.prod
myproject/node_modules
myproject/ios/podfile

I get an error
cp: /../.env.prod: No such file or directory but my .env.prod is there.

i ended up downgrading to 0.4.5 and did none of the above. it works.

@shkatulo
Copy link

@jliukai having the same issue. Seems like PROJECT_DIR is empty in pre-build script.

@shkatulo
Copy link

I ended up using this script in pre-build phases for each scheme:

rm /tmp/envfile
cp "${WORKSPACE_PATH}/../../.env.development" "${WORKSPACE_PATH}/../../.env"
rm -f "${WORKSPACE_PATH}/../../node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m"

In some reason $PROJECT_DIR is empty in pre-build action, but $WORKSPACE_PATH worked well for me.

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