"Cannot load underlying module for WinSDK"

I have a simple main.swift

import Foundation

print("imported")

With this batch file:

set SDKROOT=C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk
set SWIFTFLAGS=-sdk %SDKROOT% -I %SDKROOT%/usr/lib/swift -L %SDKROOT%/usr/lib/swift/windows
swiftc -v -o main.exe main.swift %SWIFTFLAGS%

And when I try to compile it using the 5.2.1 toolchain from the github /swift-build releases page it shows this error about not being able to load the underlying WinSDK module:

E:\swiftCode\Project1>mainExeCompile.bat
compnerd.org Swift version 5.2.2-dev (Swift a543bfba1b)
Target: x86_64-unknown-windows-msvc
"C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe" -frontend -c -primary-file main.swift -target x86_64-unknown-windows-msvc -disable-objc-interop -sdk "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk" -color-diagnostics -I "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift" -autolink-library oldnames -autolink-library msvcrt -Xcc -D_MT -Xcc -D_DLL -module-name main -o "C:\Users\Costin\AppData\Local\Temp\main-bb5632.o"
e[1m:0: e[0me[0;1;31merror: e[0me[1mcannot load underlying module for 'WinSDK'
e[0m
E:\swiftCode\Project1>

But if I try to import another say import Swift or import SwiftOnoneSupport it all compiles well:

E:\swiftCode\Project1>mainExeCompile.bat

E:\swiftCode\Project1>set SDKROOT=C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk

E:\swiftCode\Project1>set SWIFTFLAGS=-sdk C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk -I C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift -L C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows

E:\swiftCode\Project1>swiftc -v -o main.exe main.swift -sdk C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk -I C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift -L C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows
compnerd.org Swift version 5.2.2-dev (Swift a543bfba1b)
Target: x86_64-unknown-windows-msvc
"C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe" -frontend -c -primary-file main.swift -target x86_64-unknown-windows-msvc -disable-objc-interop -sdk "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk" -color-diagnostics -I "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift" -autolink-library oldnames -autolink-library msvcrt -Xcc -D_MT -Xcc -D_DLL -module-name main -o "C:\Users\Costin\AppData\Local\Temp\main-d3bd06.o"
"C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\clang.exe" -target x86_64-unknown-windows-msvc -nostartfiles -L "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows/x86_64" -L "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift/x86_64" "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows\x86_64\swiftrt.obj" "C:\Users\Costin\AppData\Local\Temp\main-d3bd06.o" -I "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk" -L "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows" -v -o main.exe
compnerd.org clang version 9.0.0 (https://github.com/apple/llvm-project c2133f2b08047708b17230b598cb789062143650) (based on LLVM 9.0.0)
Target: x86_64-unknown-windows-msvc
Thread model: posix
InstalledDir: C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\Hostx64\x64\link.exe" -out:main.exe "-libpath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\lib\x64" "-libpath:C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x64" "-libpath:C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x64" "-libpath:C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows/x86_64" "-libpath:C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift/x86_64" "-libpath:C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows" -nologo "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows\x86_64\swiftrt.obj" "C:\Users\Costin\AppData\Local\Temp\main-d3bd06.o"
Creating library main.lib and object main.exp
E:\swiftCode\Project1>

I've taken a look inside WinSDK.swiftinterface and noticed this strange line:

// swift-interface-format-version: 1.0
// swift-compiler-version: compnerd.org Swift version 5.2.2-dev (Swift a543bfba1b)
// swift-module-flags: -target x86_64-unknown-windows-msvc -disable-objc-interop -autolink-force-load -enable-library-evolution -module-link-name swiftWinSDK -swift-version 5 -O -enforce-exclusivity=unchecked -module-name WinSDK
import Swift
@_exported import WinSDK // <---- here 
public let HANDLE_FLAG_INHERIT: WinSDK.DWORD

Could this be the cause? I know this is just a generated interface file but how come is it valid as a module to import yourself?

I've attached the zipped windows folder in case someone wants to take a look on the import libs and swiftmodules here

There is a follow up step that needs to be performed manually after installation to make the Windows SDK accessible to swift.

You need to install the module maps into the Visual Studio installation. Because this modifies the Visual Studio installation, you need to run the following from an elevated ("Administrator") Native x64 Developer command prompt.

set SDKROOT=%SystemDrive%\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk
copy "%SDKROOT%\usr\share\ucrt.modulemap" "%UniversalCRTSdkDir%\Include\%UCRTVersion%\ucrt\module.modulemap"
copy "%SDKROOT%\usr\share\visualc.modulemap" "%VCToolsInstallDir%\include\module.modulemap"
copy "%SDKROOT%\usr\share\visualc.apinotes" "%VCToolsInstallDir%\include\visualc.apinotes"
copy "%SDKROOT%\usr\share\winsdk.modulemap" "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"
Terms of Service

Privacy Policy

Cookie Policy