VS Code won't run Swift on Windows

I teach computer science and have been trying to get Swift to work on one of my student's Windows computer. It's running Windows 11. We've installed the Swift toolchain, VS Code, and the Swift Extension (which also installs the CodeLLDB extension). At first it complains about not finding liblldb.dll but we realized it was looking in Swift5.10.0-Asserts (or something like that as I don't have access to my student's computer right now) when Swift had installed itself in Swift5.10.1-Asserts (the .1 vs. .0). We got around that by renaming the directory or pointing VS Code at the bin directory. It does build as I see the .build directory show up in VS Code with lots of files in it but when we run (without or without debugging) it gives us a Rust exception and tells us to add the environment variable RUST_BACKTRACE=1. We tried doing that but couldn't get any farther.

I have a Mac and have been able to get a Swift Package to run in VS Code on my Mac (in addition to of course running fine in Xcode). Is there some step we're missing to get it to work on Windows?

Thanks,
Dave

4 Likes

Hi Dave,

Can you check these instructions:

I've been using them for a few years to teach Swift on Windows.

4 Likes

Thanks. We had found that via googling and tried those previously (back in May when we were both still on campus). I can't recall if we had to change the Python version to get it to work. My recollection is it wouldn't run from VS Code - something about the launch.json file. So perhaps we did something wrong in the process. At this point I'm wondering if we got conflicting configurations since we've tried multiple ways to get it to work.

The Rust error makes me think that this is related to the "old" DAP. We have included the LLVM DAP in the toolchain for Windows from time immemorial. @adam-fowler added the ability to use the LLVM DAP for the toolchain a while ago I believe, and that might help resolve the issue with the rust error.

The python issue is likely due to the fact that you need Python 3.9 installed and setup properly but that is not part of the toolchain.

I would recommend that you first try to get things working on the command line before trying to get VSCode working as that ensures that all the necessary pieces are in working order.

Others will likely be better suited to helping you with VSCode as most of my usage tends to be from the command line.

1 Like

We did get it working from the command line at one point but we'll need to try again as we deleted Swift yesterday to free up space on his computer.

I didn't see anything on Swift.org saying you needed Python (so if it is required, it would be nice if the Swift.org instructions for Windows included that).

So the steps are the following?

  1. enable Developer Mode
  2. In terminal do:
winget install Git.Git
winget install Python.Python.3.9
(restart Terminal)
python -m ensurepip
python -m pip install six

Can we now just install Visual Studio Code from here:

or do we need to continue with the Terminal commands to install it?

And then can we install a Swift Tool chain from Swift.org

(at this point we can verify that a command line invocation runs a basic Swift hello world program).

Thanks,
Dave

Next install Swift extensions for VS Code

Now disable CodeLLDB? (since the Swift extension includes its own support)

Now in VSCode for the Swift Toolchain, select the bin directory within the Swift Toolchain directory which seems to be in something like /Users/.../AppData/Programs/Local/Swift...

My student and I are going to try again via Zoom on Thursday afternoon so I'll try to document the steps (so if it works we can share the exact steps) or if we run into issues, try to have the exact error messages.

Thanks,
Dave

Python is required for lldb and the REPL. This might be required when using VSCode, but is not mandatory under the commandline unless you use the debugger or REPL.

Yes, you can install VSCode however you like.

Yes, I would suggest this, but to do this you would need to enable the support for the LLVM DAP but that IIRC involves some tweaking of launch.json

This shouldn't be needed if you start Code after installing the Swift toolchain, it should all get picked up by default.

Good luck! The exact errors are likely helpful though.

1 Like

You need to set the VSCode Setting Swift > Debugger: Use Debug Adapter from Toolchain to true. Then if you restart the extension the auto-generated launch.json entries will be edited to use the Swift toolchain debug adapter (lldb-dap).

At this point in time because the current default version of Swift: 5.10 does not include the debug adapter on most platforms we are still requiring that CodeLLDB is installed with the swift extension. Once Swift 6 is out we can look at removing this requirement.

Thank you @adam-fowler. I will try installing Swift 6 and using that setting since something seems wrong with using the CodeLLDB extension on his computer. I will report back if we get it to work or have issues. Do you know if Python is required to use Swift with VSCode on Windows? I just want the students to be able to run and debug text-based (print statements) code on Windows - usage from the Terminal is not necessary so if that's what Python is needed for, I can skip that if it works in VSCode without Python. Ideally I'd like them to be able to write packages with unit tests - I thought I saw something about better support for unit tests in VSCode so hopefully that will work this fall.

VS Code uses the same tools as the command line. If you want to run the debugger you will need the correct version of Python.

When you are talking about unit testing I assume you are talking about swift-testing. This is a new test framework which will come with Swift 6.

