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

Modified variables are not updated after rebuilding #591

Closed
mickael-h opened this issue Jun 27, 2021 · 57 comments
Closed

Modified variables are not updated after rebuilding #591

mickael-h opened this issue Jun 27, 2021 · 57 comments

Comments

@mickael-h
Copy link

mickael-h commented Jun 27, 2021

I keep changing variable values in the .env file, but recompiling the app doesn't change anything (at least on iOS)
I tried cleaning every cache I could think of, but the config values are still the same as the first build.
Could you give instructions in order to clean the .env cache?

@VinitSarvade
Copy link

@mickael-h Faced the same issue, fixed it by clearing the xcodederiveddata in your system.

@hke-ghtest2
Copy link

Faced the same issue, every time need to manually remove node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m.

Any updates on the handling?

@pvroosendaal
Copy link

pvroosendaal commented Nov 4, 2022

Just removing the GeneratedDotEnv.m file didn't help me, that caused a compile error, because the import then fails in ReactNativeConfig.m, line 2. Running "Clean Build Folder" in Xcode works for me. This ensures the '[CP-User] Config codegen' build phase script in the Pods project is run again and the content of GeneratedDotEnv.m is regenerated.

@hke-ghtest2
Copy link

@pvroosendaal , thanks for that update.
Yes Running "Clean Build Folder" in Xcode also works for me .
I think this is the better way for refreshing the cached content.

@billnbell
Copy link
Contributor

how about another solution ?

@billnbell
Copy link
Contributor

billnbell commented Nov 13, 2022

#700

@Stas-Buzunko
Copy link

running Running "Clean Build Folder" in Xcode does work.
But it's ridiculous we have to Clean build folder each time (we pass commit hash on build so env changes on each deployment) + ENVFILE path is ignored and we have to copy env file we need to .env. This library has ~250 opened issues, no idea how it's still the most popular lib for handling env variables...

i think it's time to switch to another library as we have wasted too much time on this one. Going to try react-native-dotenv, i don't think it could be worse than this one 🤣

@luancurti
Copy link
Collaborator

Fixed in 1.4.12

@robsonos
Copy link

Just tested here and it still does not update environment variables after rebuilding the code

@alessioemireni
Copy link

alessioemireni commented Jan 12, 2023

Same here. We need to reopen the issue.
The error occurs the second time. Example:
ENVFILE=.env.production yarn ios --> OK
ENVFILE=.env yarn ios --> KO
Then I retry but no success. Seems that the script works only the first time of app lunch.

@rivaros
Copy link

rivaros commented Jan 20, 2023

@luancurti We are verry sorry, but it does not work with 1.4.12. :) Pls reopen the issue.

@dks333
Copy link

dks333 commented Jan 28, 2023

same, not working with 1.4.6

@marcoj
Copy link

marcoj commented Feb 3, 2023

Same in 1.5.0

@TwistedMinda
Copy link

Same in 1.4.5

@billnbell
Copy link
Contributor

I just delete Pods and rerun yarn ios and it seems to always work for me.

pod deintegrate && pod install

@TwistedMinda
Copy link

TwistedMinda commented Feb 14, 2023

I just delete Pods and rerun yarn ios and it seems to always work for me.

pod deintegrate && pod install

Perfect dude, thank you! This is the quickest! Not even a yarn start --reset-cache is needed
But of course, a yarn ios to rebuild with the new Pods is required

PS: You don't need pod deintegrate either

@lclarkg18
Copy link

Same issue, pod install is not fixing it consistently though.

@chdzma
Copy link

chdzma commented Feb 21, 2023

Same with the last version, ios is cached all the time

@billnbell
Copy link
Contributor

what canw e do to fix this?

@nghiaklabs
Copy link

Fixed in 1.4.12

It's not working

@gCardinal
Copy link

gCardinal commented Mar 8, 2023

Adding my voice to this issue. At least on iOS, this isn't currently working.

Edit: I was wrong, it is working. Make sure the path to your .env file is an absolute path, not a relative one. I also need to clean the project's build with XCode, but otherwise, flawless.

@engineerStuardo
Copy link

hey @gCardinal what do you mean by Make sure the path to your .env file is an absolute path?? What should I do exactly?

@billnbell
Copy link
Contributor

Everyone: This module is pretty simple. It generates a file and puts it in Pods. It does that when you yarn install - and only if it is not already there. So you have 2 choices. 1. Delete Pods - then rerun pod install or 2. pod deintegrate and then pod install

