Skip to content

Commit d0feca0

Browse files
authored
fix(macCatalyst): construct correct path for executable (#2510)
- targetBuildDir var now has `-maccatalyst` in it before it gets to this method, so no need to add it again - gracefully handle if -maccatalyst exists or not as area regresses frequently - fail fast with error message if targetBuildDir does not exist at all - add unit test for the with and without -maccatalyst case plus verify ios did not regress
1 parent 0dcdcc6 commit d0feca0

File tree

2 files changed

+125
-7
lines changed

2 files changed

+125
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import {getTempDirectory} from '../../../../../../jest/helpers';
4+
import {BuildSettings} from '../getBuildSettings';
5+
import {getBuildPath} from '../getBuildPath';
6+
7+
const targetBuildDirName = 'foo';
8+
const targetBuildDirNameWithMaccatalyst = `${targetBuildDirName}-maccatalyst`;
9+
const executableFolderPath = path.join('foo.app', 'Contents', 'MacOS', 'foo');
10+
11+
test('correctly determines macCatalyst build artifact path new style', async () => {
12+
// setup:
13+
const tmpBuildPath = getTempDirectory('maccatalyst-test-dir');
14+
fs.mkdirSync(path.join(tmpBuildPath, targetBuildDirNameWithMaccatalyst), {
15+
recursive: true,
16+
});
17+
18+
// - create buildSettings object that represents this to CLI
19+
const buildSettings: BuildSettings = {
20+
TARGET_BUILD_DIR: path.join(
21+
tmpBuildPath,
22+
targetBuildDirNameWithMaccatalyst,
23+
),
24+
EXECUTABLE_FOLDER_PATH: executableFolderPath,
25+
FULL_PRODUCT_NAME: 'unused-in-this-test',
26+
INFOPLIST_PATH: 'unused-in-this-test',
27+
};
28+
29+
// test:
30+
// - send our buildSettings in and see what build path comes out
31+
const buildPath = await getBuildPath(buildSettings, 'ios', true);
32+
33+
// assert:
34+
expect(buildPath).toBe(
35+
path.join(
36+
tmpBuildPath,
37+
targetBuildDirNameWithMaccatalyst,
38+
executableFolderPath,
39+
),
40+
);
41+
});
42+
43+
test('correctly determines macCatalyst build artifact path old style', async () => {
44+
// setup:
45+
const tmpBuildPath = getTempDirectory('maccatalyst-test-dir');
46+
fs.mkdirSync(path.join(tmpBuildPath, targetBuildDirNameWithMaccatalyst), {
47+
recursive: true,
48+
});
49+
50+
// - create buildSettings object that represents this to CLI
51+
// FIXME get the build settings as side effect from project definition,
52+
// because it's the translation of project settings to path that fails
53+
const buildSettings: BuildSettings = {
54+
TARGET_BUILD_DIR: path.join(tmpBuildPath, targetBuildDirName),
55+
EXECUTABLE_FOLDER_PATH: executableFolderPath,
56+
FULL_PRODUCT_NAME: 'unused-in-this-test',
57+
INFOPLIST_PATH: 'unused-in-this-test',
58+
};
59+
60+
// test:
61+
// - send our buildSettings in and see what build path comes out
62+
const buildPath = await getBuildPath(buildSettings, 'ios', true);
63+
64+
// assert:
65+
expect(buildPath).toBe(
66+
path.join(
67+
tmpBuildPath,
68+
targetBuildDirNameWithMaccatalyst,
69+
executableFolderPath,
70+
),
71+
);
72+
});
73+
74+
test('correctly determines iOS build artifact path', async () => {
75+
// setup:
76+
const tmpBuildPath = getTempDirectory('ios-test-dir');
77+
fs.mkdirSync(path.join(tmpBuildPath, targetBuildDirName), {
78+
recursive: true,
79+
});
80+
81+
// - create buildSettings object that represents this to CLI
82+
const buildSettings: BuildSettings = {
83+
TARGET_BUILD_DIR: path.join(tmpBuildPath, targetBuildDirName),
84+
EXECUTABLE_FOLDER_PATH: executableFolderPath,
85+
FULL_PRODUCT_NAME: 'unused-in-this-test',
86+
INFOPLIST_PATH: 'unused-in-this-test',
87+
};
88+
89+
// test:
90+
// - send our buildSettings in and see what build path comes out
91+
const buildPath = await getBuildPath(buildSettings);
92+
93+
// assert:
94+
expect(buildPath).toBe(
95+
path.join(tmpBuildPath, targetBuildDirName, executableFolderPath),
96+
);
97+
});

packages/cli-platform-apple/src/commands/runCommand/getBuildPath.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {CLIError} from '@react-native-community/cli-tools';
22
import path from 'path';
3+
import fs from 'fs';
34
import {BuildSettings} from './getBuildSettings';
45
import {ApplePlatform} from '../../types';
56

@@ -8,7 +9,7 @@ export async function getBuildPath(
89
platform: ApplePlatform = 'ios',
910
isCatalyst: boolean = false,
1011
) {
11-
const targetBuildDir = buildSettings.TARGET_BUILD_DIR;
12+
let targetBuildDir = buildSettings.TARGET_BUILD_DIR;
1213
const executableFolderPath = buildSettings.EXECUTABLE_FOLDER_PATH;
1314
const fullProductName = buildSettings.FULL_PRODUCT_NAME;
1415

@@ -24,11 +25,31 @@ export async function getBuildPath(
2425
throw new CLIError('Failed to get product name.');
2526
}
2627

27-
if (isCatalyst) {
28-
return path.join(`${targetBuildDir}-maccatalyst`, executableFolderPath);
29-
} else if (platform === 'macos') {
30-
return path.join(targetBuildDir, fullProductName);
31-
} else {
32-
return path.join(targetBuildDir, executableFolderPath);
28+
// Default is platform == ios && isCatalyst == false
29+
let buildPath = path.join(targetBuildDir, executableFolderPath);
30+
31+
// platform == ios && isCatalyst == true needs build path suffix,
32+
// but this regresses from time to time with suffix present or not
33+
// so check - there may be one already, or we may need to add suffix
34+
if (platform === 'ios' && isCatalyst) {
35+
// make sure path has one and only one '-maccatalyst' suffix on end
36+
if (!targetBuildDir.match(/-maccatalyst$/)) {
37+
targetBuildDir = `${targetBuildDir}-maccatalyst`;
38+
}
39+
buildPath = path.join(targetBuildDir, executableFolderPath);
40+
}
41+
42+
// macOS gets the product name, not the executable folder path
43+
if (platform === 'macos') {
44+
buildPath = path.join(targetBuildDir, fullProductName);
3345
}
46+
47+
// Make sure the directory exists and fail fast vs silently failing
48+
if (!fs.existsSync(targetBuildDir)) {
49+
throw new CLIError(
50+
`target build directory ${targetBuildDir} does not exist`,
51+
);
52+
}
53+
54+
return buildPath;
3455
}

0 commit comments

Comments
 (0)