SolaWing / xcode-build-server

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

Errors in LSP logs #63

Closed wojciech-kulik closed 2 months ago

wojciech-kulik commented 2 months ago

Hi! I'm getting recently some issues from LSP.

Any idea what could be causing it? I'm not sure if it's something xcode-build-server related or not.

[ERROR][2024-08-30 12:38:55] .../vim/lsp/rpc.lua:770    "rpc"   "node"  "stderr"    "(node:73637) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 change listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit\n(Use `node --trace-warnings ...` to show where the warning was created)\n"
[START][2024-08-30 15:15:01] LSP logging initiated
[ERROR][2024-08-30 15:15:01] .../vim/lsp/rpc.lua:770    "rpc"   "node"  "stderr"    "(node:22472) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 change listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit\n(Use `node --trace-warnings ...` to show where the warning was created)\n"
[ERROR][2024-08-30 16:25:39] .../vim/lsp/rpc.lua:770    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    '[2024-08-30 16:25:39.767] failed to construct TextEdits from response [\n  {\n    key.offset: 3097,\n    key.length: 0,\n    key.sourcetext: "<#Range<Int>#>"\n  }\n]\n'
[ERROR][2024-08-30 16:25:39] .../vim/lsp/rpc.lua:770    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    '[2024-08-30 16:25:39.960] failed to construct TextEdits from response [\n  {\n    key.offset: 3097,\n    key.length: 0,\n    key.sourcetext: "<#Range<Int>#>"\n  }\n]\n'
[ERROR][2024-08-30 16:30:59] .../vim/lsp/rpc.lua:770    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "/AppleInternal/Library/BuildRoots/226e9c8c-edb1-11ee-8f17-a65dcee5a99e/Library/Caches/com.apple.xbs/Sources/SourceKitLSP/dependencies/swift-syntax/Sources/SwiftParser/Lexer/Cursor.swift:620: Fatal error\n"
[START][2024-08-30 17:33:34] LSP logging initiated
[ERROR][2024-08-30 17:33:34] .../vim/lsp/rpc.lua:770    "rpc"   "node"  "stderr"    "(node:4438) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 change listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit\n(Use `node --trace-warnings ...` to show where the warning was created)\n"
[ERROR][2024-08-30 17:34:39] .../vim/lsp/rpc.lua:770    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "/AppleInternal/Library/BuildRoots/226e9c8c-edb1-11ee-8f17-a65dcee5a99e/Library/Caches/com.apple.xbs/Sources/SourceKitLSP/dependencies/swift-syntax/Sources/SwiftParser/Lexer/Cursor.swift:620: Fatal error\n"
[ERROR][2024-08-30 17:43:11] ...lsp/handlers.lua:623    '[fetchCompletions] Unhandled status from server: 429 {"error":{"code":"429","message":"Requests to the Completions_Create Operation under Azure OpenAI API version 2023-09-15-preview have exceeded call rate limit of your current OpenAI S0 pricing tier. Please retry after 1 second. Please go here: https://aka.ms/oai/quotaincrease if you would like to further increase the default rate limit."},"policy_id":"DeploymentRatelimit-User"}\n'
[ERROR][2024-08-30 17:43:11] ...lsp/handlers.lua:623    '[fetchCompletions] Unhandled status from server: 429 {"error":{"code":"429","message":"Requests to the Completions_Create Operation under Azure OpenAI API version 2023-09-15-preview have exceeded call rate limit of your current OpenAI S0 pricing tier. Please retry after 1 second. Please go here: https://aka.ms/oai/quotaincrease if you would like to further increase the default rate limit."},"policy_id":"DeploymentRatelimit-User"}\n'
[ERROR][2024-08-30 18:13:47] .../vim/lsp/rpc.lua:770    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "[2024-08-30 18:13:47.042] triggerFromIncompleteCompletions with incompatible completion session; expected DocumentURI(storage: file:///Users/wkulik/repositories/MyApp-macos/MyApp/Resources/AppConstants.swift)@8097, but got DocumentURI(storage: file:///Users/wkulik/repositories/MyApp-macos/MyApp/Resources/AppConstants.swift)@8104\n"
[ERROR][2024-08-30 18:23:45] .../vim/lsp/rpc.lua:770    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "[2024-08-30 18:23:45.614] triggerFromIncompleteCompletions with incompatible completion session; expected DocumentURI(storage: file:///Users/wkulik/repositories/MyApp-macos/MyApp/Modules/Main/Root/UtilsView.swift)@810, but got DocumentURI(storage: file:///Users/wkulik/repositories/MyApp-macos/MyApp/Modules/Main/Root/UtilsView.swift)@814\n"
[ERROR][2024-08-30 18:24:06] .../vim/lsp/rpc.lua:770    "rpc"   "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"  "stderr"    "/AppleInternal/Library/BuildRoots/226e9c8c-edb1-11ee-8f17-a65dcee5a99e/Library/Caches/com.apple.xbs/Sources/SourceKitLSP/dependencies/swift-syntax/Sources/SwiftParser/Lexer/Cursor.swift:620: Fatal error\n"
SolaWing commented 2 months ago

seems no logs from xcode-build-server? also notice you have exceeded call rate limit of your current OpenAI, did this will break your completion?

wojciech-kulik commented 2 months ago

also notice you have exceeded call rate limit of your current OpenAI, did this will break your completion?

This is weird because I don't remember using any OpenAI plugin :D. I'm using just Copilot. Update: Oh, I forgot that GitHub Copilot uses OpenAI.

How about:

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 change listeners added to [EventEmitter]

any idea what could it be?

SolaWing commented 2 months ago

Memory leaks usually do not affect functionality

wojciech-kulik commented 2 months ago

I keep having issues with LSP that stops working on my project and I've been thinking that maybe it's related to those logs.

I will try to test it a little bit more and let you know.

How about other errors (besides OpenAI & leaks). Any ideas?

SolaWing commented 2 months ago

Did you tried clean build directory, xcode-build-server cache directory and rebuild? I recently find sometimes lsp work very slow and often unresponsive. after a full clean and rebuild, it’s ok

wojciech-kulik commented 2 months ago

Yes, I usually do clean build from Xcode if something breaks, but it doesn't help for long. How can I clean xcode-builld-server cache - where it is?

wojciech-kulik commented 2 months ago

Right now for example, the LSP works partially, some navigation works, but for some things LSP can't find definition, even for a simple class. I did clean build and run xcode-build-server config again. I tried also to call xcode-build-server parse manually.

It looks like right now LSP is not able to find definitions from other xcframeworks. It is able only to navigate within the same module.

SolaWing commented 2 months ago

How can I clean xcode-builld-server cache - where it is?

It’s at ~/Library/Caches/xcode-build-server. it contains compile file from logs, and index database from lsp.

wojciech-kulik commented 2 months ago

Still no luck, I removed cached, run clean build, restarted Neovim and it's still unable to navigate to another modules:

[telescope.builtin.lsp_definitions]: No LSP Definitions found

but I can navigate with no issue from Xode.

SolaWing commented 2 months ago

did you have correctly flags pass to LSP? had any error with environment SOURCEKIT_LOGGING=3?

wojciech-kulik commented 2 months ago

I'm not sure what I should be looking for there. With this logging level, logs are messy. They print source code, everything is an error and it's not in a readable form, so it's hard to judge what's going on there. Also, what are "correct" flags :D? What should be expected there.

SolaWing commented 2 months ago

correct flags should be same as the build command for the file.

if flags is correct, and cache is clean, may check using same version for xcode and sourcekit-lsp. also you can check another editor like vscode vs swift plug-in to ensure not lsp’s fault

wojciech-kulik commented 2 months ago

Flags seem to be similar, it's hard to compare them manually. Although I also tried to perform another clean build from Xcode, export logs and parse them manually using xcode-build-server to ensure that everything is the same, but still no luck. Navigation works only in the same module (xcframework) if I try navigate outside it can't find symbols. The same issue occurs in Visual Studio (but ofc it's the same tool: sourcekit-lsp + xcode-build-server), but in Xcode everything works perfect.

I've also got another project, but it's different, it's built using modularization with SPM packages and there cross-module navigation works without any issues.

I also checked another project with xcframeworks and it works fine. So it looks like, LSP doesn't like my one specific project for some reason...

I don't know what else I can try. It worked before but it stopped working now.

SolaWing commented 2 months ago

Therefore it only reproduces in a specific private repository of yours... and is fine in other repositories..
Cross-module symbols are generally related to the index-store in compilation. Maybe your specific data triggers some kind of bug in sourcekit. Use SOURCEKIT_LOGGING=3 to look at the detailed logs to see if there are any clues If you have the smallest reproducible demo, I can also try to debug it to see if I have the same problem here.

wojciech-kulik commented 2 months ago

Nothing interesting with this env either. Just:

'sourcekit: [2:sourcekitd_send_request-after:5635:10.9803] {\n  key.internal_diagnostic: "Resolved to incomplete expression or statement."\n}\n'
'[2024-09-03 21:04:43.869] {\n  key.internal_diagnostic: "Resolved to incomplete expression or statement."\n}\n'
"[2024-09-03 21:04:43.870] SourceKitServer: Response<textDocument/definition(2)>(\n  success(Optional(LanguageServerProtocol.LocationsOrLocationLinksResponse.locations([])))\n)\n"
wojciech-kulik commented 2 months ago

Is it possible to access the index like a normal DB? I think I saw something like that in the past. Maybe I could check if specific symbols are correctly mapped?

wojciech-kulik commented 2 months ago

Symbols are correctly indexed. I managed to check example symbol if the index shows that it is referenced in the file from which I can't navigate to it.

I did it using: https://github.com/mikolasstuchlik/GLibISDemo/tree/master

SolaWing commented 2 months ago

symbols may coming from https://github.com/swiftlang/indexstore-db, although I haven't read it's code.

The final solution may be to debug Sourcekit-lsp and add more logs to see what is wrong.

Also note that the bottom layer of LSP depends on Sourcekit. Although jump and completion have the same symbols, the dependent codes are different. You can confirm whether your completion is ok. If it is ok, it is just a problem of LSP using indexdb. If it's not ok, it may be a problem with the underlying SourceKit. SourceKit itself also provides a YAML request interface, which can be requested directly using tools such as https://github.com/jpsim/SourceKitten. Theoretically, there should be no exceptions if the parameters are correct, but sourcekitten may not support some parameters, causing exceptions.

wojciech-kulik commented 2 months ago

It looks like LSP doesn't provide completions for those symbols from other modules either.

I haven't figured out how to use SourceKitten with my project, but I think there is a simpler way to test lsp. I can call it using Neovim API:

vim.lsp.buf_request_all(BUF_NR, "workspace/symbol", { query = "AppError" }, function(result)
    P(result)
end)

In my case it doesn't find that symbol in other modules. So it looks like a problem with sourcekit-lsp. Or I guess potentially it could be also caused by the lack of information from xcode-build-server? I don't fully understand how it works under the hood, but as far as I understand xcode-build-server allows sourcekit-lsp to understand the project structure and be able to provide the cross-module search, etc.

Is there any way to determine on which side there is an issue?

I also sniffed what is sent via vim.lsp.buf_requets_all when I hit gd and it's:

vim.lsp.buf_request_all(BUF_NR, "textDocument/definition", {
    position = {
        character = 45,
        line = 73,
    },
    textDocument = {
        uri = "file:///PATH_TO_MY_FILE",
    },
}, function(result)
    P(result)
end)

and the result is empty :(

SolaWing commented 2 months ago

All xcode-build-server can do is provide flags to sourcekit-lsp. If there is no problem with the flags provided by sourceKitOptionsChanged and they are consistent with the swiftc compilation command, then it is basically not a problem with xcode-build-server.

LSP depends on sourcekit, which is a service that can be requested independently. Use SOURCEKIT_LOGGING=3 to see detailed interface requests for interacting with it. If its request parameters are provided correctly but there is no correct result, it may be a bug in Swift itself. If the sourcekit is normal and there are errors elsewhere in the log, it may be an LSP problem. In addition, cross-module symbol search is done in indexdb, which has nothing to do with sourcekit. But completion is provided by sourcekit. So if the completion is normal but only the jump is abnormal, then there is a high probability that there is a problem with indexdb. If it is not normal, then there may be a problem with sourcekit or indexstore.

In the end, it is best to have a reproducible demo project to debug and locate problems.

In addition, from your log, you only provided part of the log. It seems that Cursor.swift:620: Fatal error is abnormal?

wojciech-kulik commented 2 months ago

I guess, my project somehow breaks sourcekit. A minimum reproducible demo is not very likely, as it works with other projects. I will have to live with that for now. Maybe the new Swift & sourcekit-lsp will solve this problem.

Thank you for all your help and great tips!

wojciech-kulik commented 2 months ago

I updated macOS to Sequoia and Xcode to 16 and it suddenly all works 🎊. Probably it was a bug and they fixed that. Also, finally, the smart rename is available in the latest version of sourcekit-lsp ^^.