swiftlang / sourcekit-lsp

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

PackagePlugin Framework Symbol Completion #664

Closed adam-fowler closed 8 months ago

adam-fowler commented 1 year ago

Many sourcekit-lsp features are missing for the PackagePlugin framework.

If I set up a SwiftPM build plugin, while editing the plugin source code I don't get symbol completion or textDocument/hover output for the symbols from the PackagePlugin framework.

This is with Swift 5.7 on macOS 13

ktoso commented 1 year ago

This is in VSCode right?

adam-fowler commented 1 year ago

This is in VSCode right?

Yes

ahoppen commented 1 year ago

rdar://102213837

ahoppen commented 1 year ago

The problem here is that we don’t get compiler arguments for the files in the Plugin folder. We need a new API in SwiftPM to get those arguments. Once we have that API, fixing this should be trivial.

I filed https://github.com/apple/swift-package-manager/issues/6699 to add the new API to SwiftPM.

ahoppen commented 1 year ago

Here’s a stub of a test case in SwiftPMWorkspaceTests that I started writing. The expected compiler arguments are almost certainly not correct but it should be a good starting point

func testPluginArgs() throws {
  // FIXME: should be possible to use InMemoryFileSystem.
  let fs = localFileSystem
  try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
    try fs.createFiles(root: tempDir, files: [
      "pkg/Plugins/MyPlugin/a.swift": "",
      "pkg/Sources/lib/lib.swift": "",
      "pkg/Package.swift": """
          // swift-tools-version:5.7
          import PackageDescription
          let package = Package(
            name: "a",
            products: [],
            dependencies: [],
            targets: [
              .target(name: "lib"),
              .plugin(name: "MyPlugin", capability: .buildTool)
            ]
          )
          """
    ])
    let packageRoot = try resolveSymlinks(tempDir.appending(component: "pkg"))
    let tr = ToolchainRegistry.shared
    let ws = try SwiftPMWorkspace(
      workspacePath: packageRoot,
      toolchainRegistry: tr,
      fileSystem: fs,
      buildSetup: TestSourceKitServer.serverOptions.buildSetup)

    let aswift = packageRoot.appending(components: "Plugins", "MyPlugin", "a.swift")
    let hostTriple = ws.buildParameters.triple
    let build = buildPath(root: packageRoot, platform: hostTriple.platformBuildPathComponent())

    XCTAssertEqual(ws.buildPath, build)
    XCTAssertNotNil(ws.indexStorePath)
    let arguments = try ws._settings(for: aswift.asURI, .swift)!.compilerArguments

    check(
      "-module-name", "lib", "-incremental", "-emit-dependencies",
      "-emit-module", "-emit-module-path", arguments: arguments)
    check("-parse-as-library", "-c", arguments: arguments)

    check("-target", arguments: arguments) // Only one!
#if os(macOS)
    let versionString = PackageModel.Platform.macOS.oldestSupportedVersion.versionString
    check("-target", hostTriple.tripleString(forPlatformVersion: versionString), arguments: arguments)
    check("-sdk", arguments: arguments)
    check("-F", arguments: arguments, allowMultiple: true)
#else
    check("-target", hostTriple.tripleString, arguments: arguments)
#endif

    check("-I", build.pathString, arguments: arguments)

    check(aswift.pathString, arguments: arguments)
  }
}
alexzielenski commented 3 weeks ago

Is there something that I need to do to enable this? I have this issue on swift 5.10, 5.10.1, and 6.0 toolchains, but this issue was closed as completed in January

ahoppen commented 3 weeks ago

The issue should be fixed in Swift 6.0 toolchains. If you are still seeing it, could you open a new issue for it with reproduction steps?