Thanks for clarifying that. Are there an exact set of instructions for getting Swift working on Windows with VSCode on swift.org somewhere? If not, it would be great if there were. If I get this working with one student, I'll try to get another student who hasn't done any part of the process and see if I can some up with a minimal set of steps which I'm hoping is:

  1. Follow the steps from @svanimpe linked above for installing Python
  2. Install VSCode
  3. Install Swift Toolchain from the .exe on swift.org (we'll use version 6 this time)
  4. Install the Swift VS Code extension
  5. for now use the VSCode setting @adam-fowler described above so it uses the LLDB built-in to the Swift 6 Toolchain (although for now CodeLLDB can't be disabled as it seems to also want to disable the Swift extension) but after Swift 6 is out this may not be necessary

If I'm missing something, feel free to let me know. I'm willing to help get this documented on swift.org (assuming I get it working) later if it's just nobody has gotten around to it yet.

Thanks,
Dave

2 Likes

Step 1 & 3 should be documented on swift.org already. It appears if you are installing via winget the details of dependencies is a little vague.

Install the latest Swift developer package, as well as compatible Git and Python tools if they don’t exist.

@compnerd is this something that could be made clearer? Or are the compatible versions of Git and Python installed by the command line referenced on the webpage

winget install --id Swift.Toolchain -e

Its been a wee while since I last installed a Swift toolchain on Windows and don't know if things have changed.

1 Like

Any recent version of Git should be fine, I don't think that there are any compatibility issues there.

The current instructions do not include the python version. The problem here is that this is not fixed. Older Swift releases (5.2-5.5?) required Python 3.7, the recent releases use Python 3.9, and I could see future versions requiring a different version. If we use an explicit version, it becomes misleading because it is version dependent. If there is a way to extract the information in an automated fashion and display that in a matching form, that would be nice.

1 Like

I believe I can answer that as I have Windows 11 ARM running using Parallels on my Mac and issuing that command does install Git, Python3.9, Microsoft.VCRedist.2015+.x64 [>= 14.28.29913.0], and Swift 5.10.1 (but not surprisingly that must install x86 files so it doesn't work on ARM).

winget install --id Swift.Toolchain -e
Found Swift [Swift.Toolchain] Version 5.10.1
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
This package requires the following dependencies:
  - Packages
      Git.Git
      Microsoft.VCRedist.2015+.x64 [>= 14.28.29913.0]
      Python.Python.3.9
(1/2) Found Git [Git.Git] Version 2.45.2
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Downloading https://github.com/git-for-windows/git/releases/download/v2.45.2.windows.1/Git-2.45.2-64-bit.exe
  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  64.9 MB / 64.9 MB
Successfully verified installer hash
Starting package install...
The installer will request to run as administrator, expect a prompt.
Successfully installed

(2/2) Found Microsoft Visual C++ 2015-2022 Redistributable (x64) [Microsoft.VCRedist.2015+.x64] Version 14.40.33810.0
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Downloading https://download.visualstudio.microsoft.com/download/pr/1754ea58-11a6-44ab-a262-696e194ce543/3642E3F95D50CC193E4B5A0B0FFBF7FE2C08801517758B4C8AEB7105A091208A/VC_redist.x64.exe
  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  24.2 MB / 24.2 MB
Successfully verified installer hash
Starting package install...
Successfully installed

Successfully verified installer hash
Starting package install...
Successfully installed

So yes, the instructions here:

seem good (although I'll probably install VSCode from its installer so they have the latest version more easily than trying to edit that line). We had tried those back in May but just couldn't get it to execute Swift. I just found a friend who has a Windows laptop he's not really using so I'm going to borrow it and see what progress I can make. Perhaps after installing Swift 5.10.1, I'll uninstall it and then manually install the Swift 6 toolchain .exe (adding --version 6.0.0 to the winget install doesn't find it which isn't surprising since it's not an official realease yet).

I got it mostly working.

The instructions at "Installation via Windows Package Manager" on Swift.org worked for me - not certain why they didn't work on my student's computer.

I did end up deleting the 5.10.1 toolchain and installing the 6.0 toolchain.

I tried setting the VSCode Setting for Swift > Debugger: use Debug Adapter from Toolchain. I then left Swift > Debugger: Path field empty and it seems to work but then I don't know if it's using CodeLLDB or the lldb-dap that comes with Swift 6. I then tried copying the path to the lldb-dap.exe into the field and VSCode says it cannot find lldp-dap debug adapter specified in the setting. I gave it the full path starting with "C:\Users" all the way to "6.0.0+Asserts\usr\bin\lldb-dap.exe" but it doesn't work like that or ending at \usr\bin without the filename (so just the directory path).

Does that Path need set and if so, what should it be?

I used "swift package init" and made a simple package with a test and I was able to get a Swift Package with the XCTestCase style testing to run (but not debug). If I right click and choose debug, it says "unable to find executable for ...\xctest.exe"

It couldn't "import Testing" to use the new swift-testing package, but I manually added it to the Package.swift file and I think I got it to find it and would work but this laptop only has 4GB of RAM so I gave up after a few minutes as the number of tasks needed kept growing and it didn't finish after a few minutes.

I was able to do use "swift package init --type executable" and use debugger in there and see the variable values if I set a breakpoint but the output never shows up anywhere (that seems to be a known issue with using Swift with VSCode at least on Windows).

I'd really like my students to be able to use the debugger in their tests so if anyone knows how to get that working, I'd appreciate it.

Thanks to everyone for their help. I'll give it another try with my student on his computer via Zoom to see if I can figure out what went wrong there.

To get all the dependency version correct for WinGet, I’ve made this.

In theory we can maintain such a list, but there should be a person who can access the release build ahead of time that maintains the piece of information.

1 Like

Worked with student again on his computer. We ended up deleting values in PATH environment variable for other Swift Toolchains after we installed the Swift 6 toolchain. We can run tests for a package, but can't debug. For an executable (swift package init --type executable), we can run it using the Terminal with "swift run" to see the output there, but can't debug (although I could on my borrowed computer).

We also went into the VSCode extensions for Swift and CodeLLDB and put the path to the Swift 6 Toolchain in the appropriate locations.

So if anyone knows how to get the debugger to work in Swift Packages (especially with tests for a Package), I'd appreciate ideas for that.

Here's the message we get in the error log about Rust:


  type: 'lldb',

  request: 'launch',

  sourceLanguages: [ 'swift' ],

  args: [],

  cwd: '${workspaceFolder:NewExe}',

  name: 'Debug NewExe',

  program: '${workspaceFolder:NewExe}\\.build\\debug\\NewExe.exe',

  preLaunchTask: 'swift: Build Debug NewExe',

  __configurationTarget: 6

}

Resolved debug configuration: {

  type: 'lldb',

  request: 'launch',

  sourceLanguages: [ 'swift' ],

  args: [],

  cwd: '${workspaceFolder:NewExe}',

  name: 'Debug NewExe',

  program: '${workspaceFolder:NewExe}\\.build\\debug\\NewExe.exe',

  preLaunchTask: 'swift: Build Debug NewExe',

  __configurationTarget: 6,

  env: { RUST_BACKTRACE: '1' },

  expressions: 'native',

  relativePathBase: 'c:\\Users\\dgoen\\Desktop\\NewExe',

  _adapterSettings: {

    displayFormat: 'auto',

    showDisassembly: 'auto',

    dereferencePointers: true,

    suppressMissingSourceFiles: true,

    evaluationTimeout: 5,

    consoleMode: 'commands',

    sourceLanguages: null,

    terminalPromptClear: null,

    evaluateForHovers: true,

    commandCompletions: true,

    reproducer: false

  }

}

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "Could not load \"C:\\\\Users\\\\dgoen\\\\AppData\\\\Local\\\\Programs\\\\Swift\\\\Toolchains\\\\6.0.0+Asserts\\\\usr\\\\bin\\\\liblldb.dll\" (err=0x0000007E)"', adapter\codelldb\bin/main.rs:57:57

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Debug adapter exit code=101 (0x65), signal=null.

The error message seems to indicate that you are still running codelldb for the DAP rather than the included lldb-dap.exe. There was a rename of the binary, so you should check if lldb-dap.exe exists or if lldb-vscode.exe (the old name) is present. It could be a configuration thing, but the mismatch between liblldb.dll is one of the reasons that we have moved away from CodeLLDB which might be the cause of the failure here.

1 Like

Sorry for the delay - I had family visiting.

I uninstalled Swift 5.10 and Swift 6.0 and downloaded the new Swift 6 nightly for Windows from Swift.org (dated July 2nd). I also updated the Swift extension in VSCode. I can't delete the CodeLLDB extension because it says the Swift extension depends on it.

I used swift package init to create a new package which makes a package with XCTest unit tests. I can run the tests ok but when I try to debug, it says it can't find \Swift\Platforms\6.0.0\Windows.platfor...\xctest.exe

The ... is because the dialog is too small.

I do have the setting Swift->Debugger->Use Debug Adapter From Toolchain on and the Swift->Debugger->Path is set to the full path ending in lldb-dap.exe and I verified that path/file exists at that location.

If I try to use the new Testing framework, it doesn't find it (i.e., I get a compile error on the line "import Testing"). I'd prefer to use it but I'm ok with the XCTest version if I can get that to work with debugging.

Do you have any more suggestions?

Thanks for your help.

Dave

CC: @grynspan (for swift-testing)

The XCTest error is weird - there should be no xctest.exe, but rather XCTest.dll. I don't really know where the exe extension is getting added. Can you get the literal error that it is giving, the \Swift\Platforms... would indicate the environment is not correct if that is the literal string.

1 Like

My colleague @plemarquand may be able to advise as he's done a fair amount of work on the VS Code plugin and may know why it's looking for XCTest.exe instead of XCTest.dll.

Swift Testing requires Swift 6. For the purposes of helping a student who's new to Swift, I would not use it just yet and would wait for the Swift 6 official release.