Injecting a custom FileSystem into SourceKit?

Hi,

I'm interested in getting SourceKit running in an environment where necessary build files (e.g. source files, the stdlib binaries, other binary dependencies), are not actually on the real local filesystem.

clangd has a mechanism for dealing with this: When you start a ClangdServer, you can specify a custom FileSystemProvider implementation, and then clangd reads all files through that. The implementation can go do whatever it wants to fetch the files.

I wasn't able to find a similar mechanism in SourceKit. Would it make sense to add such a mechanism?

For example, would it make sense to add a "FileSystemProvider" argument to sourcekitd::initialize(), so that anyone can compile a SourceKit with custom file fetching logic by simply modifying the call to sourcekitd::initialize()?

1 Like

The complicated part would be to get a FileSystemProvider through all the Swift compiler APIs and its use of Clang, the sourcekitd part is the easy one I think.

I did play around a bit with that, and I think there aren't too many places to change.

I modified those places in my own copy of Swift in a hacky way, and everything seems to work!

1 Like

I'm going to prepare an actual PR for this to make it more concrete and to see what people think about it. I expect it to be pretty small.

Another thing that I might want is a mechanism like clangd's context so that I can propagate per-request data into the custom filesystem implementation. I'm not completely sure if I'll need this, and I won't put it in my initial PR.

I have made a simple proof-of-concept PR (with working tests!) that illustrates an approach: https://github.com/apple/swift/pull/24417

@akyrtzi I would really appreciate if you took a look and commented and let me know if you think this is something that I could pursue to completion and merge into master!