Got it?

@engineerStuardo
Copy link

@billnbell the problem is only on iOS that if I make a change on my .env file it doesn't read that change and keep the first one. I need to delete node_modules and until then reads the new one. Sorry I'm a beginner on this.

@gCardinal
Copy link

hey @gCardinal what do you mean by Make sure the path to your .env file is an absolute path?? What should I do exactly?

I mean instead of pointing to files like so ../../.env, I had to use the absolute path /Users/my-user/dev/project-name/.env.

I also did not need to delete my pods when changing a value - I do however need to clean the project in XCode. I am also not using the environment variable way to point to different files. I had to do it this way, so in the end I have something like this in my Podfile:

CURRENT_DIRECTORY = Dir.pwd()
PROJECT_ROOT = File.expand_path("#{CURRENT_DIRECTORY}/../../../")
ENV_FILES = {
  'Debug' => "#{PROJECT_ROOT}/.env",
  'Debug-Staging' => "#{PROJECT_ROOT}/.env.stg",
  'Debug-Prod' => "#{PROJECT_ROOT}/.env.prd",
  'Release' => "#{PROJECT_ROOT}/.env.dev",
  'Release-Staging' => "#{PROJECT_ROOT}/.env.stg",
  'Release-Prod' => "#{PROJECT_ROOT}/.env.prd",
}

# Then, in my post install hook:

  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if target.name == 'react-native-config'
        config.build_settings['ENVFILE'] = ENV_FILES[config.name]
      end
    end
  end

This was what finally worked for me, but as I said, when I change values, I need to clean the project. No other way around it that I could find.

@engineerStuardo
Copy link

Thank you so much @gCardinal 🥇

@jackkrone
Copy link

@mickael-h Faced the same issue, fixed it by clearing the xcodederiveddata in your system.

This works for me. To be more specific, I added the following script to my package.json, which I run every time I change the contents of my .env file:

"deleteDerivedData": "rm -rf ${HOME}/Library/Developer/Xcode/DerivedData/*"

I am using different schemes in my project which means I have a .env.prod, .env.develop, and .env.qa file, the contents of which are copied into the main .env file when running the associated scheme. I always delete derived data before switching the scheme I'm running locally.

@arundevkv
Copy link

@jackkrone that worked for me. Thanks

@toorandroidmaster
Copy link

Faced the same issue, every time need to manually remove node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m.

Any updates on the handling?

i fixed this issue using this
#698 (comment)

@Irfanwani
Copy link

Irfanwani commented Jul 11, 2023

Same issue with iOS still persists. for me deleting the pods and reinstalling them worked but it takes a lot of time for me for just to rewrite the env variables.

@billnbell
Copy link
Contributor

Yeah to fix this correctly we need to add to the pipeline an env variable check or something "RESET_CONFIG=1" and then when building delete the file and re-generates. I am sure this is not a high priority - but the author of this package should see if there is a way to make this easier to regenerate the generated files.

@developerdanx
Copy link

This is still an issue. Seriously we don't have a good solution yet?

@billnbell
Copy link
Contributor

I think they take PRs :)

@nathantew14
Copy link

I tried pretty much everything suggested here and it still doesn’t work...

@bodiavaleur
Copy link

bodiavaleur commented Jul 21, 2023

Got it fixed by generating GeneratedDotEnv.m on every build

Inside Edit scheme... -> Build -> Pre-actions

cp "${PROJECT_DIR}/../.env.production" "${PROJECT_DIR}/../.env"
"${SRCROOT}/../../../node_modules/react-native-config/ios/ReactNativeConfig/BuildXCConfig.rb" "${SRCROOT}/../.env" "${SRCROOT}/tmp.xcconfig"
"${SOURCE_ROOT}/../../../node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb" "${SOURCE_ROOT}/../.env" "${SOURCE_ROOT}/../../../node_modules/react-native-config/ios/ReactNativeConfig/"
^ Add this line and change relative path to your node_modules

@nathantew14
Copy link

i gave up and started using expo. it just... works! never turning back.

@Biplovkumar
Copy link

I added "clear": "rm -f node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m", in Package.json scripts. it is working fine. but is is a temporary solution.
I am using 1.5.1 and still facing same issue. Please resolve.

@joaquinvaz
Copy link

