VS Code won't run Swift on Windows

It happens when I right click in the VSCode gutter and choose to debug the tests. It offers to open the launch.json file. I don't fully understand what should go in those so it's just whatever it put in there by default.

The launch.json file has:

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

(typed in by hand on my Mac by looking at file on Windows computer)

If I choose to to debug from the VSCode Run menu, then it says no executable in the root path of my project directory.

Thanks,
Dave

@dave256 can you go to the VSCode output pane and in the dropdown there should be a Swift entry. Select it. And then cut and paste the contents of the output here.

@adam-fowler
Here's the output when I right-click on the test in the gutter and choose debug:

14:54:35: Exec Task: swift build --build-tests -Xlinker -debug:dwarf -Xswiftc -diagnostic-style=llvm
14:54:52: Error: "Debugger not started"

Using the Run menu and selecting "Start Debugging" doesn't seem to put anything there - just the dialog about showing launch.json.

As far as Swift 6 vs. Swift 5 I don't have a strong preference for what we'll be using it for this fall, but since I couldn't get Swift 5 to work and Swift 6 has something built-in for using in VSCode, I tried it. I have gotten farther with Swift 6 than 5, but maybe if I go back to Swift 5 with what I know now, I might get this far. If things go well this fall, I may use it a bit in our operating systems/parallel computing course in January so having the full Swift concurrency features might be useful then but of course by then Swift 6 will be out. The backstory is we did a little bit in our computer graphics course this past January-May semester on their iPads using the Swift Playgrounds app and the students wanted to do more with Swift. We mainly use Python and C++ in our curriculum with a little Java and I teach an elective iOS development app. Not surprisingly the students liked Swift over C++ and I do want to teach them about modern concurrency techniques (async/await, etc.) in the parallel computing course. This fall is a software design course so I was going to have them write some model code in Swift packages with me writing the SwiftUI code that will use it. So I'd like them to be able to write a package and tests and then we can move the model code to their iPads to run in Swift Playgrounds with the SwiftUI code I'll write that uses the model. I don't see any way to run tests in the iPad Swift playgrounds app (other than manually writing your own main function to just execute code and print out messages if there are errors) plus there's no debugger in Swift Playgrounds.

@compnerd and yes the XCTest.dll files do exist in the directory structure but the error message definitely says .exe. See full reply above.

If you run the command Swift: Capture VS Code Swift Diagnostic Bundle and create an issue in the vscode-swift repository with the files the command created we can have a closer look there.

@adam-fowler I'm happy to create an issue there but I'm not certain I know what you're asking me to do. In the Swift extension settings, I checked "Output additional diagnostics to the Swift Output View".

When I right click in the gutter of a test and choose debug, this is what shows up in the output view:

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"

If I use the Run -> Start Debugging menu, this is what shows up:

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.

That is the path to the lldb-dap.exe file - it's there (546KB). If this is what you want, I'll open an issue here: GitHub - swiftlang/vscode-swift: Visual Studio Code Extension for Swift
with this information.

If it's not can you either tell me explicitly what you want me to do or point me to online documentation explaining what you want me to do.

Thanks,
Dave

In Visual Studio Code, select View > Command Palette (or press the corresponding keyboard shortcut to achieve the same result).

In the box (palette) that pops up, start typing in Swift: Capture VS Code Swift Diagnostic Bundle, select that option when it appears (assuming the plugin is installed of course), and the program will save a bunch of files and notify you about it.

Hmm, something is wrong .. if you look at:

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"}

The args contains -XCTest ... which implies that it is misconfigured. I suspect that you may have checked in a launch configuration file which you should remove and never commit and require that the user re-create it each time. This is configured for a macOS debug environment rather than Windows and they are incompatible due to the ObjC XCTest vs Swift XCTest implementations being different.

I don't think I did anything to specify -XCTest. I just did all of the following on the Windows 11 computer (no Mac involved).

Opened its shell/terminal app and in a directory ran:

cd Desktop
mkdir Version3
swift package init

This created a directory with the files I expected:
Package.swift
Sources/Version3/Version3.swift
Tests/Version3Tests/Version3Tests.swift
.gitignore

I then opened that Version3 directory with VSCode.

There is no launch.json file but the Version3Tests.swift contains an XCTestCase subclass.

I put a simple func sqr(_ x: Double) -> Double { x * x }
in the Version3.swift file
and in the test file, put this inside a test function:
XCTestAssertEqual(sqr(3.0), 9.0)

As I've said before, right-clicking in the test gutter and choosing to run, works (no launch.json file is created).

Right-clicking and choosing to debug complains about the missing xctest.exe file I mentioned earlier. It gives me the option to open a launch.json file which it generates and contains (same as from my earlier message):

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

Is there something else I should put in this launch.json file or is something else wrong?

Thanks,
Dave

-XCTest is a valid argument. It is how tests are filtered when running XCTest executables directly.

For some reason it is not finding the lldb-dap.exe. Are you certain it is available on your file system? And yes can we move this to an issue in the vscode-swift repo with the log details.

@adam-fowler yes, I'm certain the lldb-ldap.exe exists in the path it shows.

I've created an issue here:

Thanks,
Dave

1 Like

Correct, but the -XCTest argument is for filtering tests with the XCTest runner which is part of the macOS implementation and not the generated test executor with swift-corelibs-xctest.

Yep you're right I can't remember all the different setups anymore

I just put up a PR to address the debugger not launching correctly on Windows: Use correct xctest path when debugging on Windows by plemarquand · Pull Request #972 · swiftlang/vscode-swift · GitHub

