Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compilation database not listing every Swift source file #14264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

2xsaiko
Copy link
Contributor

@2xsaiko 2xsaiko commented Feb 17, 2025

For SourceKit-LSP to highlight Swift sources correctly, the compile_commands.json has to contain an entry for each file with the corresponding "file" key. However, Ninja generates only one entry per build element, with the first input file as the input file for the compilation database entry, which is a problem for Swift targets consisting of multiple files since they are compiled with one command invocation per target instead of one per file. This adds entries for the build element for each input file to make LSP work for the rest of the source files to the compilation database.

@2xsaiko 2xsaiko requested a review from jpakkane as a code owner February 17, 2025 06:42
@2xsaiko 2xsaiko force-pushed the push-mkvtxuvxymym branch 2 times, most recently from 8a89ecc to 26d874b Compare March 1, 2025 00:09
d = {'directory': builddir, 'arguments': arguments, 'file': file}
jsondb_data += [d]
with open(os.path.join(builddir, 'compile_commands.json'), 'w', encoding='utf-8') as f:
json.dump(jsondb_data, f, indent=2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not a fan of hotpatching the compilation database in this manner. For example in C you can write #include<otherfile.c> and it is not reflected in the compilation db at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is about the compilation entrypoints though, C LSPs should be smart enough to follow includes, but they can't do that if none of the files are known to it. This is the latter case.

For example, a Swift target with two source files A.swift and B.swift will only have A.swift in the compilation database since both are compiled with the same rule (Swift doesn't support per-file compiles in its stable CLI interface and neither does the LSP), and this gets worse the more source files you have, making LSP work for almost no files without this patch.

Adding entries to the compilation database is the only obvious option I've seen, since Ninja doesn't have enough information to generate this itself (which input corresponds to which output file?) and also explicitly states here that only the first file will be used:

given a list of rules, each of which is expected to be a C family language compiler rule whose first input is the name of the source file, prints on standard output a compilation database in the JSON format expected by the Clang tooling interface. Available since Ninja 1.2.

(Also see ninja-build/ninja#1590)

What do you suggest instead?

Copy link
Member

@jpakkane jpakkane Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest instead?

What is the exact thing you need this information for? Or in other words, is getting the same information via Meson introspection not sufficient?

Th file in question is the compilation command database after all. It lists compilation commands. That's what it is for.

Copy link
Contributor Author

@2xsaiko 2xsaiko Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SourceKit-LSP (such as with the Swift VSCode plugin) uses this file to provide syntax highlighting/errors for source code. For it to work, it needs an entry in the compilation database for each source file.

I don't think they'll want to add explicit Meson support...

@2xsaiko 2xsaiko force-pushed the push-mkvtxuvxymym branch from 26d874b to 9088d2a Compare March 7, 2025 19:39
This makes SourceKit-LSP work correctly with multi-source-file targets.
@2xsaiko 2xsaiko force-pushed the push-mkvtxuvxymym branch from 9088d2a to dcf62f3 Compare March 25, 2025 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants