Getting the following error when importing Darwin or Foundation in Swift

Hi,

I keep getting this error when trying to import Darwin or Foundation using command line swift.
Anyone have any ideas?

Welcome to Swift version 5.5.2-dev.
Type :help for assistance.
1> import Foundation
:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/copyfile.h:41:9: error: unknown type name 'uint32_t'
typedef uint32_t copyfile_flags_t;
^

:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/copyfile.h:62:44: error: unknown type name 'uint32_t'
int copyfile_state_get(copyfile_state_t s, uint32_t flag, void * dst);
^

:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/copyfile.h:63:44: error: unknown type name 'uint32_t'
int copyfile_state_set(copyfile_state_t s, uint32_t flag, const void * src);
^

:30:9: note: in file included from :30::30:
import "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h"
^

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:
#include <math.h>
^

/usr/local/include/math.h:29:12: note: in file included from /usr/local/include/math.h:29:/usr/local/include/math.h:29:

include <math.h>

       ^

error: /usr/local/include/math.h:22:12: error: '/usr/include/math.h' file not found

include "/usr/include/math.h"

       ^

Thanks in advance.
Richard

What OS version are you on:

% sw_vers
ProductName:	macOS
ProductVersion:	12.2.1
BuildVersion:	21D62

And what Xcode are you using:

% xcode-select -p 
/Applications/Xcode.app/Contents/Developer

What do you see with this:

% xcrun swift --version
swift-driver version: 1.26.21 Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
Target: x86_64-apple-macosx12.0

And if you run swift via xcrun:

% xcrun swift
Welcome to Swift version 5.5.2-dev.
Type :help for assistance.
  1> import Foundation
  2> ^D

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

$ sw_vers
ProductName: macOS
ProductVersion: 12.2
BuildVersion: 21D49

$ xcode-select -p
/Applications/Xcode.app/Contents/Developer

$ xcrun swift --version
swift-driver version: 1.26.21 Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
Target: x86_64-apple-macosx12.0

$ xcrun swift
Welcome to Swift version 5.5.2-dev.
Type :help for assistance.
1> import Foundation
:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/copyfile.h:41:9: error: unknown type name 'uint32_t'
typedef uint32_t copyfile_flags_t;
^

:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/copyfile.h:62:44: error: unknown type name 'uint32_t'
int copyfile_state_get(copyfile_state_t s, uint32_t flag, void * dst);
^

:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/copyfile.h:63:44: error: unknown type name 'uint32_t'
int copyfile_state_set(copyfile_state_t s, uint32_t flag, const void * src);
^

:30:9: note: in file included from :30::30:
import "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h"
^

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:
#include <math.h>
^

/usr/local/include/math.h:29:12: note: in file included from /usr/local/include/math.h:29:/usr/local/include/math.h:29:

include <math.h>

       ^

error: /usr/local/include/math.h:22:12: error: '/usr/include/math.h' file not found

include "/usr/include/math.h"

       ^

Thanks in advance

FYI, if you use triple backquote delimiters, your code blocks won’t get interpreted by the Markdown parser.

So your setup looks very similar to mine [1], suggesting two theories:

  • There’s something broken with your Xcode.

  • There’s something weird about your user configuration.

We can test both of these.


To test your Xcode:

% codesign -v -v --strict --deep /Applications/Xcode.app                                                                            
/Applications/Xcode.app: valid on disk
/Applications/Xcode.app: satisfies its Designated Requirement

This will take a while, enough time for a tea break!

What does this show?


To test your user configuration:

  1. Use System Preferences > Users & Groups to create a new test user.

  2. Log in as that user.

  3. Repeat the steps from my first response.

Do things still fail in that context?

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

[1] The difference between macOS 12.2 and 12.2.1 doesn’t matter in this context, although you should update because the latter some some important security fixes.

Hi Quin,

Thanks for your help so far.

$ codesign -v -v --strict --deep /Applications/Xcode.app
/Applications/Xcode.app: the code cannot be read by the verifier (file system permissions etc.)

This is surprising since I only completely reinstalled Xcode recently.

I will test a new user now.

Cheers
Richard

Hi Quinn,

I just tested with a new user.

I got the same error.

Cheers
Richard

Hi Quinn,

I tried using sudo and got the expected result.

$ sudo codesign -v -v --strict --deep /Applications/Xcode.app
Password:
/Applications/Xcode.app: valid on disk
/Applications/Xcode.app: satisfies its Designated Requirement

I just tested with a new user.

OK. That suggests that Xcode itself is the problem.

I only completely reinstalled Xcode recently.

How did you install Xcode? From the Mac App Store? Or from the developer web site?

Note I generally recommend the latter, for the reasons I outlined in this thread on DevForums

If you add more -v options to the codesign command, does it tell you which file is the problem?

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

Hi Quinn,

I added the extra '-v' option and this is what I got.

I can't tell if there's anything wrong.

I'm using Xcode from the App Store. I'm downloading the Xcode from the developer's site now.

Cheers
Richard

The output was too large to put on the reply. So I put it in a text file on drop box.
The link is

Hi Quin,

I've downloaded the developer's site Xcode. Xcode-beta.app
How do I make swift point to this version of Xcode?

Cheers
Richard

The output was too large to put on the reply. So I put it in a text
file on drop box.

Thanks. Unfortunately that shows the working case, where you ran it with sudo, and I was looking for the failing case.

I've downloaded the developer's site Xcode. Xcode-beta.app

OK, just to be clear, the non-beta versions of Xcode are available from the developer web site. It’s fine to use the beta if you like, but just don’t think that you have to use it.

How do I make swift point to this version of Xcode?

When you run a developer tool from the command-line, it ‘trampolines’ over to the tool in your currently selected Xcode. To select a specific Xcode, use xcode-select. For more on that, see its man page.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Hi Quin,