I'm having this issue, any solution? only running "Clean Build Folder" in Xcode does work

@nathantew14
Copy link

nathantew14 commented Oct 5, 2023

I'm having this issue, any solution? only running "Clean Build Folder" in Xcode does work

Just use expo @Biplovkumar @joaquinvaz

@henaharon
Copy link

henaharon commented Nov 28, 2023

I have renamed .env.development
to
.env.integration
Works like a charm!

@rolfen
Copy link

rolfen commented Jan 17, 2024

I found the solution. I format my disk and reinstall the OS and the variable updates. Works every time.

PS: Found a trick on another github thread:

Manually edit the file importing react-native-dotenv by either adding an empty line or whitespace will work.

zetachang/react-native-dotenv#24 (comment)

@arelstone
Copy link

I found the solution. I format my disk and reinstall the OS and the variable updates. Works every time.

It works 100% of the times.. Thanks for the solution 👏 🎆

@brunovollmer
Copy link

Anything speaking against doing it like this:

rm -rf "${SRCROOT}/../node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m"
cp "${PROJECT_DIR}/../.env.development" "${PROJECT_DIR}/../.env"  
"${SRCROOT}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildXCConfig.rb" "${SRCROOT}/.." "${SRCROOT}/tmp.xcconfig"

@Biplovkumar
Copy link

Just u

This is not a solution

@bigriot
Copy link

bigriot commented Feb 10, 2024

Anything speaking against doing it like this:

rm -rf "${SRCROOT}/../node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m"
cp "${PROJECT_DIR}/../.env.development" "${PROJECT_DIR}/../.env"  
"${SRCROOT}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildXCConfig.rb" "${SRCROOT}/.." "${SRCROOT}/tmp.xcconfig"

I think this is pretty much it. A simple update to the documentation would suffice IMHO. I would add
rm "${SRCROOT}/tmp.xcconfig"
as a post action script or add tmp.xcconfig to .gitignore

@endrits079
Copy link

Why is this closed when this issue apparently still exists?

@phuocantd
Copy link

@mickael-h Faced the same issue, fixed it by clearing the xcodederiveddata in your system.

This works for me. To be more specific, I added the following script to my package.json, which I run every time I change the contents of my .env file:

"deleteDerivedData": "rm -rf ${HOME}/Library/Developer/Xcode/DerivedData/*"

I am using different schemes in my project which means I have a .env.prod, .env.develop, and .env.qa file, the contents of which are copied into the main .env file when running the associated scheme. I always delete derived data before switching the scheme I'm running locally.

It not work for me

@samipshresthaq
Copy link

samipshresthaq commented Apr 6, 2024

Faced the same issue, every time need to manually remove node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m.
Any updates on the handling?

i fixed this issue using this #698 (comment)

This worked for me on ios! Apr 6 2024

@sergsar
Copy link

sergsar commented Apr 28, 2024

The issue still exists on iOS and deleting .env files every build isn't a good idea at all

@arelstone
Copy link

The problem is not the env file nor the xcode cache. The problem is that a file is written into the ios and android directories of this library.

A workaround that I am using is to create a prestart script in our package.json that deleted thoes files

"prestart": "rm -f node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m node_modules/react-native-config/android/build/generated/source/buildConfig/debug/com/lugg/RNCConfig/BuildConfig.java node_modules/react-native-config/android/build/generated/source/buildConfig/release/com/lugg/RNCConfig/BuildConfig.java"

This requires us to restart the metro bundler when we've updated the env - Which to me makes sense.

@valerii-link
Copy link

#788 (comment)

@eisodev
Copy link

eisodev commented Jul 11, 2024

From reading the comments, this works for me when I change a custom ENV variable URL from server to localhost for running a local backend and then starting my React Native app (following is from my "scripts" in package.json:

"refreshENVforiOS": "touch \"node_modules/react-native-config/ios/ReactNativeConfig/BuildDotenvConfig.rb\"",
"ios": "npm run refreshENVforiOS && react-native run-ios --terminal terminal",

npm run ios

Starts a new terminal window (like it used to before newer React Native versions). And it now reads the correct env variables that I updated in the .env file in the project root.

@Yangeok
Copy link

Yangeok commented Aug 14, 2024

Script prestart is the charm! Thank you

@khanh21011999
Copy link

You saved me from stupid dog shit I faced 2 days

#788 (comment)

You saved me from stupid dog shit 2 day, thank god

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