SolaWing / xcode-build-server

a build server protocol implementation for integrate xcode with sourcekit-lsp
MIT License
428 stars 19 forks source link

Does it work with Clang or ObjectiveC? #50

Closed TMTBO closed 8 months ago

TMTBO commented 8 months ago

Hello, I use this plugin with Swift/SwiftUI project, and it works perfectlly. But LSP does not works with ObjectiveC project without .compile file or compile_commands.json file. Is it necessary to generate a .compile file or compile_commands.json file.

SolaWing commented 8 months ago

Yes, it support all languages which xcode supported, including c, cpp, objc, objc++, as long as you config correctly.

for config correctly, first you should generate buildServer.json at your repo root, and build it, like Readme says, and depend on your editor, you should also ensure config your soucekit-lsp to support c-language familly (which may default only active for swift).

Is it necessary to generate a .compile file or compile_commands.json file.

.compile is generate by xcode-build-server, it is need to xcode-build-server to work. if you config as bind xcode log, this file will store in ~/Library/Cache/xcode-build-server and won't pollute your local dir. compile_commands.json is annother flags source which used by clangd. though it can work as a source, it is redundant as xcode-build-server will provide same info for xcode projects

TMTBO commented 8 months ago

I have make the sourcekit-lsp works with objc files. However, it does not seem to recognize some system symbols, nor does it recognize code introduced by cocoapods.

image
SolaWing commented 8 months ago

@TMTBO you can export environment SOURCEKIT_LOGGING=3 to see detail logs from sourcekit-lsp. this should check if the sourcekit-lsp works correctly.

also notice sourcekit-lsp use objective-c, objective-cpp as the filetype identifier, but vim's file type is objc. it need convert for sourcekit-lsp to recongnized it. for nvim builtin lsp it maybe get_language_id from help start_client (I use coc and not confirm it.)

TMTBO commented 8 months ago

OK, I will take a look at the log later. BTW, should I need to start clangd and sourcekit simultaneously?

SolaWing commented 8 months ago

clangd and sourcekit-lsp are different lsp server, and sourcekit-lsp internally use clangd. so normally you dont need clangd in xcode project

TMTBO commented 8 months ago

I have setup the sourcekit, but the response always got empty with jump-definations action.

----- startup looks good -------

[ERROR][2024-03-17 00:53:51] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "INFO:root:# xcode build server with python 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]]\n"
[ERROR][2024-03-17 00:53:51] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    'INFO:server:Xcode Build Server Startup\nDEBUG:server:Req --> {"id":1,"params":{"version":"1.0","displayName":"SourceKit-LSP","rootUri":"file:\\/\\/\\/Users\\/xxx\\/Workspace\\/Code\\/XPxxx_IOS\\/","bspVersion":"2.0","capabilities":{"languageIds":["c","cpp","objective-c","objective-cpp","swift"]}},"jsonrpc":"2.0","method":"build\\/initialize"}\nDEBUG:server:Res <-- {"jsonrpc": "2.0", "id": 1, "result": {"displayName": "xcode build server", "version": "0.1", "bspVersion": "2.0", "rootUri": "file:///Users/xxx/Workspace/Code/XPxxx_IOS/", "capabilities": {"languageIds": ["c", "cpp", "objective-c", "objective-cpp", "swift"]}, "data": {"indexDatabasePath": "/Users/xxx/Library/Caches/xcode-build-server/-Users-xxx-Workspace-Code-XPxxx_IOS/indexDatabasePath-8658e275958fad9d0a32ee327d4c466d", "indexStorePath": "/Users/xxx/Library/Developer/Xcode/DerivedData/xxx-eprseawvkqbebxchongvprxnaeoc/Index.noindex/DataStore"}}}\nDEBUG:server:Req --> {"params":{},"jsonrpc":"2.0","method":"build\\/initialized"}\n[2024-03-17 00:53:51.426] initialized build server xcode build server\n[2024-03-17 00:53:51.427] opened IndexStoreDB at /Users/xxx/Library/Caches/xcode-build-server/-Users-xxx-Workspace-Code-XPxxx_IOS/indexDatabasePath-8658e275958fad9d0a32ee327d4c466d with store path /Users/xxx/Library/Developer/Xcode/DerivedData/xxx-eprseawvkqbebxchongvprxnaeoc/Index.noindex/DataStore\n'
[ERROR][2024-03-17 00:53:51] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "[2024-03-17 00:53:51.560] SourceKitServer: Notification<initialized>(\n  clientID: ObjectIdentifier(0x00006000022cc400),\n  params: InitializedNotification()\n)\n"
[ERROR][2024-03-17 00:53:51] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "The flag `-index` is obsolete and ignored.\n"
[ERROR][2024-03-17 00:53:51] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "I[00:53:51.577] Apple clangd version 15.0.0 (clang-1500.1.0.2.5)\nI[00:53:51.577] Features: mac+xpc\nI[00:53:51.577] PID: 85659\nI[00:53:51.577] Working directory: /Users/xxx/Workspace/Code/XPxxx_IOS\nI[00:53:51.577] argv[0]: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clangd\nI[00:53:51.577] argv[1]: -compile_args_from=lsp\nI[00:53:51.577] argv[2]: -background-index=false\nI[00:53:51.577] argv[3]: -index=false\nI[00:53:51.577] Starting LSP over stdin/stdout\nI[00:53:51.578] <-- initialize(1)\n"

