events.action: opened
events.author: danielcompton
events.masked_author: username_0
events.text: ### Description of the problem
  
  I think there are two compounding issues here:
  
  1. xcode-locator chooses an Xcode on my cloned backup drive (`Macintosh SSD`) over my primary boot drive (`MacintoshNVME`). 
  2. Additionally, it does so without quoting the spaces in the drive name, so the clang build fails.
  
  <pre>
  bazel clean; and bazel build ratelimit --verbose_failures --sandbox_debug
  ...
    (cd /private/var/tmp/_bazel_daniel/c1e18630777b604bf59a4d2d2e564069/execroot/smyte && \
    exec env - \
      APPLE_SDK_PLATFORM='' \
      APPLE_SDK_VERSION_OVERRIDE='' \
      <strong>DEVELOPER_DIR='/Volumes/Macintosh SSD/Applications/Xcode.app/Contents/Developer' \</strong>
      PATH={elided}
      SDKROOT='/Volumes/Macintosh SSD/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk' \
  ...
  clang: <strong>error: no such file or directory: 'SSD/Applications/Xcode.app</strong>/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'
  clang: <strong>warning: no such sysroot directory: '/Volumes/Macintosh'</strong> [-Wmissing-sysroot]
  Target //ratelimit:ratelimit failed to build
  ...
  FAILED: Build did NOT complete successfully
  </pre>
  
  ### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
  
  * Setup a clone of your boot drive onto another drive on the Mac. Name it `Macintosh SSD`
  * Clone https://github.com/smyte/smyte-db
  * Run `bazel clean && bazel build ratelimit`
  
  ### What operating system are you running Bazel on?
  
  macOS 10.13.5 (High Sierra)
  
  ### What's the output of `bazel info release`?
  
  release 0.15.0-homebrew
  
  ### What's the output of `git remote get-url origin ; git rev-parse master ; git rev-parse HEAD` ?
  
  ```
  https://github.com/smyte/smyte-db.git
  34129e0184408086cde6fe9694d9a7d7ca9c2016
  34129e0184408086cde6fe9694d9a7d7ca9c2016
  ```
  
  ###  Have you found anything relevant by searching the web?
  
  I've looked, but couldn't see anything for this issue. It was hard to search though, as there are lots of other hits for issues with Xcode-locator which weren't relevant here.
  
  ### Any other information, logs, or outputs that you want to share?
  
  If I eject the drive and run a full `bazel clean --expunge` then I can get further in the build process. It still fails, but it doesn't look to be related to this issue.
  
  Apologies for the maximal test case, I've never used bazel before, and am not very familiar with the C/C++ ecosystem to be able to make a smaller case.
  
  If I run `Xcode-locator` with the drive attached from the temporary build root I get:
  
  ```console
  $ _bin/xcode-locator
  {
  	"9.3": "/Applications/Xcode-beta.app/Contents/Developer",
  	"9.3.0": "/Volumes/Macintosh SSD/Applications/Xcode-beta.app/Contents/Developer",
  	"9": "/Applications/Xcode-beta.app/Contents/Developer",
  	"9.4.1": "/Volumes/Macintosh SSD/Applications/Xcode.app/Contents/Developer",
  	"9.4": "/Applications/Xcode.app/Contents/Developer",
  }
  ```
  
  If I run it after detaching the drives I get 
  
  ```console
  $ _bin/xcode-locator
  {
  	"9.3": "/Applications/Xcode-beta.app/Contents/Developer",
  	"9.3.0": "/Volumes/MacintoshNVME/Applications/Xcode-beta.app/Contents/Developer",
  	"9": "/Applications/Xcode-beta.app/Contents/Developer",
  	"9.4.1": "/Applications/Xcode.app/Contents/Developer",
  	"9.4": "/Applications/Xcode.app/Contents/Developer",
  }
  ```
  
  Full build output:
  
  <details>
  <pre>
  $ bazel clean; and bazel build ratelimit --verbose_failures --sandbox_debug
  INFO: Starting clean.
  INFO: Analysed target //ratelimit:ratelimit (45 packages loaded).
  INFO: Found 1 target...
  ERROR: /private/var/tmp/_bazel_daniel/c1e18630777b604bf59a4d2d2e564069/external/hiredis_git/BUILD.bazel:1:1: C++ compilation of rule '@hiredis_git//:hiredis_c' failed (Exit 1): sandbox-exec failed: error executing command
    (cd /private/var/tmp/_bazel_daniel/c1e18630777b604bf59a4d2d2e564069/execroot/smyte && \
    exec env - \
      APPLE_SDK_PLATFORM='' \
      APPLE_SDK_VERSION_OVERRIDE='' \
      DEVELOPER_DIR='/Volumes/Macintosh SSD/Applications/Xcode.app/Contents/Developer' \
      PATH={redacted} \
      SDKROOT='/Volumes/Macintosh SSD/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk' \
      TMPDIR=/var/folders/rg/ghxj82wn4gq5cwgncr08n37w0000gr/T/ \
      XCODE_VERSION_OVERRIDE=9.4.1 \
    /usr/bin/sandbox-exec -f /private/var/tmp/_bazel_daniel/c1e18630777b604bf59a4d2d2e564069/sandbox/darwin-sandbox/48/sandbox.sb /private/var/tmp/_bazel_daniel/c1e18630777b604bf59a4d2d2e564069/execroot/smyte/_bin/process-wrapper '--timeout=0' '--kill_delay=15' external/local_config_cc/wrapped_clang '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -O0 -DDEBUG -iquote external/hiredis_git -iquote bazel-out/darwin-fastbuild/genfiles/external/hiredis_git -iquote external/bazel_tools -iquote bazel-out/darwin-fastbuild/genfiles/external/bazel_tools -Ibazel-out/darwin-fastbuild/bin/external/hiredis_git/_virtual_includes/hiredis_c -MD -MF bazel-out/darwin-fastbuild/bin/external/hiredis_git/_objs/hiredis_c/external/hiredis_git/dict.d '-isysroot __BAZEL_XCODE_SDKROOT__' -Wno-unused-function -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c external/hiredis_git/dict.c -o bazel-out/darwin-fastbuild/bin/external/hiredis_git/_objs/hiredis_c/external/hiredis_git/dict.o)
  clang: error: no such file or directory: 'SSD/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'
  clang: warning: no such sysroot directory: '/Volumes/Macintosh' [-Wmissing-sysroot]
  Target //ratelimit:ratelimit failed to build
  INFO: Elapsed time: 14.623s, Critical Path: 0.39s
  INFO: 45 processes: 45 darwin-sandbox.
  FAILED: Build did NOT complete successfully
  </pre>
  </details>
  
  Running a Swift Playground on the machine with the extra drive detached prints
  
  ```
  import Foundation
  
  let arr = LSCopyApplicationURLsForBundleIdentifier("com.apple.dt.Xcode" as CFString, nil)
  print(arr!)
  ```
  
  ```
  Unmanaged<CFArray>(_value: <__NSArrayI 0x7fd94084bfc0>(
  file:///Applications/Xcode.app/,
  file:///Applications/Xcode-beta.app/,
  file:///Volumes/MacintoshNVME/Applications/Xcode-beta.app/
  )
  )
  ```
  
  I'll update this report shortly with the drive re-attached.
events.title: xcode-locator chooses Xcode on cloned drive over boot drive
events.type: issue
events.action: created
events.author: jin
events.masked_author: username_1
events.text: cc @username_2
events.type: comment
events.action: created
events.author: jmmv
events.masked_author: username_2
events.text: I don't think we can do anything here. We could add heuristics to prefer the local drive... but then surely someone would want the opposite behavior.
  
  Separately, I'm pretty sure Bazel doesn't handle spaces in paths for many reasons. So I don't think it's worth keeping this bug open just for the Xcode case...
events.type: comment
events.action: closed
events.author: jmmv
events.masked_author: username_2
events.type: issue
events.action: created
events.author: troberti
events.masked_author: username_3
events.text: I got a similar problem, where Bazel is trying to select a developer dir on a Time Machine backup drive. Surely the Time Machine volume should be excluded.
  
  ```
  DEBUG: /private/var/tmp/_bazel_tijmen/3067a3892c7a5fd8ba6cbb960045e8bd/external/bazel_tools/tools/osx/xcode_configure.bzl:89:14: Invoking xcodebuild failed, developer dir: /Volumes/Durandal/Backups.backupdb/Marathon/2020-08-20-091648/Leela - Data/Applications/Xcode-beta.app/Contents/Developer ,return code 256, stderr: Timed out, stdout: 
  ```
  
  This then times out the "local_config_cc" step, and bazel does not work.
events.type: comment
events.action: created
events.author: keith
events.masked_author: username_4
events.text: If you're add the time machine backup to spotlight's ignored directories does that fix it? https://apple.stackexchange.com/a/10086
events.type: comment
events.action: created
events.author: troberti
events.masked_author: username_3
events.text: I tried that, but it gives a notice that you cannot exclude Time Machine backup folders. Presumably that is done automatically.
  
  I fixed the issue by unmounting the Time Machine drive, and then running bazel works as fast as expected. Otherwise it hangs and fails eventually.
events.type: comment
events.action: created
events.author: keith
events.masked_author: username_4
events.text: pretty interesting, I also have backups setup similar to this (not with time machine) and those don't appear in xcode-locators output, theoretically if we could find some heuristic to ignore them it would go here https://github.com/bazelbuild/bazel/blob/f572d3ba1e6c977bace5c638da1628724f7b3e1f/tools/osx/xcode_locator.m#L125-L135
events.type: comment
events.action: created
events.author: danielcompton
events.masked_author: username_0
events.text: Apple provides an API [CSBackupSetItemExcluded](https://developer.apple.com/documentation/coreservices/1445043-csbackupsetitemexcluded?language=objc) to mark directories as excluded by Time Machine, that might be readable?
events.type: comment
events.action: created
events.author: keith
events.masked_author: username_4
events.text: you can try changing bazel to read this to see https://developer.apple.com/documentation/coreservices/1443602-csbackupisitemexcluded?language=objc
events.type: comment