Repository: bazelbuild/bazel
Issue: xcode-locator fails in macOS-10.12
Labels: type: bug
P2
z-team-Apple

### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

```bash
git clone https://github.com/bazelbuild/examples.git
cd cpp-tutorial/stage1
bazel build //...
```

Error:
```
Starting local Bazel server and connecting to it...
INFO: Analyzed target //main:hello-world (11 packages loaded, 122 targets configured).
INFO: Found 1 target...
ERROR: /Users/soonhok/work/examples/cpp-tutorial/stage1/main/BUILD:1:1: C++ compilation of rule '//main:hello-world' failed: I/O exception during sandboxed execution: Running '/var/tmp/_bazel_soonhok/install/1a037b6c0d8096293d1eecfde6528fbd/_embedded_binaries/xcode-locator 9.2.0.9C40b' failed.
Process terminated by signal 6
stdout:
stderr: 2019-05-21 14:03:01.688 xcode-locator[938:9684] Found bundle com.apple.dt.Xcode in file:///Applications/Xcode.app/; contents on disk: (
    "file:///Applications/Xcode.app/Contents/"
)
2019-05-21 14:03:01.691 xcode-locator[938:9684] Version strings for file:///Applications/Xcode.app/: short=9.2, expanded=9.2.0
2019-05-21 14:03:01.691 xcode-locator[938:9684] -[__NSPlaceholderDictionary initWithContentsOfURL:error:]: unrecognized selector sent to instance 0x7fb1a3d01ee0
2019-05-21 14:03:01.692 xcode-locator[938:9684] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithContentsOfURL:error:]: unrecognized selector sent to instance 0x7fb1a3d01ee0'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fffb2dd065b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00007fffc7c0f48d objc_exception_throw + 48
	2   CoreFoundation                      0x00007fffb2e522d4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x00007fffb2d41cf5 ___forwarding___ + 1061
	4   CoreFoundation                      0x00007fffb2d41848 _CF_forwarding_prep_0 + 120
	5   xcode-locator                       0x0000000107b74c1c FindXcodes + 1692
	6   xcode-locator                       0x0000000107b73c01 main + 289
	7   libdyld.dylib                       0x00000001083d8235 start + 1
	8   ???                                 0x0000000000000002 0x0 + 2
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Target //main:hello-world failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 17.985s, Critical Path: 0.12s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
```

### What operating system are you running Bazel on?

macOS 10.12.6 (16G2016)

### What's the output of `bazel info release`?

```
release 0.25.2
```

### Any other information, logs, or outputs that you want to share?

Xcode version: 9.2 (9C40b)


Issue comments:
This should have been fixed by https://github.com/bazelbuild/bazel/commit/231270c67d5aa771462245531fa9b2ee7d3d0ae8, you're definitely on 0.25.2? It definitely appears to be in that tag https://github.com/bazelbuild/bazel/blob/0.25.2/tools/osx/xcode_locator.m#L181-L188

cc @sayrer 
> you're definitely on 0.25.2?

FYI, I'm using `bazelbuild/tap/bazel: stable 0.25.2`.
Can you try with the current 0.26.0 RC build? Found here https://releases.bazel.build/0.26.0/rc12/index.html
I've tried it but it didn't work:

Error message:
```
Starting local Bazel server and connecting to it...
INFO: Analyzed target //main:hello-world (11 packages loaded, 123 targets configured).
INFO: Found 1 target...
ERROR: /Users/soonhok/work/examples/cpp-tutorial/stage1/main/BUILD:1:1: C++ compilation of rule '//main:hello-world' failed: I/O exception during sandboxed execution: Running '/var/tmp/_bazel_soonhok/install/54a04d78acda2a8e5a7014da4e4d54ea/_embedded_binaries/xcode-locator 9.2.0.9C40b' failed.
Process terminated by signal 6
stdout:
stderr: 2019-05-21 14:53:15.131 xcode-locator[16168:46166] Found bundle com.apple.dt.Xcode in file:///Applications/Xcode.app/; contents on disk: (
    "file:///Applications/Xcode.app/Contents/"
)
2019-05-21 14:53:15.133 xcode-locator[16168:46166] Version strings for file:///Applications/Xcode.app/: short=9.2, expanded=9.2.0
2019-05-21 14:53:15.134 xcode-locator[16168:46166] -[__NSPlaceholderDictionary initWithContentsOfURL:error:]: unrecognized selector sent to instance 0x7faae0c02200
2019-05-21 14:53:15.134 xcode-locator[16168:46166] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithContentsOfURL:error:]: unrecognized selector sent to instance 0x7faae0c02200'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fffb2dd065b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00007fffc7c0f48d objc_exception_throw + 48
	2   CoreFoundation                      0x00007fffb2e522d4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x00007fffb2d41cf5 ___forwarding___ + 1061
	4   CoreFoundation                      0x00007fffb2d41848 _CF_forwarding_prep_0 + 120
	5   xcode-locator                       0x0000000104e01c1c FindXcodes + 1692
	6   xcode-locator                       0x0000000104e00c01 main + 289
	7   libdyld.dylib                       0x0000000105663235 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Target //main:hello-world failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 7.473s, Critical Path: 0.08s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
```

Bazel version:
```
> bazel version

Build label: 0.26.0rc12
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue May 21 09:59:36 2019 (1558432776)
Build timestamp: 1558432776
Build timestamp as int: 1558432776
```
FYI, `bazel-0.24.1` has no problem.
I've submitted https://github.com/bazelbuild/bazel/pull/8426 to fix this

Pull request review comments:

Patch that resolved the issue:
diff --git a/tools/osx/xcode_locator.m b/tools/osx/xcode_locator.m
index 90c00fafcc5b74..18f77bdab1ac14 100644
--- a/tools/osx/xcode_locator.m
+++ b/tools/osx/xcode_locator.m
@@ -177,16 +177,7 @@ static void AddEntryToDictionary(
           url, version, expandedVersion);
 
     NSURL *versionPlistUrl = [url URLByAppendingPathComponent:@"Contents/version.plist"];
-
-    // macOS 10.13 changed the signature of initWithContentsOfURL,
-    // and deprecated the old one.
-    NSDictionary *versionPlistContents;
-#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_12
-    versionPlistContents = [[NSDictionary alloc] initWithContentsOfURL:versionPlistUrl error:nil];
-#else
-    versionPlistContents = [[NSDictionary alloc] initWithContentsOfURL:versionPlistUrl];
-#endif
-
+    NSDictionary *versionPlistContents = [[NSDictionary alloc] initWithContentsOfURL:versionPlistUrl];
     NSString *productVersion = [versionPlistContents objectForKey:@"ProductBuildVersion"];
     if (productVersion) {
       expandedVersion = [expandedVersion stringByAppendingFormat:@".%@", productVersion];


Test patch: