swiftlang / sourcekit-lsp

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

Building right after opening can corrupt the Swift dependencies checkouts #740

Closed tristanlabelle closed 1 year ago

tristanlabelle commented 1 year ago

Open a Swift project with several git dependencies (such as https://github.com/pointfreeco/swift-composable-architecture.git) in VSCode and immediately start a build.

With high likelihood, you will hit errors like:

    fatal: not a git repository (or any of the parent directories): .git

error: input file 'C:\Code\swift-composable-architecture\.build\checkouts\swift-argument-parser\Plugins\GenerateManual\GenerateManualPluginError.swift' was modified during the build

error: 'swift-benchmark': Failed to clone repository https://github.com/google/swift-benchmark:
    Cloning into 'C:\Code\swift-composable-architecture\.build\checkouts\swift-benchmark'...
    error: could not write config file C:/Code/swift-composable-architecture/.build/checkouts/swift-benchmark/.git/config: No such file or directory
    fatal: could not set 'core.logallrefupdates' to 'true'

The checkout might now be in a bad state where there will be compilation errors in dependencies because of missing files or similar issues. The workaround is to delete .build and rebuild.

The issue is that the Resolve Package Dependencies task started by the Swift extension and the Build All task started by the user run concurrently and both try to checkout git repositories at once, resulting in corrupt checkouts. SourceKit-LSP should enforce mutual exclusion of those two tasks because it is fairly easy for a user to get into this state and hard to diagnose.

Using:

sswg.swift-lang Version: 1.2.1
compnerd.org Swift version 5.9-dev (LLVM 1626c3d9b5175d1, Swift 0c0ec72255527fa)
Target: x86_64-unknown-windows-msvc
Windows 11
ahoppen commented 1 year ago

Tracked in Apple’s issue tracker as rdar://108670689

adam-fowler commented 1 year ago

This is not really a SourceKit-LSP issue. It is a combination of a SwiftPM and VSCode extension issue.

As an aside, you should be able to fix up your project by running a swift package update instead of completely deleting your .build folder.

tristanlabelle commented 1 year ago

@adam-fowler Thanks for the details. It makes sense that this is not the best place to track the issue, and I understand the solution not obvious. Would it still help for me to file issues on the two repos you mention (and then close this)?

adam-fowler commented 1 year ago

Yes go ahead and add the issues. It would be good if you could verify if this is specific to Windows as well.

adam-fowler commented 1 year ago

@tristanlabelle do you want to close this here as vscode-swift is the best place for this issue.

tristanlabelle commented 1 year ago

Not really vscode-swift more than here. I expect a fix would be in swift-corelibs-dispatch. Whatever makes your tracking easier.

adam-fowler commented 1 year ago

Not sure how building on top of a resolve is the fault of swift-corelibs-dispatch. You could point the finger at SwiftPM for not dealing with this situation. But I do have a solution on the way which will make build tasks unavailable while a resolve or update is running.

tristanlabelle commented 1 year ago

Oh sorry! I thought we were in a different issue. Ignore my reply and yes let's close this. I have a note to file the SwiftPM bug as well. Thanks!