CoreSpotlight indexing fails with mysterious errors if AppSandbox isn't explicitly disabled in Xcode

Originator:aditya
Number:rdar://37340318 Date Originated:08-Feb-2018 10:41 am
Status:Open Resolved:
Product:macOS + SDK Product Version:10.13.3
Classification:Serious Bug Reproducible:Always
 
Summary:
In a project in which the AppSandbox setting is never touched (under the Capabilities) for the target, all `CSSearchableIndex` operations fail with the error:

Error Domain=CSIndexErrorDomain Code=-1003 "(null)" UserInfo={NSUnderlyingError=0x60400044a020 {Error Domain=NSCocoaErrorDomain Code=4097 "Couldn’t communicate with a helper application."}}

Filtering by "spotlight" in macOS’s Console.app, the following messages also appear:
corespotlightd	com.apple.corespotlight	Could not resolve bundle from <xpc object>
corespotlightd	com.apple.corespotlight	Could not resolve bundle id for <xpc object>
CSTest	com.apple.corespotlight	Retrying <CSSearchableIndexRequest:0x60c00008bf90; id=0, label="delete-all-items", index="SomeTestIndex"/0x60c0000a46e0, retry=1/1> from Error Domain=NSCocoaErrorDomain Code=4097 "Couldn’t communicate with a helper application."
corespotlightd	com.apple.corespotlight	Could not resolve bundle from <xpc object>
corespotlightd	com.apple.corespotlight	Could not resolve bundle id for <xpc object>
corespotlightd	com.apple.corespotlight	Could not resolve bundle from <xpc object>
corespotlightd	com.apple.corespotlight	Could not resolve bundle id for <xpc object>
CSTest	com.apple.corespotlight	Retrying <CSSearchableIndexRequest:0x60400008a000; id=1, label="index-items", index="SomeTestIndex"/0x60c0000a46e0, retry=1/1> from Error Domain=NSCocoaErrorDomain Code=4097 "Couldn’t communicate with a helper application."
corespotlightd	com.apple.corespotlight	Could not resolve bundle from <xpc object>
corespotlightd	com.apple.corespotlight	Could not resolve bundle id for <xpc object>



Steps to Reproduce:
1. Run the attached sample application

Expected Results:
The indexing of the searchable item completes successfully and "Index completed successfully" is logged to the console.

Actual Results:
The aforementioned error occurs, and "An error occurred when indexing" is logged.

Version:
10.13.3

Notes:
A workaround for this issue is to toggle the "App Sandbox" capability for the app target. This adds the following section to the pbxproj, which somehow sets a flag in the binary that allows communication with corespotlightd:

SystemCapabilities = {
            com.apple.ApplicationGroups.Mac = {
            enabled = 0;
        };
        com.apple.Sandbox = {
             enabled = 0;
        };
    };
};

Comments

Aditya Krishnadevan

Even though this works as intended, the errors returned from CSSearchableIndex do not reflect this at all, and makes it really difficult to figure out what the issue this. This should be be documented on CSSearchableIndex too, as one doesn't typically associate a setting in Xcode with runtime failures (as evidenced by https://stackoverflow.com/questions/47392032/unknown-error-when-adding-an-cssearchableitem-to-core-spotlight-macos/)

Apple Developer Relations

Engineering has determined that this issue behaves as intended based on the following information: CoreSpotlight can only be used to index/search items associated with bundle of application. App needs to be signed (attached project has code-signing off).

Aditya Krishnadevan

The workaround I noted in the original post only works when SIP is disabled. When SIP is enabled, this error occurs until Development Signing is enabled. The error should definitely reflect that, as this was pretty difficult to figure out.

Aditya Krishnadevan

Note that this issue will not happen with a newly created project, as that has the Sandbox enabled by default. You can reproduce this in a new project by deleting the SystemCapabilities entry from the pbxproj

Aditya Krishnadevan

https://github.com/PSPDFKit-labs/radar.apple.com/tree/master/37340318%20-%20CoreSpotlight%20Sandbox%20Failure%20macOS


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!