Skip to content

Commit a89b477

Browse files
authored
fix(apple): filter Node wrappers before writing env files (#2252)
1 parent afe68dc commit a89b477

File tree

9 files changed

+100
-74
lines changed

9 files changed

+100
-74
lines changed

ios/node.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require('json')
2+
require_relative('pod_helpers')
3+
4+
def find_node
5+
# If `pod install` is run inside a "virtual" environment like
6+
# [Yarn](https://yarnpkg.com/), we might find Node wrappers instead of the
7+
# actual binary.
8+
paths = `type --all --path node`.split("\n")
9+
i = paths.find_index { |bin| `file #{bin}`.include? 'Mach-O' }
10+
raise 'Could not find Node' if i.nil?
11+
12+
paths[i]
13+
end
14+
15+
def nearest_node_modules(project_root)
16+
path = find_file('node_modules', project_root)
17+
assert(!path.nil?, "Could not find 'node_modules'")
18+
19+
path
20+
end
21+
22+
def package_version(package_path)
23+
package_json = JSON.parse(File.read(File.join(package_path, 'package.json')))
24+
Gem::Version.new(package_json['version'])
25+
end

ios/pod_helpers.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ def try_pod(name, podspec, project_root)
9898
pod name, :podspec => podspec if File.exist?(File.join(project_root, podspec))
9999
end
100100

101-
def override_build_settings!(build_settings, settings_to_append)
102-
settings_to_append&.each do |setting, value|
103-
build_settings[setting] = value
104-
end
105-
end
106-
107101
def use_hermes?(options)
108102
use_hermes = ENV.fetch('USE_HERMES', nil)
109103
return use_hermes == '1' unless use_hermes.nil?

ios/test_app.rb

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_relative('assets_catalog')
55
require_relative('entitlements')
66
require_relative('info_plist')
7+
require_relative('node')
78
require_relative('pod_helpers')
89
require_relative('privacy_manifest')
910
require_relative('xcode')
@@ -34,18 +35,6 @@ def autolink_script_path(project_root, target_platform)
3435
File.join(package_path, 'native_modules')
3536
end
3637

37-
def nearest_node_modules(project_root)
38-
path = find_file('node_modules', project_root)
39-
assert(!path.nil?, "Could not find 'node_modules'")
40-
41-
path
42-
end
43-
44-
def package_version(package_path)
45-
package_json = JSON.parse(File.read(File.join(package_path, 'package.json')))
46-
Gem::Version.new(package_json['version'])
47-
end
48-
4938
def react_native_path(project_root, target_platform)
5039
@react_native_path ||= {}
5140

@@ -217,14 +206,10 @@ def make_project!(xcodeproj, project_root, target_platform, options)
217206
end
218207

219208
# Note the location of Node so we can use it later in script phases
220-
File.open(File.join(project_root, '.xcode.env'), 'w') do |f|
221-
node_bin = `which node`.strip!
222-
f.write("export NODE_BINARY=#{node_bin}\n")
223-
end
224-
File.open(File.join(destination, '.env'), 'w') do |f|
225-
node_bin = `dirname $(which node)`.strip!
226-
f.write("export PATH=#{node_bin}:$PATH\n")
227-
end
209+
node_bin = find_node
210+
File.write(File.join(project_root, '.xcode.env'), "export NODE_BINARY='#{node_bin}'\n")
211+
File.write(File.join(destination, '.env'),
212+
"export PATH=#{`dirname '#{node_bin}'`.strip!}:$PATH\n")
228213

229214
react_native = react_native_path(project_root, target_platform)
230215
rn_version = package_version(react_native.to_s).segments

ios/xcode.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
1414
PRODUCT_VERSION = 'PRODUCT_VERSION'.freeze
1515
USER_HEADER_SEARCH_PATHS = 'USER_HEADER_SEARCH_PATHS'.freeze
1616
WARNING_CFLAGS = 'WARNING_CFLAGS'.freeze
17+
18+
def override_build_settings!(build_settings, overrides)
19+
overrides&.each do |setting, value|
20+
build_settings[setting] = value
21+
end
22+
end

test/pack.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ describe("npm pack", () => {
150150
"ios/assets_catalog.rb",
151151
"ios/entitlements.rb",
152152
"ios/info_plist.rb",
153+
"ios/node.rb",
153154
"ios/pod_helpers.rb",
154155
"ios/privacy_manifest.rb",
155156
"ios/test_app.rb",

test/test_node.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require('minitest/autorun')
2+
3+
require_relative('../ios/node')
4+
5+
def fixture_path(*args)
6+
Pathname.new(__dir__).join('__fixtures__', *args)
7+
end
8+
9+
class TestTestApp < Minitest::Test
10+
def test_nearest_node_modules
11+
expected = fixture_path('test_app', 'node_modules')
12+
13+
assert_equal(expected, nearest_node_modules(fixture_path('test_app')))
14+
15+
react_native = fixture_path('test_app', 'node_modules', 'react-native')
16+
17+
assert_equal(expected, nearest_node_modules(react_native))
18+
19+
assert_equal(expected, nearest_node_modules(fixture_path('test_app', 'src')))
20+
end
21+
22+
def test_package_version
23+
react_native = fixture_path('test_app', 'node_modules', 'react-native')
24+
25+
assert_equal(Gem::Version.new('1000.0.0'), package_version(react_native))
26+
27+
cli = fixture_path('test_app', 'node_modules', '@react-native-community', 'cli-platform-ios')
28+
29+
assert_equal(Gem::Version.new('4.10.1'), package_version(cli))
30+
end
31+
end

test/test_pod_helpers.rb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,4 @@ def test_v
112112
assert_equal(1_001_000, v(1, 1, 0))
113113
assert_equal(1_001_001, v(1, 1, 1))
114114
end
115-
116-
def test_override_build_settings
117-
build_settings = { 'OTHER_LDFLAGS' => '-l"Pods-TestApp"', 'ONLY_ACTIVE_ARCH' => 'NO' }
118-
119-
override_build_settings!(build_settings, {})
120-
121-
assert_equal('-l"Pods-TestApp"', build_settings['OTHER_LDFLAGS'])
122-
123-
override_build_settings!(build_settings, { 'OTHER_LDFLAGS' => ' -ObjC' })
124-
125-
assert_equal(' -ObjC', build_settings['OTHER_LDFLAGS'])
126-
127-
# Test passing a table
128-
build_settings_arr = { 'OTHER_LDFLAGS' => ['$(inherited)', '-l"Pods-TestApp"'] }
129-
override_build_settings!(build_settings_arr, { 'OTHER_LDFLAGS' => [' -ObjC'] })
130-
131-
assert_equal([' -ObjC'], build_settings_arr['OTHER_LDFLAGS'])
132-
133-
# Test setting a new key
134-
override_build_settings!(build_settings, { 'OTHER_CFLAGS' => '-DDEBUG' })
135-
136-
assert_equal('-DDEBUG', build_settings['OTHER_CFLAGS'])
137-
override_build_settings!(build_settings, { 'ONLY_ACTIVE_ARCH' => 'YES' })
138-
139-
assert_equal('YES', build_settings['ONLY_ACTIVE_ARCH'])
140-
end
141115
end

test/test_test_app.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,6 @@ def test_autolink_script_path
4343
end
4444
end
4545

46-
def test_nearest_node_modules
47-
expected = fixture_path('test_app', 'node_modules')
48-
49-
assert_equal(expected, nearest_node_modules(fixture_path('test_app')))
50-
51-
react_native = fixture_path('test_app', 'node_modules', 'react-native')
52-
53-
assert_equal(expected, nearest_node_modules(react_native))
54-
55-
assert_equal(expected, nearest_node_modules(fixture_path('test_app', 'src')))
56-
end
57-
58-
def test_package_version
59-
react_native = fixture_path('test_app', 'node_modules', 'react-native')
60-
61-
assert_equal(Gem::Version.new('1000.0.0'), package_version(react_native))
62-
63-
cli = fixture_path('test_app', 'node_modules', '@react-native-community', 'cli-platform-ios')
64-
65-
assert_equal(Gem::Version.new('4.10.1'), package_version(cli))
66-
end
67-
6846
def test_react_native_pods
6947
[
7048
[0, '0.71'],

test/test_xcode.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require('minitest/autorun')
2+
3+
require_relative('../ios/xcode')
4+
5+
class TestXcode < Minitest::Test
6+
def test_override_build_settings
7+
build_settings = { 'OTHER_LDFLAGS' => '-l"Pods-TestApp"', 'ONLY_ACTIVE_ARCH' => 'NO' }
8+
9+
override_build_settings!(build_settings, {})
10+
11+
assert_equal('-l"Pods-TestApp"', build_settings['OTHER_LDFLAGS'])
12+
13+
override_build_settings!(build_settings, { 'OTHER_LDFLAGS' => '-ObjC' })
14+
15+
assert_equal('-ObjC', build_settings['OTHER_LDFLAGS'])
16+
17+
# Test passing a table
18+
build_settings_arr = { 'OTHER_LDFLAGS' => ['$(inherited)', '-l"Pods-TestApp"'] }
19+
override_build_settings!(build_settings_arr, { 'OTHER_LDFLAGS' => ['-ObjC'] })
20+
21+
assert_equal(['-ObjC'], build_settings_arr['OTHER_LDFLAGS'])
22+
23+
# Test setting a new key
24+
override_build_settings!(build_settings, { 'OTHER_CFLAGS' => '-DDEBUG' })
25+
26+
assert_equal('-DDEBUG', build_settings['OTHER_CFLAGS'])
27+
28+
override_build_settings!(build_settings, { 'ONLY_ACTIVE_ARCH' => 'YES' })
29+
30+
assert_equal('YES', build_settings['ONLY_ACTIVE_ARCH'])
31+
end
32+
end

0 commit comments

Comments
 (0)