sourcekit-lsp icon indicating copy to clipboard operation
sourcekit-lsp copied to clipboard

Source generated by build plugin is ignored

Open adam-fowler opened this issue 2 years ago • 10 comments

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

adam-fowler avatar Nov 11 '22 09:11 adam-fowler

rdar://102242345

ahoppen avatar Nov 11 '22 17:11 ahoppen

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

adam-fowler avatar Dec 06 '22 13:12 adam-fowler

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

mpilman avatar Apr 08 '23 19:04 mpilman

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 :)

smumriak avatar Jul 02 '23 00:07 smumriak

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

adam-fowler avatar Jul 07 '23 16:07 adam-fowler

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.

ahoppen avatar Jul 12 '23 06:07 ahoppen

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.

dabrahams avatar Dec 21 '23 15:12 dabrahams

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 😅 .

dangdennis avatar May 03 '24 05:05 dangdennis

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.

ahoppen avatar May 03 '24 14:05 ahoppen

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

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

adam-rocska avatar May 07 '24 22:05 adam-rocska