swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.76k stars 1.35k forks source link

[SR-11504] for some reason, /usr/local/include takes priority over the SDK #4662

Open weissi opened 5 years ago

weissi commented 5 years ago
Previous ID SR-11504
Radar rdar://problem/55601495
Original Reporter @weissi
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Package Manager | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: b40fb2b5182a5282640ecfe2f7c53671

Issue Description:

Even if not asked to add any directories to the include path, when invoking (the) clang (importer), /usr/local/include seems to take priority over the SDK.

Repro:

git clone https://github.com/apple/swift-nio
sudo mkdir -p /usr/local/include
sudo bash -c 'echo "#error \"this should not happen\"" > /usr/local/include/string.h'
cd swift-nio
swift build

Note, to unbrick your computer, run sudo rm -f /usr/local/include/string.h.

Result:

[...]
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/mach/mach_vm.h:6:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/mach/mach_vm.h:6:
#include <string.h>
         ^
/usr/local/include/string.h:1:2: error: "this should not happen"
#error "this should not happen"
 ^
<module-includes>:436:9: note: in file included from <module-includes>:436:
#import "libc.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/libc.h:37:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/libc.h:37:
#include <string.h>
         ^
/usr/local/include/string.h:1:2: error: "this should not happen"
#error "this should not happen"
 ^
<unknown>:0: error: could not build Objective-C module 'Darwin'
weissi commented 5 years ago

@swift-ci create

weissi commented 5 years ago

CC @belkadan/@ddunbar/@aciidb0mb3r

weissi commented 5 years ago

Xcode has no trouble building swift-nio even in the presence of the bogus string.h so I'd say this is a SwiftPM issue, tagging it as such for now even through it might be a bogus default in clang/swiftc.

belkadan commented 5 years ago

I think we looked into this before and it turns out to come from how the command-line tools work on macOS without being invoked through xcrun, or maybe without an explicit SDK (either from xcrun or from -sdk). Ankit, do you have the discussion for that?

belkadan commented 5 years ago

(As in, this is expected behavior on macOS and we aren't going to change it.)

weissi commented 5 years ago

@belkadan wait, xcrun swift build would workaround this?

aciidgh commented 5 years ago

This is xcrun's behavior when an explicit SDK is not passed and `swift build` goes through xcrun on macOS. From xcrun's man page:

       CPATH
          This  environment variable is modified by xcrun to include /usr/local/include when an explicit SDK is not requested via environment vari-
          able nor command line argument and neither -nostdinc nor -nostdsysteminc are present.

@weissi It'll work if you run `xcrun -sdk macosx swift build`

This has known to cause one other similar issue so far. I don't know if it's a good idea for SwiftPM to try to detect and unset this env variable.

weissi commented 5 years ago

@aciidb0mb3r oh nice, thank you! Hmm, I think SwiftPM and Xcode should behave the same by default?

aciidgh commented 5 years ago

I agree that they should but this is an unfortunate bug because of the implementation details of the underlying tools. There's no way to properly detect if CPATH was set by xcrun or not.

weissi commented 5 years ago

yup...