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

Open
DLevai94 opened this Issue Nov 26, 2017 · 33 comments

Comments

Projects
None yet
@DLevai94
Copy link

DLevai94 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

This comment has been minimized.

Copy link

selfeky commented Dec 3, 2017

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

@aestrro

This comment has been minimized.

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

@DLevai94

This comment has been minimized.

Copy link
Author

DLevai94 commented Dec 4, 2017

@selfeky unfortunately no.

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

@DLevai94

This comment has been minimized.

Copy link
Author

DLevai94 commented Dec 4, 2017

@selfeky It works nicely if you downgrade React Native to v0.49.5!

@aestrro

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

GunnarHolwerda commented Jan 3, 2018

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

TimothePearce commented Jan 29, 2018

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

@ifero

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

dancomanlive commented Feb 16, 2018

@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

This comment has been minimized.

Copy link

apparition47 commented Feb 27, 2018

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

This comment has been minimized.

Copy link

benjaminketron commented Mar 21, 2018

@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

This comment has been minimized.

Copy link

PatNeedham commented Apr 26, 2018

@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

This comment has been minimized.

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

This comment has been minimized.

Copy link

PatNeedham commented Apr 26, 2018

@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

This comment has been minimized.

Copy link

TimothePearce commented Apr 26, 2018

@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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

mjgallag commented Jun 17, 2018

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

@eseQ

This comment has been minimized.

Copy link

eseQ commented Jul 9, 2018

If you use fastlane try bundle exec fastlane comand

@reilem

This comment has been minimized.

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

This comment has been minimized.

Copy link

Martian2Lee commented Aug 21, 2018

Thank you @scottmas , saved my day!

@gatspy

This comment has been minimized.

Copy link

gatspy commented Sep 17, 2018

@birkir perfect solution, thank you!
issuecomment-353156419

@Blargh1

This comment has been minimized.

Copy link

Blargh1 commented Nov 30, 2018

For pods: #125

@jpmazza

This comment has been minimized.

Copy link

jpmazza commented Jan 10, 2019

Hey!
I'm having this issue, too.
1_ I've set as Info.plist Preprocessor Prefix File: ${BUILD_DIR}/GeneratedInfoPlistDotEnv.h.
2_ I have the react-native-config manually linking.
3_ I have three different env files: .env.dev, .env.beta, .env.prod

I don't even find the GeneratedDotEnv.m file and GeneratedInfoPlistDotEnv neither. Where should be them located?

Any thoughts? Thanks in advance!

@djGrill

This comment has been minimized.

Copy link

djGrill commented Jan 21, 2019

Linking manually worked perfect for me; as suggested here.

@smakosh

This comment has been minimized.

Copy link

smakosh commented Feb 27, 2019

I followed your solution @birkir but now I'm getting this error

he following build commands failed:

	ExternalBuildToolExecution build-env
(1 failure)
@birkir

This comment has been minimized.

Copy link

birkir commented Mar 1, 2019

@smakosh Go to the log explorer panel, select all (log, warnings and errors) and scroll up until you find the reason.

What you pasted is not why it failed, just that it failed and we can't help you with only this information I am afraid.

@smakosh

This comment has been minimized.

Copy link

smakosh commented Mar 1, 2019

I solved it by adding this post_install script to my podfile

post_install do |installer|
  installer.pods_project.targets.each do |target|
    targets_to_ignore = %w(React)

    if targets_to_ignore.include? target.name
      target.remove_from_project
    end

    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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.