This is what I got after I switch path to the beta version of Xcode.

Not sure what the extension point errors are... Maybe I missed something?

Still getting the same error though....

Thanks
Richard

$ sudo xcode-select --switch /Users/richardlam/workDev/Xcode-beta.app
Password:
(base) [richardlam@RLMacBook-Pro-2018 workDev]$ swift
2022-03-09 09:49:46.364 xcodebuild[45517:1960526] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionSentinelHostApplications for extension Xcode.DebuggerFoundation.AppExtensionHosts.watchOS of plug-in com.apple.dt.IDEWatchSupportCore
2022-03-09 09:49:46.364 xcodebuild[45517:1960526] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionPointIdentifierToBundleIdentifier for extension Xcode.DebuggerFoundation.AppExtensionToBundleIdentifierMap.watchOS of plug-in com.apple.dt.IDEWatchSupportCore
2022-03-09 09:49:47.665 xcodebuild[45518:1960554] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionSentinelHostApplications for extension Xcode.DebuggerFoundation.AppExtensionHosts.watchOS of plug-in com.apple.dt.IDEWatchSupportCore
2022-03-09 09:49:47.665 xcodebuild[45518:1960554] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionPointIdentifierToBundleIdentifier for extension Xcode.DebuggerFoundation.AppExtensionToBundleIdentifierMap.watchOS of plug-in com.apple.dt.IDEWatchSupportCore
Apple Swift version 5.6 (swiftlang-5.6.0.323.60 clang-1316.0.20.8)
Target: x86_64-apple-macosx12.0

Welcome to Swift!

Subcommands:

swift build Build Swift packages
swift package Create and work on packages
swift run Run a program from a package
swift test Run package tests
swift repl Experiment with Swift code interactively (default)

Use swift --help for descriptions of available options and flags.

Use swift help <subcommand> for more information about a subcommand.

Welcome to Apple Swift version 5.6 (swiftlang-5.6.0.323.60 clang-1316.0.20.8).
Type :help for assistance.
1> import Darwin
expression failed to parse:
:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Users/richardlam/workDev/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/copyfile.h:41:9: error: unknown type name 'uint32_t'
typedef uint32_t copyfile_flags_t;
^

:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Users/richardlam/workDev/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/copyfile.h:62:44: error: unknown type name 'uint32_t'
int copyfile_state_get(copyfile_state_t s, uint32_t flag, void * dst);
^

:5:9: note: in file included from :5::5:
import "copyfile.h"
^

error: /Users/richardlam/workDev/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/copyfile.h:63:44: error: unknown type name 'uint32_t'
int copyfile_state_set(copyfile_state_t s, uint32_t flag, const void * src);
^

:30:9: note: in file included from :30::30:
import "/Users/richardlam/workDev/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h"
^

/Users/richardlam/workDev/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:10: note: in file included from /Users/richardlam/workDev/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:/Users/richardlam/workDev/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include/tgmath.h:13:
#include <math.h>
^

/usr/local/include/math.h:29:12: note: in file included from /usr/local/include/math.h:29:/usr/local/include/math.h:29:

include <math.h>

       ^

error: /usr/local/include/math.h:22:12: error: '/usr/include/math.h' file not found

include "/usr/include/math.h"

       ^

1>

Still getting the same error though

Wow, that’s quite weird. If you launch the Xcode app and then create a new project from the macOS > Command Line Tool target, are you about to build, run, and debug that?

Does your Mac have SIP enabled? You can test that with the following command:

% csrutil status
System Integrity Protection status: enabled.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Wow, that’s quite weird. If you launch the Xcode app and then create a new project from the macOS > Command Line Tool target, are you about to build, run, and debug that?

I can build, run and debug it with no problem. Everything works using the Xcode app.
It's only when I run swift on the command line that I have issues.

% csrutil status

I get the following:

[richardlam@RLMacBook-Pro-2018 workDev]$ csrutil status
System Integrity Protection status: enabled.

Cheers
Richard

It's only when I run swift on the command line that I have issues.

OK.

Do you have system-like headers installed in /usr/local/include? Looking at the log output you posted [1], I see paths like /usr/local/include/math.h. Does that file exist? If so, it’s possible that it’s messing up the C importer.

If you do have such files, temporarily move the whole /usr/local/include directory aside and see if that improves things.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

[1] Again, if you surround text with triple backquote delimiters, that’ll make it render nicely.

Hi Quinn,

Moving /usr/local/include seems to fix it. Thanks
However I'm not sure what was using the /usr/local/include.

It must have been something else I installed. What's the best practice for these header files?
I might run into problems once I start using something else which uses these header files.

Thanks again
Cheers
Richard

Do you have Homebrew installed? Unless you are using a non-standard location, /usr/local/include is where Homebrew installs packages. It is searched first before /usr/include, so if a Homebrew package installed its own version of the C std headers (math.h, stdio.h, etc.), then they will take precedence over the system header files and cause the problems you've having. /usr/local/include is also a popular place for other packages.

I would try an experiment of renaming /usr/local/include/math.h to something else, then try running things to see if it makes a difference or if you run into other problems. It may be superflous.

Thanks Jon,

Yes I have Homebrew installed. Maybe if I do an update on all the Homebrew packages, it might fix any packages which uses /usr/local/include?

Cheers
Richard

It might. However, if it's a package that hasn't been updated since you last updated, you will still have the problem.

Do you know if you've installed anything else besides Homebrew packages in /usr/local? They could be culprits, too. Do you have the GCC suite installed?

Sorry. Don't know which package might be using /usr/local/include? Could be any number of things.
I do have gcc installed. I should probably reinstall it..?

Cheers
Richard