Skip to content

Commit

Permalink
fix: xcworkspaces parsing should handle many xcprojects
Browse files Browse the repository at this point in the history
We've been notified of a case where a user legitimately has more files
in listed in their workspace than the project and pods. This would crash
when running: npm @react-native-community/cli ios

Fixes: facebook/react-native#46881
  • Loading branch information
blakef committed Oct 8, 2024
1 parent 9da12a4 commit 12877a5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/cli-platform-apple/src/tools/__tests__/getInfo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type {IOSProjectInfo} from '@react-native-community/cli-types';

import execa from 'execa';
import fs from 'fs';
import {getInfo} from '../getInfo';

jest.mock('execa', () => ({
sync: jest.fn(),
}));

jest.mock('fs', () => ({
readFileSync: jest.fn(),
}));

describe('getInfo', () => {
it('handles non-project / workspace locations in a ', () => {
const name = `YourProjectName`;
(fs.readFileSync as jest.Mock)
.mockReturnValueOnce(`<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:${name}.xcodeproj">
</FileRef>
<FileRef
location = "group:Pods/Pods.xcodeproj">
</FileRef>
<FileRef
location = "group:container/some_other_file.mm">
</FileRef>
</Workspace>`);
(execa.sync as jest.Mock).mockReturnValue({stdout: '{}'});
getInfo({isWorkspace: true, name} as IOSProjectInfo, 'some/path');

const execaSync = execa.sync as jest.Mock;
// Should not call on Pods or the other misc groups
expect(execaSync.mock.calls).toEqual([
[
'xcodebuild',
['-list', '-json', '-project', `some/path/${name}.xcodeproj`],
],
]);
});
});
4 changes: 4 additions & 0 deletions packages/cli-platform-apple/src/tools/getInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export function getInfo(
return refs.reduce<IosInfo | undefined>((result, ref) => {
const location = ref['@_location'];

if (!location.endsWith('.xcodeproj')) {
return result;
}

// Ignore the project generated by CocoaPods
if (location.endsWith('/Pods.xcodeproj')) {
return result;
Expand Down

0 comments on commit 12877a5

Please sign in to comment.