Swift 5.4 on Windows: error: missing required modules: 'CFXMLInterface', 'CoreFoundation'

When building a sample application using XMLParser, this project builds and runs properly on macOS 11.3, but fails with the following output on a shiny new Windows 10 installation built following the instructions on the Download page of swift.org:

C:\Users\Me\Desktop\XML>swift build
warning: Failed creating default cache locations, Error Domain=NSCocoaErrorDomain Code=256 "(null)"
warning: Failed creating default cache locations, Error Domain=NSCocoaErrorDomain Code=256 "(null)"
[1/1] Planning build
[1/1] Planning build

* Build Completed!
[1/3] Compiling XMLTest main.swift
C:\Users\Matt Stoker\Desktop\XML\main.swift:3:8: error: missing required modules: 'CFXMLInterface', 'CoreFoundation'
 import FoundationXML
           ^

Package.swift

// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "XMLTest",
    platforms: [
        .macOS(.v10_15),
        .iOS(.v13),
        .tvOS(.v13),
        .watchOS(.v6),
    ],
    products: [
        .executable(
            name: "xml-test",
            targets: ["XMLTest"]
        ),
    ],
    dependencies: [
    ],
    targets: [
        .executableTarget(
            name: "XMLTest",
            path: "."
        )
    ]
)

main.swift

import Foundation
#if canImport(FoundationXML)
import FoundationXML
#endif

let svg = 
"""
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0.0,0.0,936.0 1008.0">
    <path stroke="black" fill="lightblue" d="
        M 0.0,0.0 L 936.0,0.0 L 936.0,1008.0 L 0.0,1008.0 L 0.0,0.0 Z
    "/>
</svg>
"""
guard let svgData = svg.data(using: .utf8) else { fatalError() }
let parserDelegate = SVGParserDelegate()
let parser = XMLParser.init(data: svgData)
parser.delegate = parserDelegate
print("Parsing...")
let parseCompleted = parser.parse()
print("Parse Completed. Result: \(parseCompleted)")

class SVGParserDelegate: NSObject, XMLParserDelegate {
    func parserDidStartDocument(_ parser: XMLParser) {
    }

    func parserDidEndDocument(_ parser: XMLParser) {
    }

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes: [String: String] = [:]) {
        switch elementName.lowercased() {
        case "svg", "g":
            print("Started group tag")
        case "path":
            guard let pathData = attributes["d"] else {
                print("Started path tag with no path data")
                return
            }
            print("Started path: \(pathData)")
        default:
            break
        }
    }

    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        switch elementName.lowercased() {
        case "svg", "g":
            print("Ended group tag")
        case "path":
            print("Ended path tag")
        default:
            break
        }
    }

    func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
        print("Parse Error: \(parseError)")
    }
}

This appears to be the correct way to import Foundation's XML functionality. Anyone have ideas on why the standard library modules can't be found?

[SR-14578] Unable to import FoundationXML on Windows - Swift

It's a known issue, that has been fixed for a while, but the change accidentally didn't make it into 5.4. This should be fixed in the 5.5 snapshots, and I hope that we may be able to pull in the fix in a dot-release for 5.4.

As to why the problem exists: CoreFoundation is not meant to be public, and the Windows port has generally been far more aggressive about preventing leakage of internal interfaces and encouraging cross-compilation.

1 Like

We started building with Swift 5.4.2 and I can confirm that this is fixed! Importing FoundationXML works correctly when building on Windows 10 64-bit. Our code for Windows now has all the features that iOS, macOS, Linux, and Android has. Thanks @compnerd !