Skip to content

Commit 540982c

Browse files
committed
fix: add guard for multiple application targets
1 parent f86c40f commit 540982c

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

packages/react-native-brownfield/src/expo-config-plugin/ios/xcodeHelpers.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,16 @@ export function addExpoPre55ShellPatchScriptPhase(
298298
appTargetName: string;
299299
}
300300
) {
301+
const applicationTargets = getApplicationTargetNames(project);
301302
const resolvedAppTargetName =
302-
appTargetName || getApplicationTargetName(project);
303+
appTargetName || getApplicationTargetName(applicationTargets);
303304

304305
if (!resolvedAppTargetName) {
305-
throw new SourceModificationError(
306-
'Could not determine the iOS app target name from the Xcode project. Please provide the app target name in plugin options.'
307-
);
306+
const errorMessage =
307+
applicationTargets.length > 1
308+
? `Multiple iOS application targets found in the Xcode project (${applicationTargets.join(', ')}). Please set ios.appTargetName in plugin options.`
309+
: 'Could not determine the iOS app target name from the Xcode project. Please provide the app target name in plugin options.';
310+
throw new SourceModificationError(errorMessage);
308311
}
309312

310313
project.addBuildPhase(
@@ -325,10 +328,21 @@ export function addExpoPre55ShellPatchScriptPhase(
325328
}
326329

327330
/**
328-
* Returns the iOS application target name from PBXNativeTarget section.
331+
*
332+
* @param applicationTargets iOS application target names
333+
* @returns First iOS application target name if there is exactly one, otherwise null
329334
*/
330-
function getApplicationTargetName(project: XcodeProject): string | null {
335+
function getApplicationTargetName(applicationTargets: string[]): string | null {
336+
if (applicationTargets.length !== 1) return null;
337+
return applicationTargets[0];
338+
}
339+
340+
/**
341+
* Returns iOS application target names from PBXNativeTarget section.
342+
*/
343+
function getApplicationTargetNames(project: XcodeProject): string[] {
331344
const nativeTargets = project.pbxNativeTargetSection();
345+
const applicationTargets = new Set<string>();
332346

333347
for (const [key, value] of Object.entries(nativeTargets)) {
334348
if (key.endsWith('_comment')) continue;
@@ -340,10 +354,12 @@ function getApplicationTargetName(project: XcodeProject): string | null {
340354
const targetName = String(target?.name ?? '')
341355
.replace(/"/g, '')
342356
.trim();
343-
if (targetName) return targetName;
357+
if (targetName) {
358+
applicationTargets.add(targetName);
359+
}
344360
}
345361

346-
return null;
362+
return [...applicationTargets];
347363
}
348364

349365
/**

packages/react-native-brownfield/src/expo-config-plugin/types/ios/BrownfieldPluginIosConfig.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
export interface BrownfieldPluginIosConfig {
55
/**
66
* The name of the iOS app target
7-
* If not provided, the plugin will try to determine the application target name from the Xcode project
8-
* @default - ""
7+
* If not provided, the plugin will try to determine the application target name from the Xcode project.
8+
* Auto-detection works only when there is exactly one iOS application target.
9+
* @default ""
910
*/
1011
appTargetName?: string;
1112

0 commit comments

Comments
 (0)