Skip to content

FileManager.default.createFile() doesn't work #5593

Closed
swiftlang/swift-foundation
#992
@kkebo

Description

@kkebo

Description

FileManager.default.createFile() doesn't work.

Steps to reproduce

  1. mkdir foo
  2. cd foo
  3. swift package init --type executable
  4. Edit Sources/main.swift
    import Foundation
    
    print(FileManager.default.createFile(atPath: "/tmp/foo", contents: Data()))
  5. swift build --swift-sdk <Swift SDK for Wasm>
  6. wasmtime run --dir /tmp .build/debug/foo.wasm

Expected behavior

$ wasmtime run --dir /tmp .build/debug/foo.wasm
true

Actual behavior

$ wasmtime run --dir /tmp .build/debug/foo.wasm
false

Environment

$ uname -a
Linux Raspberry-beetle 6.6.51+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.6.51-1+rpt2 (2024-10-01) aarch64 GNU/Linux
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
$ swift --version
Swift version 6.0.2-dev (LLVM 43d73cb0cc589f7, Swift aaa632cea622394)
Target: aarch64-unknown-linux-gnu
$ swift sdk list
6.0-SNAPSHOT-2024-09-18-a-wasm32-unknown-wasi
$ wasmtime -V
wasmtime 25.0.1 (b4cb894c9 2024-09-24)

Supplement

swift-foundation's createFile() implementation forces to use the option .atomic.

So we can see the detailed error by running try Data().write(to: URL(filePath: "/tmp/foo"), options: .atomic).

Swift/ErrorType.swift:253: Fatal error: Error raised at top level: FoundationEssentials.CocoaError(code: FoundationEssentials.CocoaError.Code(rawValue: 3328), userInfo: ["NSUserStringVariant": "Folder"])
Error: failed to run main module `.build/debug/hoge.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
           0: 0x6c1b71 - <unknown>!$ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAHSus6UInt32VtF
           1: 0x76a8da - <unknown>!swift_errorInMain
           2: 0x2c755 - <unknown>!main
           3: 0xc2f036 - <unknown>!__main_void
           4: 0x2c2b9 - <unknown>!_start
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    2: wasm trap: wasm `unreachable` instruction executed

If options contain .atomic, createFile() will call writeToFileAux(), and writeToFileAux() will call createProtectedTemporaryFile() and createTemporaryFile(). However, SwiftWasm doesn't support createTemporaryFile().

How about stopping adding .atomic in createFile() if os(WASI)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions