swiftlang / vscode-swift

Visual Studio Code Extension for Swift
https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang
Apache License 2.0
727 stars 50 forks source link

unable to debug the tests in a swift package (library not executable) on Windows #970

Closed dave256 closed 1 month ago

dave256 commented 1 month ago

The discussion of this issue started here: https://forums.swift.org/t/vs-code-wont-run-swift-on-windows/72721/22

If you create a package on Windows 11 with

swift package init

and then use VSCode to open the directory containing the package, you can run the package, but you can't debug the package.

I've tried this with the Swift 6 Toolchain (from the July 2nd build available on swift.org) but I also believe it doesn't work with Swift 5.10 release on swift.org.

If I right click in the gutter and choose to Debug the test, a Windows dialog pops up complaining "unable to find executable in the Swift\Platforms...\xctest.exe"

I turned on the Swift extension setting to "Output additional diagnostics to the Swift Output View" and this is what appears there when I right click in the gutter for the XCTestCase subclass and choose to debug.

09:51:56: SourceKit-LSP setup
09:51:57: Failed to find "C:\Users\roger\AppData\Local\Programs\Swift\Toolchains\6.0.0+Asserts\usr\bin\lldb-dap.exe"
09:52:18: NewPlayingCards: add: c:\Users\roger\Desktop\NewPlayingCards
09:52:18: NewPlayingCards: Resolving Dependencies: starting ... 
09:52:18: Exec Task: (NewPlayingCards) swift package resolve
09:52:18: NewPlayingCards: focus: c:\Users\roger\Desktop\NewPlayingCards
09:52:23: NewPlayingCards: Resolving Dependencies: ... done.
10:06:39: Exec Task: swift build --build-tests -Xlinker -debug:dwarf -Xswiftc -diagnostic-style=llvm
10:06:44: NewPlayingCards: XCTest Debug Config: {"type":"lldb","request":"launch","sourceLanguages":["swift"],"name":"XCTest: Test NewPlayingCards","cwd":"c:\\Users\\roger\\Desktop\\NewPlayingCards","args":["-XCTest","NewPlayingCardsTests.RankXCTests","c:\\Users\\roger\\Desktop\\NewPlayingCards\\.build\\debug\\NewPlayingCardsPackageTests.xctest"],"preLaunchTask":null,"terminal":"console","program":"C:\\Users\\roger\\AppData\\Local\\Programs\\Swift\\Platforms\\6.0.0\\Windows.platform\\Developer\\Library\\XCTest-development\\usr\\bin64\\xctest","env":{"SWIFT_TESTING_ENABLED":"0"},"testType":"XCTest"}
10:06:47: Error: "Debugger not started"

Note the file

C:\Users\roger\AppData\Local\Programs\Swift\Toolchains\6.0.0+Asserts\usr\bin\lldb-dap.exe

does actually exist despite the error saying it failed to find it.

Choosing the Run menu's Start Debugging has me then choose lldb and then it complains and creates a launch.json file.

{
    "version": "0.2.0",
    "configurations": [
     {
      "type": "lldb",
      "request": "launch",
      "name: "Debug",
      "program": "${workspaceFolder}/<executable file>",
      "args": [],
      "cwd": "${workspaceFolder}"
    }
  ]   
}

Choosing run again doesn't work, but I assume something must be put in place of <executable file> for the program setting but I'm not certain what to put there.

If I make a package with

swift package init --type executable

and then choose the menu Run -> Start Debugging, I can debug that and it stops at breakpoints showing the values of variables, etc.

Expected behavior It should debug a package that is a standard library package.

Environment

If you need any more information I'm happy to provide it as I'd like to get this working by the end of August for use in a class since some of my students have Windows computers. I have a Mac so I'm not very familiar with Windows or VSCode but have a borrowed Windows laptop I'm using to work on this.

adam-fowler commented 1 month ago

Hi @dave256 thanks for this. I think all your issues are centred on the fact the extension is failing to find lldb-dap.exe. We need to work out why this is the case.

adam-fowler commented 1 month ago

Although why it is looking for XCTest.exe is confusing. @plemarquand do you have any ideas?

