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

'GeneratedDotEnv.m' file not found #187

Closed
thedevdavid opened this issue Nov 26, 2017 · 68 comments
Closed

'GeneratedDotEnv.m' file not found #187

thedevdavid opened this issue Nov 26, 2017 · 68 comments

Comments

@thedevdavid
Copy link

thedevdavid commented Nov 26, 2017

If I use this package with Cocoapods and follow the README's instructions, the iOS build fails every time. With C Preprocessor error "'GeneratedDotEnv.m' file not found".

Without any pods, it works.

Does anybody have any experience with this?
I'm stuck for 2 days now because of this issue. I can't find any solution. I tried a lot of things by now.

@selfeky
Copy link

selfeky commented Dec 3, 2017

@DLevai94 I have the same issue.
Did you find a solution?

@aestrro
Copy link

aestrro commented Dec 4, 2017

I am running into the same issue, this error seems to be happening on react-native 0.50.+

Can the following commit be problematic for react-native link react-native-config?
facebook/react-native@4c196ae

@thedevdavid
Copy link
Author

@selfeky unfortunately no.

Is it possible that react-native-firebase package has the same issue? facebook/react-native#15460 (comment)

@aestrro
Copy link

aestrro commented Dec 4, 2017

Yeah, reading up on it it seems the community who uses it excitedly wanted to jump on the 50.x version of RN. Folks, this is a beautiful project - I'll write more tests on my backend while this is in flight. Great job, it has reduced plenty of my RN complications my life in many ways. I'll be patient.

@birkir
Copy link

birkir commented Dec 20, 2017

I used to have my own version of react-native-config, because the project was stale for a while, but I came up with a solution that works quite well with cocoapods for now.

Add thisbuild-env.sh file somewhere in your project (for example ./scripts/build-env.sh)

#/bin/sh
TARGET_ENV=".env"
RNCDIR="./node_modules/react-native-config/ios"

if [ ! -z "$SYMROOT" ]; then
  # Ensure directories exist before copying files
  mkdir -p $SYMROOT
  mkdir -p $BUILD_DIR

  # Build dotenv
  cd $RNCDIR
  ./ReactNativeConfig/BuildDotenvConfig.ruby
  cd -

  # Copy generated dotenv files to node_modules directory
  cp "$BUILD_DIR/GeneratedInfoPlistDotEnv.h" "$RNCDIR/ReactNativeConfig/GeneratedInfoPlistDotEnv.h"
  cp "$SYMROOT/GeneratedDotEnv.m" "$RNCDIR/ReactNativeConfig/GeneratedDotEnv.m"
fi
  1. (Menu) Editor > Add target
  2. Select Cross-Platform from the top and then "External Build System".
  3. Write build-env as Product Name, update your identifiers etc.
  4. Edit the fields as following:
  • Build Tool: ${SRCROOT}/../scripts/build-env.sh
  • Arguments: $(ACTION)
  • Directory: ${SRCROOT}/..
  • ✅ Pass build settings in environment
  1. (Menu) Product > Scheme > Edit Scheme
  2. Select Build from the left menu.
  3. Press the + symbol from the bottom of the window.
  4. Select build-env from the list.
  5. Move it to the top of list (before your product)
  6. Done!

Now you can use the module as-is with cocoapods. I have a even better solution with dynamically built env for JS, so I don't have to clean and build again while developing (https://github.com/ueno-llc/react-native-starter).

@GunnarHolwerda
Copy link

Hi all, I ran into this error and was using @birkir's method for resolving the issue involving CocoaPods. It was working fine util I had to archive my project where BuildDotenvConfig.rb would error on attempting to copy the GeneratedDotEnv.m and GeneratedInfoPlistDotEnv.h files.

To fix this issue I updated BuildDotenvConfig.rb to add all lines with the comment # ADD THIS LINE

#!/usr/bin/env ruby
require 'fileutils' # ADD THIS LINE
# Allow utf-8 charactor in config value
# For example, APP_NAME=中文字符
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

defaultEnvFile = ".env"

# pick a custom env file if set
if File.exists?("/tmp/envfile")
  custom_env = true
  file = File.read("/tmp/envfile").strip
else
  custom_env = false
  file = ENV["ENVFILE"] || defaultEnvFile
end

puts "Reading env from #{file}"

