swiftwasm / swift

WebAssembly support for the Swift programming language
https://swiftwasm.org
1.28k stars 28 forks source link

Cannot import Foundation on DEVELOPMENT channel #5560

Closed kkebo closed 5 months ago

kkebo commented 7 months ago

Environments where I reproduced the issue

Steps to reproduce

$ curl -LO https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2023-11-21-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2023-11-21-a-ubuntu20.04_aarch64.tar.gz
$ tar xvf wift-wasm-DEVELOPMENT-SNAPSHOT-2023-11-21-a-ubuntu20.04_aarch64.tar.gz
$ export PATH=$PWD/swift-wasm-DEVELOPMENT-SNAPSHOT-2023-11-21-a/usr/bin:$PATH
$ which swift
/path/to/swift-wasm-DEVELOPMENT-SNAPSHOT-2023-11-21-a/usr/bin/swift
$ mkdir wasi-demo
$ cd wasi-demo
$ swift package init --type executable
$ echo 'import Foundation' > Sources/main.swift
$ swift build
Building for debugging...
[7/7] Linking wasi-demo
Build complete! (2.73s)
$ swift build --triple wasm32-unknown-wasi
Building for debugging...
/path/to/wasi-demo/Sources/main.swift:1:8: error: no such module 'Foundation'
import Foundation
       ^

Only recently built toolchains seem to have this problem, as swift-wasm-5.9-SNAPSHOT-2023-08-06-a was fine.

kateinoigakukun commented 7 months ago

Thank you for detailed report 😺 Recent toolchain snapshot uses the upstream prebuilt compiler binaries, and I intentionally didn't upstream a patch that makes -use-static-resource-dir default in swift-frontend to reduce implicit behaviors in low-level tools. At this time, you can build with swift build --triple wasm32-unknown-wasi -Xswiftc -Xfrontend -Xswiftc -use-static-resource-dir.

I'm planning to publish a Swift SDK with some wasm specific swiftc options like this as default.

kkebo commented 7 months ago

That makes sense. Thank you for your detailed explanation and a workaround (and also your contributions to the upstream).

I'm planning to publish a Swift SDK with some wasm specific swiftc options like this as default.

It's awesome!

kkebo commented 5 months ago

I have still been stuck on this issue. If I can't find a solution, I will wait for the Swift SDK for WASI.

$ which swift
/path/to/swift-wasm-DEVELOPMENT-SNAPSHOT-2023-12-12-a/usr/bin/swift
$ swift -version
Swift version 5.11-dev (LLVM 91cd37b9110872c, Swift 703a4719b0c6f3a)
Target: aarch64-unknown-linux-gnu
$ swift build --triple wasm32-unknown-wasi -Xswiftc -Xfrontend -Xswiftc -use-static-resource-dir
Building for debugging...
LLVM ERROR: /path/to/swift-wasm-DEVELOPMENT-SNAPSHOT-2023-12-12-a/usr/share/wasi-sysroot/usr/lib/swift_static/wasi/static-executable-args.lnk not found
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /path/to/swift-wasm-DEVELOPMENT-SNAPSHOT-2023-12-12-a/usr/bin/swiftc -L /path/to/wasi-demo/.build/wasm32-unknown-wasi/debug -o /path/to/wasi-d
emo/.build/wasm32-unknown-wasi/debug/wasi-demo.wasm -module-name wasi_demo -emit-executable /path/to/wasi-demo/.build/wasm32-unknown-wasi/debug/wasi_demo.build/main.swift.o /path/to
/wasi-demo/.build/wasm32-unknown-wasi/debug/wasi_demo.build/wasi_demo.swiftmodule.o -target wasm32-unknown-wasi -sdk /path/to/swift-wasm-DEVELOPMENT-SNAPSHOT-2023-12-12-a/us
r/share/wasi-sysroot -g -Xfrontend -use-static-resource-dir
1.      Swift version 5.11-dev (LLVM 91cd37b9110872c, Swift 703a4719b0c6f3a)
2.      Compilation construction
3.      Building compilation jobs
4.      While building jobs for driver Action link of type image
5.      While determining output for driver CommandOutput
{
    PrimaryOutputType = image;
    Inputs = [
        CommandInputPair {
            Base = /path/to/wasi-demo/.build/wasm32-unknown-wasi/debug/wasi_demo.build/main.swift.o,
            Primary = /path/to/wasi-demo/.build/wasm32-unknown-wasi/debug/wasi_demo.build/main.swift.o
        }];
    DerivedOutputFileMap = {
/path/to/wasi-demo/.build/wasm32-unknown-wasi/debug/wasi_demo.build/main.swift.o -> image: "/path/to/wasi-demo/.build/wasm32-unknown-wasi/debug/wasi-demo.wasm"
/path/to/wasi-demo/.build/wasm32-unknown-wasi/debug/wasi_demo.build/main.swift.o -> autolink: "/tmp/main.swift-6f7c1b.autolink"

    };
}
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swiftc          0x0000aaaaebb7b1d8
1  swiftc          0x0000aaaaebb79064
2  swiftc          0x0000aaaaebb7b920
3  linux-vdso.so.1 0x0000ffff0d0687d0 __kernel_rt_sigreturn + 0
4  libc.so.6       0x0000ffff0b4b2790
5  libc.so.6       0x0000ffff0b46b6fc raise + 44
6  libc.so.6       0x0000ffff0b4578b0 abort + 228
7  swiftc          0x0000aaaaebad4378
8  swiftc          0x0000aaaae642ab70
9  swiftc          0x0000aaaae641c0ac
10 swiftc          0x0000aaaae6402260
11 swiftc          0x0000aaaae64003f4
12 swiftc          0x0000aaaae63f98e0
13 swiftc          0x0000aaaae5446384
14 libc.so.6       0x0000ffff0b457b80
15 libc.so.6       0x0000ffff0b457c60 __libc_start_main + 160
16 swiftc          0x0000aaaae544479c
[1/2] Linking wasi-demo.wasm
kateinoigakukun commented 5 months ago

