kabiroberai / node-swift

Create Node modules in Swift
MIT License
492 stars 16 forks source link

Building example fails with error: call to global actor 'NodeActor'-isolated initializer 'init(name:callback:)' in a synchronous nonisolated context #26

Closed justjake closed 4 months ago

justjake commented 4 months ago

It appears the init of NodeFunction isn't valid because it's async?

$ xcodebuild -version
Xcode 15.4
Build version 15F31d

$ npm i

> node-swift-example@1.0.0 install
> node-swift rebuild

[2/2] Initializing...
Fetching https://github.com/apple/swift-syntax.git from cache
Fetched https://github.com/apple/swift-syntax.git from cache (0.59s)
Computing version for https://github.com/apple/swift-syntax.git
Computed https://github.com/apple/swift-syntax.git at 510.0.2 (0.65s)
Creating working copy for https://github.com/apple/swift-syntax.git
Working copy of https://github.com/apple/swift-syntax.git resolved at 510.0.2
Building for production...
/Users/jitl/src/node-swift/example/Sources/MyExample/MyExample.swift:6:20: error: call to global actor 'NodeActor'-isolated initializer 'init(name:callback:)' in a synchronous nonisolated context
        "add": try NodeFunction { (a: Double, b: Double) in
                   ^
/Users/jitl/src/node-swift/example/Sources/MyExample/MyExample.swift:3:1: note: in expansion of macro 'NodeModule' here
#NodeModule(exports: [
^~~~~~~~~~~~~~~~~~~~~~
/Users/jitl/src/node-swift/example/node_modules/node-swift/Sources/NodeAPI/Sugar.swift:23:24: note: calls to initializer 'init(name:callback:)' from outside of its actor context are implicitly asynchronous
    public convenience init<each A: AnyNodeValueCreatable>(
                       ^
error: fatalError

/Users/jitl/src/node-swift/lib/builder.js:259
                throw new Error(`swift build exited with status ${result.status}`);
                      ^

Error: swift build exited with status 1
    at Object.<anonymous> (/Users/jitl/src/node-swift/lib/builder.js:259:23)
    at Generator.next (<anonymous>)
    at /Users/jitl/src/node-swift/lib/builder.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/jitl/src/node-swift/lib/builder.js:4:12)
    at Object.build (/Users/jitl/src/node-swift/lib/builder.js:60:12)
    at /Users/jitl/src/node-swift/lib/cli.js:61:23
    at Generator.next (<anonymous>)
    at /Users/jitl/src/node-swift/lib/cli.js:32:71
    at new Promise (<anonymous>)
kabiroberai commented 4 months ago

This looks like a regression due to SE-0411. The fix is to use #NodeModule { return ... } instead of #NodeModule(exports: ...). Will update the readme accordingly, thanks for flagging this.

kabiroberai commented 4 months ago

Found a better fix that involved updating the macro instead. @justjake could you please try the README example again using the latest commit (specify branch: "main" for the SwiftPM dependency on node-swift) and check if it works?

justjake commented 4 months ago

Thanks for the quick response - I'm not sure what you mean by branch: "main" - I just checked out this repo and ran npm install in the example directory to produce that error

kabiroberai commented 4 months ago

ah makes sense, yes you should be able to do that successfully now