plemarquand commented 1 month ago

I'm in the process of setting up a new dedicated Windows machine to try this out.

adam-fowler commented 1 month ago

@dave256 it looks like we have a fix for this. Hopefully will have it released in the next day.

dave256 commented 1 month ago

@adam-fowler yes, I saw the follow-post in the Swift forums. I will try it tomorrow or Saturday. I assume this is only for XCTest unit tests. I figured I'd teach the students the new Testing framework rather than XCTest if it was possible (rather than teach them the old way). when I try "import Testing" in a test file, VSCode can't find the module (even when I've installed the Swift 6 Toolchain). But again, if they can debug either style of tests, I (and they) will be thrilled as some of them used Swift Playgrounds on their iPad last semester and missed having a debugger (although I'm not certain they missed me forcing them to write unit tests.😁)

plemarquand commented 1 month ago

@dave256 Unfortunately swift-testing isn't in the open source Swift 6 toolchain yet, only in the Xcode 16 beta. However your students can try it out by adding it as an explicit dependency to your Package.swift:

.package(url: "https://github.com/apple/swift-testing.git", from: "0.11.0")

dave256 commented 1 month ago

This new extension seemed to solve it looking for xctest.exe but the debugger still doesn't start.

Message 39 here has the details: https://forums.swift.org/t/vs-code-wont-run-swift-on-windows/72721/39

I can copy it here if you want.

dave256 commented 1 month ago

@adam-fowler followed-up on the above Swift forum link. Still one more minor issue - the directory containing the xctest.dll file needs to be added to the PATH. Another update to this extension is planned that should alleviate the need to do that but I can confirm that adding that directory to the PATH worked. In my cases this is the directory I added to the PATH, rebooted, and it worked.

C:\Users\roger\AppData\Local\Programs\Swift\Platforms\6.0.0\Windows.platform\Developer\Library\XCTest-development\usr\bin64\

plemarquand commented 1 month ago

@dave256 Good stuff, @adam-fowler just put up and merged https://github.com/swiftlang/vscode-swift/pull/977 to add it to the PATH. I'm working on a follow on patch to do something similar for swift-testing as it looks like it needs it as well.

dave256 commented 1 month ago

@plemarquand @adam-fowler I think I got the new Testing library to work although it took forever to build the first time (not surprisingly, especially given it had to build the testing package on this low-powered Windows laptop), but even an incremental build (adding another test) was very slow. At least VSCode recognized the @Test macro and ran the test (and does stop at a breakpoint). I'll stick with XCTest for this fall semester. Thanks again for all your help.

plemarquand commented 1 month ago

@dave256 I just put up https://github.com/swiftlang/vscode-swift/pull/978 which should get swift-testing debugging working on Windows same as XCTest (without the need for you to add the path for XCTest.dll into your PATH).

Just to confirm, you got swift-testing tests running? It looked like in https://forums.swift.org/t/vs-code-wont-run-swift-on-windows/72721/41 they weren't working for you.

I agree the first time swift-testing compiles is slow on Windows. I believe it's related to its included macros. Subsequent builds after the first are faster, but definitely could stand to be quicker.

dave256 commented 1 month ago

@plemarquand Thanks. I'll download the updated Swift extension when it's released, remove the added Path, and try it.

I did get swift-testing to work today. I may have not had it set up correctly in the Package.swift file before - I always get confused about the syntax for also adding the dependency inside the target. But yes, even the subsequent builds are a couple orders of magnitude slower than XCTest builds after adding a new test (but nothing compared to the initial build which needs to build Swift Testing). I'm fine with using XCTest this fall semester. If the speed seems fine on a more reasonable development computer that you have compared to the borrowed one I have to test with, I wouldn't worry about it too much. When the semester starts, I'll see how it works on a student computer or try it on one of the Windows computers in our computer lab next time I'm on campus and have time to get everything installed. I also posted a quick follow-up in the swift forums thread indicating I got the Swift Testing library working in VSCode.

dave256 commented 1 month ago

@plemarquand I verified version 1.10.6 of the extension works without the need for the extra Path directory for the XCTest files. Thank you.