I'll take a look this week

kateinoigakukun commented 5 months ago

Looks like the crash issue exists only in the legacy driver. We can use the new swift-driver after https://github.com/apple/swift-driver/pull/1523, so stay tuned.

kkebo commented 5 months ago

Thank you for the information.

By the way, the new swift-driver is broken in Asahi Linux at this time. I recently build all my packages with -Xswiftc -disallow-use-new-driver. So I may not be able to use it even if the patch is merged. (I know this is not related to SwiftWasm.)

$ mkdir foo
$ swift package init --type executable
Creating executable package: foo
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
$ swift build
error: 'foo': Invalid manifest (compiled with: ["/home/kebo/.local/bin/swiftc", "-vfsoverlay", "/tmp/TemporaryDirectory.J4dMCk/vfs.yaml", "-L", "/home/kebo/.local/bin", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/home/kebo/.local/bin", "-swift-version", "5", "-I", "/home/kebo/.local/bin", "-package-description-version", "5.11.0", "/home/kebo/foo/Package.swift", "-Xfrontend", "-disable-implicit-concurrency-module-import", "-Xfrontend", "-disable-implicit-string-processing-module-import", "-o", "/tmp/TemporaryDirectory.xVIo20/foo-manifest"])
error: fatalError
/home/kebo/foo/Package.swift:4:8: error: no such module 'PackageDescription'
import PackageDescription
       ^
$ swift build -Xswiftc -disallow-use-new-driver
Building for debugging...
<unknown>:0: warning: legacy driver is now deprecated; consider avoiding specifying '-disallow-use-new-driver'
<unknown>:0: warning: legacy driver is now deprecated; consider avoiding specifying '-disallow-use-new-driver'
[7/7] Linking foo
Build complete! (1.78s)
$ swift --version
Swift version 5.11-dev (LLVM 6e97019dc6cb7d8, Swift 0cc72fb3446a846)
Target: aarch64-unknown-linux-gnu
$ swiftly list
Installed release toolchains
----------------------------

Installed snapshot toolchains
-----------------------------
main-snapshot-2024-01-13 (in use)
kateinoigakukun commented 5 months ago

The root problem is swiftly's installation process. Please ask them to stop using symlink.

kkebo commented 5 months ago

The root problem is swiftly's installation process. Please ask them to stop using symlink.

Thank you for your advice. I created https://github.com/swift-server/swiftly/issues/92 and https://github.com/apple/swift/issues/70932.

kkebo commented 5 months ago

apple/swift-driver#1523 was merged. Although the crash issue has been resolved, -use-static-resource-dir still seems to have no effect.

$ which swift
/path/to/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-01-17-a/usr/bin/swift
$ swift --version
Swift version 5.11-dev (LLVM 3c1c797448de605, Swift a48dc87f86faf49)
Target: aarch64-unknown-linux-gnu
$ swift build --triple wasm32-unknown-wasi -Xswiftc -Xfrontend -Xswiftc -use-static-resource-dir
Building for debugging...
warning: Could not read SDKSettings.json for SDK at: /path/to/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-01-17-a/usr/share/wasi-sysroot
error: emit-module command failed with exit code 1 (use -v to see invocation)
/home/kebo/wasi-demo/Sources/main.swift:1:8: error: no such module 'Foundation'
import Foundation
       ^
/home/kebo/wasi-demo/Sources/main.swift:1:8: error: no such module 'Foundation'
import Foundation
       ^
error: fatalError
kateinoigakukun commented 5 months ago

The proper command is now swift build --triple wasm32-unknown-wasi --static-swift-stdlib, could you try again with it?

kkebo commented 5 months ago

Thank you. I have successfully built the package.

$ swift build --triple wasm32-unknown-wasi --static-swift-stdlib
Building for debugging...
warning: Could not read SDKSettings.json for SDK at: /home/kebo/downloads/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-01-17-a/usr/share/wasi-sysroot
<unknown>:0: warning: libc not found for 'wasm32-unknown-wasi'; C stdlib may be unavailable
<unknown>:0: warning: libc not found for 'wasm32-unknown-wasi'; C stdlib may be unavailable
warning: Could not read SDKSettings.json for SDK at: /home/kebo/downloads/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-01-17-a/usr/share/wasi-sysroot
[7/7] Linking wasi-demo.wasm
Build complete! (2.84s)
kateinoigakukun commented 5 months ago

Making --static-swift-stdlib as default is a separate topic (implementation is here), so closing this as resolved.