dotenv = begin
  # https://regex101.com/r/cbm5Tp/1
  dotenv_pattern = /^(?:export\s+|)(?<key>[[:alnum:]_]+)=((?<quote>["'])?(?<val>.*?[^\\])\k<quote>?|)$/

  # find that above node_modules/react-native-config/ios/
  path = File.join(Dir.pwd, "../../../#{file}")
  if File.exists?(path)
    raw = File.read(path)
  elsif File.exists?(file)
    raw = File.read(file)    
  else
    defaultEnvPath = File.join(Dir.pwd, "../../../#{defaultEnvFile}")
    if !File.exists?(defaultEnvPath)
      # try as absolute path
      defaultEnvPath = defaultEnvFile
    end
    defaultRaw = File.read(defaultEnvPath)
    if (defaultRaw)
      raw = defaultRaw + "\n" + raw
    end
  end

  raw.split("\n").inject({}) do |h, line|
    m = line.match(dotenv_pattern)
    next h if m.nil?
    key = m[:key]
    # Ensure string (in case of empty value) and escape any quotes present in the value.
    val = m[:val].to_s.gsub('"', '\"')
    h.merge(key => val)
  end
rescue Errno::ENOENT
  puts("**************************")
  puts("*** Missing .env file ****")
  puts("**************************")
  {} # set dotenv as an empty hash
end

# create obj file that sets DOT_ENV as a NSDictionary
dotenv_objc = dotenv.map { |k, v| %Q(@"#{k}":@"#{v}") }.join(",")
template = <<EOF
  #define DOT_ENV @{ #{dotenv_objc} };
EOF

# ensure paths exist
FileUtils.mkdir_p ENV["SYMROOT"] # ADD THIS LINE
FileUtils.mkdir_p ENV["BUILD_DIR"] # ADD THIS LINE

# write it so that ReactNativeConfig.m can return it
path = File.join(ENV["SYMROOT"], "GeneratedDotEnv.m")

File.open(path, "w") { |f| f.puts template }

# create header file with defines for the Info.plist preprocessor
info_plist_defines_objc = dotenv.map { |k, v| %Q(#define __RN_CONFIG_#{k}  #{v}) }.join("\n")

# write it so the Info.plist preprocessor can access it
path = File.join(ENV["BUILD_DIR"], "GeneratedInfoPlistDotEnv.h")
File.open(path, "w") { |f| f.puts info_plist_defines_objc }

if custom_env
  File.delete("/tmp/envfile")
end

puts "Wrote to #{path}"

This ensured the full file paths I was attempting to write to existed before writing.

@birkir
Copy link

birkir commented Jan 19, 2018

Yes I ran into this as well at some point on a new project that didn't have any previously archived builds, I updated the shell script snippet.

@timothepearce
Copy link

Hi guys, is this the only solution right now to use react-native-config with cocoapods?

@ifero
Copy link

ifero commented Feb 2, 2018

Yes, at the moment the only solutions available are @birkir or mine (#83 (comment)), but @birkir 's is much more cleaner and work inside Xcode.
The only think that I don't like is that I had to create 3 build-env.sh files, one for each target (prod, stage, dev)

@alextorn
Copy link

alextorn commented Feb 7, 2018

Folks, you should double check if libReactNativeConfig.a is available in target settings - Build Phases - Link Binary With Libraries:

screenshot 2018-02-06 17 12 11

Sometimes regular automatic linking is not enough. I solved the problem by manual linking react-native-config library.

react-native: v 0.52.2
react-native-config: v 0.11.5

UPD: I cleaned cache and found that linking is not enough...

@dancomanlive
Copy link

@alextorn libReactNativeConfig.a is missing indeed. Would you be so kind to elaborate on the manual linking? The RN docs are not very clear. Thanks!

@apparition47
Copy link

After spending half a day on this, managed to get get this project to build and expose the .env in JS but without CocoaPods. Instead, I manually linked this project as which I documented in my updated README.

Using RN 0.53.0.

Basically the process is to:

  1. add the ../node_modules/react-native-config/ios/ReactNativeConfig.xcodeproj to your own project
  2. In your Target settings, Build Phases, Link Binary With Libraries, add libReactNativeConfig.a.
  3. Manage scheme, expand "Build", click "Pre-actions", "New Run Script Action", enter:
if [ "${ENVFILE}" ]; then echo "${ENVFILE}" > /tmp/envfile ; else echo ".env" > /tmp/envfile; fi
  1. Ensure your .env.prod file has some key/values without spaces around the = like I've been seeing around these threads. Then run in term: $ ENVFILE=.env.prod react-native run-ios.

@benjaminketron
Copy link

@apparition47 Thank you. This saved a bit of time. In practice i found step three to be unnecessary when importing with

#import <ReactNativeConfig/ReactNativeConfig.h>

rather than

#import "ReactNativeConfig.h"

Does that make sense or did I miss something in the thread you were targeting with step 3?

@PatNeedham
Copy link

@birkir I tried following your suggestion with the ./scripts/build-env.sh file and adding it to the new build-env target, but when attempting to build or clean the app from Xcode, I always get Shell Script Invocation Error:

make: *** No targets specified and no makefile found.  Stop.
Command /usr/bin/make failed with exit code 2

Same result even after changing the permissions for that shell script. Did you are anyone else run into this script invocation issue as well, and if so how was it resolved?

@timothepearce
Copy link

timothepearce commented Apr 26, 2018

@PatNeedham It works for me, I remember I got your mistake when I positioned the target after React in the build order.

Try in that one:
capture d ecran 2018-04-26 a 22 41 28

I hope it helps you.

@PatNeedham
Copy link

@timothepearce my targets were also in that order. When I clicked on the Manage Schemes... button in the lower left of that pop-up, I noticed build-env did not initially have Shared checked on the right hand column:

image

Having that checked still produced the same shell script invocation result.

Running ls -l scripts/build-env.sh from command line results in

---------x  1 patneedham  staff  553 Apr 26 15:16 scripts/build-env.sh

Actually made some changes to eliminate those permission restrictions so it is now like this:

-rwxrwxrwx  1 patneedham  staff  553 Apr 26 17:12 scripts/build-env.sh

Same result when trying to run Xcode build. Do those updated permissions look off somehow?

@timothepearce
Copy link

@PatNeedham You must click on Edit Scheme... not Manage Schemes... to edit the build order.
You are not suppose to have any build-end target in the screenshot you send.

Look at mine:
capture d ecran 2018-04-26 a 23 49 38

@peacechen
Copy link

peacechen commented May 10, 2018

The generated files are a nightmare after upgrading to React Native 0.55. birkir's script does the job of generating the DotEnv files, but using it as a new target isn't able to handle different schemes.

To support different schemes, use his script without setting TARGET_ENV and place it in the scheme's Build -> Pre-action script box.

echo ".env.prod" > /tmp/envfile
./ios/react-native-config-gen.sh

And here's the modified react-native-config-gen.sh script:

#/bin/sh

RNCDIR="./node_modules/react-native-config/ios"

if [ ! -z "$SYMROOT" ]; then
  # Ensure directories exist before copying files
  mkdir -p $SYMROOT
  mkdir -p $BUILD_DIR

  # Build dotenv
  cd $RNCDIR
  ./ReactNativeConfig/BuildDotenvConfig.ruby
  cd -

  # Copy generated dotenv files to node_modules directory
  cp "$BUILD_DIR/GeneratedInfoPlistDotEnv.h" "$RNCDIR/ReactNativeConfig/GeneratedInfoPlistDotEnv.h"
  cp "$SYMROOT/GeneratedDotEnv.m" "$RNCDIR/ReactNativeConfig/GeneratedDotEnv.m"
fi

Note: Remove react-native-config from the Podfile, add it under Libraries, and add libReactNativeConfig.a to Linked Frameworks and Libraries.

@vasilich6107
Copy link

vasilich6107 commented May 19, 2018

Hi)
I had a similar issue. When passed through the README installation instructions everything is ok.
But after using command pod install I got the same error 'GeneratedDotEnv.m' file not found

If you are experiencing similar problems add this lines to the end of your podfile

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == "react-native-config"
      target.remove_from_project
    end
  end
end

@pontusab
Copy link

pontusab commented Jun 7, 2018

@vasilich6107's Solution work, but don't be stupid like me and forget to remove the pod pod 'react-native-config', :path => '../node_modules/react-native-config'

@scottmas
Copy link

scottmas commented Jun 7, 2018

The simplest way around this I found was to simply temporarily delete my Podfile, and only THEN do yarn add react-native-config && react-native link. That way the library is added to ios via the manual linking method, instead of being added to your Podfile. After that, I restore my Podfile by simply doing git checkout ios/Podfile

@mjgallag
Copy link
Contributor

FYI, this appears to be the same issue as #125.

@eseQ
Copy link

eseQ commented Jul 9, 2018

If you use fastlane try bundle exec fastlane comand

@reilem
Copy link

reilem commented Aug 19, 2018

@vasilich6107 & @pontusab 's solution worked for me. Dragged an dropped the ReactNativeConfig.xcodeproj from node_modules to the 'Libraries' folder and added "libReactNativeConfig.a" (NOT libreact-native-config.a) under 'Link Binary With Libraries' in 'Build Phases'. And removing the left over reference in Podfile from the automatic linking.

Then using #import <ReactNativeConfig/ReactNativeConfig.h> to import instead of "ReactNativeConfig.h".

@Martian2Lee
Copy link

Thank you @scottmas , saved my day!

@gatspy
Copy link

gatspy commented Sep 17, 2018

@birkir perfect solution, thank you!
issuecomment-353156419

@Blargh1
Copy link

Blargh1 commented Nov 30, 2018

For pods: #125

@alvelig
Copy link

alvelig commented Aug 28, 2019

I've managed to solve it without linking like this #125 (comment)

@helderberto
Copy link

helderberto commented Sep 6, 2019

I'm on version 0.60.5 of React Native.

I made the following steps:

  1. yarn add react-native-config@^0.11.7;

  2. react-native link react-native-config

  3. Add this code at the end of your PodFile:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == 'react-native-config'
      phase = target.project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
      phase.shell_script = "cd ../../"\
                            " && RNC_ROOT=./node_modules/react-native-config/"\
                            " && export SYMROOT=$RNC_ROOT/ios/ReactNativeConfig"\
                            " && export BUILD_DIR=$RNC_ROOT/ios/ReactNativeConfig"\
                            " && ruby $RNC_ROOT/ios/ReactNativeConfig/BuildDotenvConfig.ruby"

      target.build_phases << phase
      target.build_phases.move(phase,0)
    end

  end
end
  1. cd ios && pod deintegrate && pod install

This works fine for me. :)

@codeithuman
Copy link

@helderburato, thank you! This worked for me on react-native 0.59.10 and react-native-config 0.11.7`.

@MaffooBristol
Copy link

The latest solution worked for me too, but I don't really know why it needed to 😕

It used to build fine but then I added react-native-navigation and it exploded!

@jasonbodily
Copy link

@helderburato 's answer works for me too, but since I'm using react-native 0.60+, my build now complains that it's linked. When I run react-native unlink react-native-config, it works and there's no complaining.

@MaffooBristol
Copy link

For the time being I've actually removed this module as it keeps springing up random errors and the original solution I used seems very unstable. So my advice is that although that podfile hack does work initially, it may not be worth the additional problems it causes.

Not sure what other solution there is right now though unfortunately :(

@M1chaelChen
Copy link

Thanks to my colleague who found this magical commit which works perfectly. No workaround needed!
yarn add react-native-config@luggit/react-native-config#1eb6ac01991210ddad2989857359a0f6ee35d734

"react-native": "0.61.2"

@GaniduAsh
Copy link

GaniduAsh commented Oct 31, 2019

This relates with a permission issue , executing sudo chmod -R 777 /path of the source will solve the issue :)

@zedtux
Copy link

zedtux commented Nov 8, 2019

I had to adapt the post install script to make it working with RN 0.60.6 (and also to make it more Ruby style):

post_install do |installer|
  installer.pods_project.targets.each do |target|
    next unless target.name == 'react-native-config'

    phase = target.project.new(
      Xcodeproj::Project::Object::PBXShellScriptBuildPhase
    )
    phase.shell_script = <<-SCRIPT
      cd ../../ && \
      RNC_ROOT=./node_modules/react-native-config/ && \
      export SYMROOT=$RNC_ROOT/ios/ReactNativeConfig && \
      export BUILD_DIR=$RNC_ROOT/ios/ReactNativeConfig && \
      ruby $RNC_ROOT/ios/ReactNativeConfig/BuildDotenvConfig.ruby ${SRCROOT} ${SRCROOT}
    SCRIPT

    target.build_phases << phase
    target.build_phases.move(phase, 0)
  end
end

Adding the two ${SRCROOT} in order to define the place where is the .env file and where to build the GeneratedDotEnv.m file fixed the script.

@pedro
Copy link
Contributor

pedro commented Nov 14, 2019

👍 #349 has been merged/released with 0.12

@MaxInMoon
Copy link

@pedro can you have a look on #414 please?
I can't make it work with rn 0.61 and 0.12

@vvusts
Copy link

vvusts commented Nov 27, 2019

@zedtux I get error that .ruby file doesn’t exist. I have file on this path but it ends with .rb any idea?

@zedtux
Copy link

zedtux commented Nov 27, 2019

@vvusts well then just replace .ruby by .rb 😉

@leemcmullen
Copy link

leemcmullen commented Apr 2, 2020

I was experiencing this issue when trying to build on AppCenter with RN0.61 and react-native-config 0.12.

I was using the helpful script posted by @zedtux (#187 (comment)) to get around it.

I'm now running RN0.61.5, I've upgraded to react-native-config 1.0 and AppCenter now builds without a problem.

** UPDATE 17th April 2020 **
I've started getting the following issue: Build input file cannot be found: GeneratedDotEnv.m during local archive and during CI builds (on AppCenter). The only fix I can find is this (#391 (comment)) but it's annoying because you have to update podspec whenever node_modules is re-built. Only fix which appeared to work consistently across local archiving and remote building/archiving.

psamim added a commit to psamim/react-native-config that referenced this issue Oct 13, 2020
@samzmann
Copy link

I recently ran into this issue. Simply updating react-native-config from 1.4.2 to 1.4.3 fixed the issue for me!

@chenop
Copy link

chenop commented Sep 19, 2022

Clean xcode and rebuilding fixed it for me

@anija
Copy link

anija commented Oct 20, 2022

Experiencing this occasionally on 1.4.11

@spsaucier
Copy link

We fixed by removing the requirements for GeneratedInfoPlistDotEnv.h, since it's not recommended by the react-native-config Readme anymore, and then we followed the instructions towards Config.xcconfig enabling env vars in Info.plist: https://github.com/luggit/react-native-config#availability-in-build-settings-and-infoplist, making sure that our Info.plist was using the proper $(VAR) syntax.

@dcsan
Copy link

dcsan commented Dec 7, 2022

@chenop what do you mean by clean xcode ? I have a clean-all task as follows, are there any other ghostly artifacts that need to be hunted down?

clean-all:
    -rm ./package-lock.json
    -rm -rf ./node_modules
    -rm -rf ./ios/Pods
    -rm -rf ./ios/Podfile.lock
    -rm -fr $TMPDIR/metro-cache
    -rm $TMPDIR/haste-map-*

@chenop
Copy link

chenop commented Dec 7, 2022

@dcsan clean xcode means:
Product --> Clean Build Folder

@ShepSims
Copy link

@dcsan clean xcode means: Product --> Clean Build Folder

Useful Hotkey for this is (Shift + Command + K) in XCode

@ko-devHong
Copy link

This problem appears to be caused by the failure to find the .env configuration file. I put the .env file in the project and did the following action.

  1. xcode -> app icon lick => edit scheme click
    스크린샷 2023-02-24 오전 11 07 40
  2. build => pre-actions
  3. write scripts

example

# Type a script or drag a script file from your workspace to insert its path.
PROD_FILE="${PROJECT_DIR}/../.env.production"
DEV_FILE="${PROJECT_DIR}/../.env.development"
if [ -f "$PROD_FILE" ]; then
    cp "$PROD_FILE" "${PROJECT_DIR}/../.env"
else 
    cp "$DEV_FILE" "${PROJECT_DIR}/../.env"
fi

"${SRCROOT}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildXCConfig.rb" "${SRCROOT}/.." "${SRCROOT}/tmp.xcconfig"

@ShepSims
Copy link

ShepSims commented Mar 9, 2023

I had accidentally deleted my tmp.xcconfig from the ios folder

a quick
cd ios && pod install && cd .. from the project root directory worked for me as that regenerates the file

might also clean XCode before running again (⌘Command + K )

@brascene
Copy link

brascene commented Apr 6, 2023

Pod install again and error was gone.

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