Fix bundleQueriesDirectoryURL to work on all platforms #43
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 (thebundleQueriesDirectoryURL
function).The error I was running into was thrown when it was discovered the file returned from
bundleQueriesDirectoryURL
didn't exist.swift-tree-sitter/Sources/SwiftTreeSitter/LanguageConfiguration.swift
Lines 74 to 82 in 114a515
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.