swiftlang / sourcekit-lsp

Language Server Protocol implementation for Swift and C-based languages
Apache License 2.0
3.31k stars 275 forks source link

Source generated by build plugin is ignored #665

Open adam-fowler opened 1 year ago

adam-fowler commented 1 year ago

The source code generated by a SwiftPM build plugin does not get parsed by SourceKit-LSP. So any code referencing generated code is considered an error and "Go to definition..." etc don't work

I have a sample project here https://github.com/adam-fowler/soto-codegenerator-plugin-test. It has an AWS model file that defines the API for the SNS service from AWS. The build plugin generates source code from this and the generated code is used in the file SNSTest.swift. SourceKit-LSP reports 4 problems in this file even though the project builds fine

ahoppen commented 1 year ago

rdar://102242345

adam-fowler commented 1 year ago

It looks like this is not completely true. I can get code completion etc to work by restarting the sourcekit-lsp server. I'm guessing this is a similar issue to #620

mpilman commented 1 year ago

I am running into the same problem. In my project I am using protobuf, grpc, and a custom code generator and LSP isn't able to find any of the generated code (not even after restarting VSCode, LSP, reloading the window, clean and rebuild...)

I created a tiny [reproduction project(https://github.com/mpilman/ProtoRepro) (on macOS the protoc has to be installed and the environment variable PROTOC_PATH=/opt/homebrew/bin/protoc has to be set in order for this to build).

This is what the swift-file looks like in VSCode after the project has been successfully built:

image
smumriak commented 1 year ago

I've moved my generated code from manually running the tool to being a plugin yesterday and I'm affected by this too. The repo is located here, tho it's not a tiny example :)

adam-fowler commented 1 year ago

This appears to be resolved in the latest swift 5.9 toolchain. But we still have the issue where we cannot use go to definition within generated code.

EDIT: This still needs a LSP restart

ahoppen commented 1 year ago

The problem for not getting any semantic functionality in the generated files is that we don’t get compiler arguments for them. As I understand, being able to get them requires a couple of non-trivial changes to SwiftPM, which we’re now tracking in https://github.com/apple/swift-package-manager/issues/6700.

dabrahams commented 10 months ago

FWIW: Restarting SourceKit doesn't help me at all; I need to manually copy the generated source file into my project folder to get things to work.

dangdennis commented 5 months ago

I've encountered this issue since I've started a swift vapor project with vscode. But suddenly the lsp is able to parse the generated files after I switched to xcode today to develop the module that uses the swift openapi plugin and exports the generated source code.

A little unwilling to try to replicate by deleting my .build since it's actually working for once 😅 .

ahoppen commented 5 months ago

FWIW, a workaround for now is to generate all files into their own module with public access level (and leave the module otherwise empty) and then import that module with the generated code.

adam-rocska commented 5 months ago

@ahoppen ’s tip seems to work for me too. Quite hacky though 😅

EDIT: In my case, I made it package scoped.