Another thing to check, once that PR goes in, is to make sure LLDB is configured correctly by running lldb from the command line.

I had to fixup Python in my PATH and the CodeLLDB extension doesn't seem to report an error if it can't start lldb, so it was hanging at initialization.

@plemarquand thanks for getting to this so quickly. I'd be happy to try this now but I'm not certain what the steps are to install the extension from GitHub before a new release with this commit is made. If that's simple for someone to explain, I'll do try it or if there will be a new release in a day or two, I can obviously wait.

Also, this is what lldb showed for me when I just typed lldb and pressed return in the PowerShell:

==== LLDB swift-healthcheck log. ===
This file contains the configuration of LLDB's embedded Swift compiler to help diagnosing module import and search path issues. The swift-healthcheck command is meant to be run *after* an error has occurred.
lldb version 17.0.6
Swift version 6.0-dev (LLVM 3602748d4ec9947, Swift 09b8b0861a69528)
(lldb)

Thanks,
Dave

@dave256 We're aiming for a release tomorrow, but if you'd like to try it out beforehand you can check out the latest vscode-swift main branch and follow the steps on Installing a pre released version

@plemarquand ah, I should have found that. This borrowed Windows laptop is underpowered (limited disk space and 4GB of RAM) so I'll wait until tomorrow. I've set a reminder to try it and will report back if it does indeed now work for me. Thanks again.

1 Like

@plemarquand @adam-fowler

I installed the new VSCode Swift extension and restarted it. I think we're making progress but it's still not working. When I right click in the gutter for an XCTestCase class and chose to debug it, a Windows dialog opens that says unknown error and offers to open launch.json (which didn't previously exist but it will create one if I say to open it).

This shows up in the Debug Console:

Console is in 'commands' mode, prefix expressions with '?'.
Launching: c:\Users\roger\Desktop\Version3\.build\debug\Version3PackageTests.xctest Version3Tests.Version3Tests

If I go to the Output Console and choose Swift in the right filter, this is what appears:

08:31:10: Exec Task: swift build --build-tests -Xlinker -debug:dwarf -Xswiftc -diagnostic-style=llvm
08:31:13: Version3: XCTest Debug Config: {"type":"lldb","request":"launch","sourceLanguages":["swift"],"name":"XCTest: Test Version3","cwd":"c:\\Users\\roger\\Desktop\\Version3","args":["Version3Tests.Version3Tests"],"preLaunchTask":null,"terminal":"console","program":"c:\\Users\\roger\\Desktop\\Version3\\.build\\debug\\Version3PackageTests.xctest","env":{},"testType":"XCTest"}
08:31:17: Error: "Debugger not started"

If I change the filter for the Output console to LLDB, this is what appears:

Initial debug configuration: {
  type: 'lldb',
  request: 'launch',
  sourceLanguages: [ 'swift' ],
  name: 'XCTest: Test Version3',
  cwd: 'c:\\Users\\roger\\Desktop\\Version3',
  args: [ 'Version3Tests.Version3Tests' ],
  preLaunchTask: null,
  terminal: 'console',
  program: 'c:\\Users\\roger\\Desktop\\Version3\\.build\\debug\\Version3PackageTests.xctest',
  env: {},
  testType: 'XCTest'
}
Resolved debug configuration: {
  type: 'lldb',
  request: 'launch',
  sourceLanguages: [ 'swift' ],
  name: 'XCTest: Test Version3',
  cwd: 'c:\\Users\\roger\\Desktop\\Version3',
  args: [ 'Version3Tests.Version3Tests' ],
  preLaunchTask: null,
  terminal: 'console',
  program: 'c:\\Users\\roger\\Desktop\\Version3\\.build\\debug\\Version3PackageTests.xctest',
  env: {},
  testType: 'XCTest',
  expressions: 'native',
  relativePathBase: 'c:\\Users\\roger\\Desktop\\Version3',
  _adapterSettings: {
    displayFormat: 'auto',
    showDisassembly: 'auto',
    dereferencePointers: true,
    suppressMissingSourceFiles: true,
    evaluationTimeout: 5,
    consoleMode: 'commands',
    sourceLanguages: null,
    terminalPromptClear: null,
    evaluateForHovers: true,
    commandCompletions: true,
    reproducer: false
  }
}
[ERROR codelldb::debug_session] unknown error
[ERROR codelldb::debug_session] Internal debugger error: error: process 0 in state = exited, but cannot detach it in this state.
==== LLDB swift-healthcheck log. ===
This file contains the configuration of LLDB's embedded Swift compiler to help diagnosing module import and search path issues. The swift-healthcheck command is meant to be run *after* an error has occurred.
lldb version 17.0.6
Swift version 6.0-dev (LLVM 3602748d4ec9947, Swift 09b8b0861a69528)
Debug adapter exit code=0 (0x0), signal=null.

The Version3Tests.swift file is just:

import XCTest
@testable import Version3

final class Version3Tests: XCTestCase {
    func testSqr() {
        let x = 3.0
        let y = sqr(x)
        XCTAssertEqual(y, 9.0)
    }
}

And the Verstion3.swift file contains:

func sqr(_ x: Double) -> Double {
    x * x
}

All the other files in the directory are just the result of swift package init (no other files were modified).

Any other thoughts on what is wrong? Again, I'm happy to run any other diagnostics if you explain what you want me to do.

Thanks,
Dave

Hi @dave256

I saw the issue as soon as I did the release. I’m sorry about this. The issue is the xctest.dll isn’t in the path. If you include the path to the xctest.dll in your own path it should work

We will do another release that fixes this soon. Unfortunately not today though.