Skip to content

Conversation

BruceMcRooster
Copy link
Contributor

@BruceMcRooster BruceMcRooster commented Oct 18, 2025

TL;DR I went down the rabbit hole to figure out what was causing #42. This fixes #42 and more broadly ensure full support for loading queries on all platforms.

As discussed in #42, I was experimenting and running into issues where LanguageConfiguration(_ tsLanguage: OpaquePointer, name: String) would fail to load some language query files because they couldn't find them. I did some investigative work, and discovered several issues in how file paths for these queries are looked up (the bundleQueriesDirectoryURL function).
The error I was running into was thrown when it was discovered the file returned from bundleQueriesDirectoryURL didn't exist.

guard let queriesURL = Self.bundleQueriesDirectoryURL(for: bundleName) else {
throw LanguageConfigurationError.queryDirectoryNotFound
}
let path = queriesURL.standardizedFileURL.path
if FileManager.default.isReadableFile(atPath: path) == false {
throw LanguageConfigurationError.queryDirectoryNotReadable(queriesURL)
}

The crux of the problem is that when targetting macOS, SwiftPM and Xcode may generate different bundle structures. The old structure just assumed that, on macOS, the queries directory it was looking for to load language queries was at <LanguageBundle>.bundle/Content/Resources/queries. This was only the case in certain scenarios when building with Xcode. SwiftPM (which I was using for experimenting) would use the same flattened structure (<LanguageBundle>.bundle/queries) already discovered for other Apple platforms. I couldn't nail down when this would happen, or more specifically a build flag to check when this happens, so I just included a runtime check on macOS.
I decided to test my code on Linux because I assumed it was previously working and didn't want to cause any regressions. I used the official Docker image, running a sample project with docker run --rm -v "$(pwd):/src" -w "/src" swift:latest swift run, and discovered the problem was far from solved there. It turns out SwiftPM, when compiling for Linux (and from my research Windows as well, though I don't have a way to test that), instead stores the language queries at <LanguageBundle>.resources/queries. I added an additional conditional compilation for that.
Then I decided to be extra thorough and test on basically every platform Xcode offered in its dropdown. As expected, iOS, visionOS, tvOS, and watchOS worked perfectly based on the assumption of the flattened structure. Then I tried running on Mac Catalyst because it was the last thing I'd yet to try. It failed because it used the other macOS-style structure. I changed the conditional compilations to account for this.

When targeting macOS (and Mac Catalyst, after testing), SwiftPM and Xcode
may generate different bundle structures. I couldn't figure out a better way
to address this than to use runtime checks.
When targeting Linux (tested) and Windows (untested, but fairly certain),
Swift uses .resources instead of .bundle, which meant this query didn't work
at all on those platforms.
@mattmassicotte
Copy link
Contributor

Fantastic stuff, thank you so much. Incredible how difficult this has turned out to be.

@mattmassicotte mattmassicotte merged commit 39236f7 into tree-sitter:main Oct 20, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LanguageConfiguration fails to load queries in macOS 26 using SwiftPM

2 participants