swiftwasm / swift

WebAssembly support for the Swift programming language

Home Page:https://swiftwasm.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`canImport(Dispatch)` is wrongly truthy on WASI target

robbertvanginkel opened this issue · comments

Description
While trying to compile the same swift to wasm from both linux and macos, I noticed that some builds would fail depending on the host OS for the same toolchain release.

Steps to reproduce
For the following swift file:

var x = "nodispatch"
#if canImport(Dispatch)
import Dispatch
var q = DispatchQueue(label: "foo", attributes: .concurrent)
x = "yesdispatch"
#endif
print(x)

On macos:

$ curl -SL -O https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.7-SNAPSHOT-2023-01-17-a/swift-wasm-5.7-SNAPSHOT-2023-01-17-a-macos_arm64.pkg
$ pkgutil --expand-full swift-wasm-5.7-SNAPSHOT-2023-01-17-a-macos_arm64.pkg toolchain/
$ export PATH="$PWD/toolchain/Payload/usr/bin:$PATH"
$ swiftc -target wasm32-unknown-wasi test.swift -o test.wasm
$ wasmtime test.wasm
nodispatch

But on linux (tested under docker run -it ubuntu:20.04):

$ curl -SL -O https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.7-SNAPSHOT-2023-01-17-a/swift-wasm-5.7-SNAPSHOT-2023-01-17-a-ubuntu20.04_aarch64.tar.gz
$ tar -xf swift-wasm-5.7-SNAPSHOT-2023-01-17-a-ubuntu20.04_aarch64.tar.gz
$ export PATH="/swift-wasm-5.7-SNAPSHOT-2023-01-17-a/usr/bin:$PATH"
$ swiftc -target wasm32-unknown-wasi test.swift -o test.wasm
test.swift:5:9: error: cannot find 'DispatchQueue' in scope
var q = DispatchQueue(label: "foo", attributes: .concurrent)
        ^~~~~~~~~~~~~

Expected behavior
I expected some difference in behavior for swift on macos vs linux, but given that I'm targetting wasm here I expected a statement like #if canImport(Dispatch) would be consistently pass/failing depending on the target toolchain irrespective of the host.

Thank you for creating a ticket for this :)
This is an issue of the toolchain directory layout. The libdispatch's mdoulemap is placed at usr/lib/swift_static/dispatch/module.modulemap but the directory is shared between all platforms. This is apparently wrong place considering crosscompiling. Ideally, that should be moved to platform-specific directory like usr/lib/swift_static/wasi/ to avoid this kind of issue.

But I haven't tried this yet and I think it's better to discuss the toolchain layout in the forum at first to fix it in upstream also.