-------- works for a singal file --------

[ERROR][2024-03-17 00:59:36] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "I[00:59:36.041] <-- textDocument/symbolInfo(340)\n[2024-03-17 00:59:36.041] SourceKitServer: Request<textDocument/definition>(\n  id: 334,\n  clientID: ObjectIdentifier(0x00006000022cc400),\n  params: DefinitionRequest(textDocument: LanguageServerProtocol.TextDocumentIdentifier(uri: LanguageServerProtocol.DocumentURI(storage: file:///Users/xxx/Workspace/Code/XPxxx_IOS/xxx/AppDelegate/XPLaunchTaskMgr/XPLaunchTaskMgr.m)), position: Position(line: 1592, utf16index: 20))\n)\n"
[ERROR][2024-03-17 00:59:36] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "I[00:59:36.041] --> reply:textDocument/symbolInfo(340) 0 ms\n[2024-03-17 00:59:36.041] performing indexed jump-to-def with usr c:objc(cs)XPLaunchTaskMgr(im)checkMqttInfo\n"
[ERROR][2024-03-17 00:59:36] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "[2024-03-17 00:59:36.043] SourceKitServer: Response<textDocument/definition(334)>(\n  success(Optional(LanguageServerProtocol.LocationsOrLocationLinksResponse.locations([LanguageServerProtocol.Location(uri: LanguageServerProtocol.DocumentURI(storage: file:///Users/xxx/Workspace/Code/XPxxx_IOS/xxx/AppDelegate/XPLaunchTaskMgr/XPLaunchTaskMgr.m), _range: LanguageServerProtocol.CustomCodable<LanguageServerProtocol.PositionRange>(wrappedValue: Range(Position(line: 1604, utf16index: 8)..<Position(line: 1604, utf16index: 8))))])))\n)\n"

------ does not works for Cross-file access

[ERROR][2024-03-17 01:03:41] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "[2024-03-17 01:03:41.673] SourceKitServer: Request<textDocument/definition>(\n  id: 339,\n  clientID: ObjectIdentifier(0x00006000022cc400),\n  params: DefinitionRequest(textDocument: LanguageServerProtocol.TextDocumentIdentifier(uri: LanguageServerProtocol.DocumentURI(storage: file:///Users/xxx/Workspace/Code/XPxxx_IOS/xxx/AppDelegate/XPLaunchTaskMgr/XPLaunchTaskMgr.m)), position: Position(line: 1595, utf16index: 26))\n)\n"
[ERROR][2024-03-17 01:03:41] .../vim/lsp/rpc.lua:734    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "I[01:03:41.672] <-- textDocument/symbolInfo(345)\nI[01:03:41.672] --> reply:textDocument/symbolInfo(345) 0 ms\nI[01:03:41.673] <-- textDocument/definition(346)\nI[01:03:41.673] --> reply:textDocument/definition(346) 0 ms\n[2024-03-17 01:03:41.673] SourceKitServer: Response<textDocument/definition(339)>(\n  success(Optional(LanguageServerProtocol.LocationsOrLocationLinksResponse.locations([])))\n)\n"
SolaWing commented 8 months ago

@TMTBO sourcekit-lsp seems recongnized the file.. I saw you log record bsp messages.. you should already set the debug environment. but I don't see sourcekit-lsp request the XPLaunchTaskMgr.m flags from xcode-build-server. am I missing something? or can you remove compile_commands.json to avoid it's interfere?

TMTBO commented 8 months ago

Woo, it works now! There some error hanppend with my xcodebuild yestoday. And it works now. Thank you @SolaWing.