"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 (GitHub - apple/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. This fork is used to manage Appleā€™s stable releases of Clang as well as support the Swift 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"

Note that the instructions on https://www.reddit.com/r/swift/comments/ixx0bl/swift_is_now_available_for_windows_10/ are currently incorrect, since they have:

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\ucrt.modulemap "%VCToolsInstallDir%\include\visualc.apinotes"
copy %SDKROOT%\usr\share\winsdk.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"

...and line 3 is wrong (has the wrong source file).

There's another set of errors about CoreFoundation after installing the module maps:

C:\Users\jesse\Projects\swift-win32>ninja -C build SwiftWin32 UICatalog
ninja: Entering directory `build'
[1/2] Linking Swift shared library bin\SwiftWin32.dll
FAILED: bin/SwiftWin32.dll Sources/CMakeFiles/SwiftWin32.dir/Application/_TriviallyConstructible.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/Application.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/ApplicationDelegate.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/ApplicationMain.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/Information.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/LaunchKeyOptions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AlertAction.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AlertController.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AnimationCurve.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Button.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Color.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ContentContainer.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ContentSizeCategoryAdjusting.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Control.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/DatePicker.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Device.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/EdgeInsets.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Font.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/FontMetrics.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Label.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ProgressView.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Responder.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Scene.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneConfiguration.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneDelegate.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneSession.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneSizeRestrictions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Screen.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Slider.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Stepper.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Switch.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TableView.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TableViewCell.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TableViewDataSource.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextField.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextInputTraits.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextView.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TraitCollection.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TraitEnvironment.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/View.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewController.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewControllerTransitionCoordinator.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewControllerTransitionCoordinatorContext.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Window.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/WindowScene.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/AffineTransform.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Point.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Rect.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Size.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/BatteryMonitor.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Error.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/IndexPath+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Logging.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Rect+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/PropertyWrappers.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/String+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WindowClass.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WindowsHandle.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WindowsHandle+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WinSDK+Extensions.swift.obj swift/SwiftWin32.swiftmodule lib/SwiftWin32.lib
cmd.exe /C "cd . && C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -output-file-map Sources\CMakeFiles\SwiftWin32.dir\output-file-map.json -incremental -j 12 -emit-library -o bin\SwiftWin32.dll -module-name SwiftWin32 -module-link-name SwiftWin32 -emit-module -emit-module-path swift\SwiftWin32.swiftmodule -emit-dependencies -DSwiftWin32_EXPORTS -sdk C:/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk -O -libc MD   -Xcc -DNONAMELESSUNION -Xcc -DCOBJMACROS  @CMakeFiles\SwiftWin32.rsp    -Xlinker -implib:lib\SwiftWin32.lib  && cd ."
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
ninja: build stopped: subcommand failed.

You missed some of the flags that you needed to pass or there is a typo in the flags. Either way, this is something that is expected for the time being. The newer snapshots actually have significantly improved things (thanks to help from @millenomi) by removing the need for the headers all together and further improvements to the installer to reduce the needed flags.

I noted that the project that you are using there is Swift/Win32, for which the current instructions are not compatible with 5.3 any longer - that project has moved onto the latest version of Swift development. For Swift/WIn32 specific issues though, I would recommend that you report issues to the project via GitHub as these forums are not for that project.

@compnerd I am currently "suffering" from a similar error, the error as noted in SR-14578, would be nice if this could be resolved soon... Thanks.

SR-14578 is different from this issue. This issue was due to the setup not being run through to completion. SR-14578 is due to a missing change in Foundation that should already be there in the 5.5 snapshots.