Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yams is not compatible with Swift REPL #325

Closed
FunkyMonkey14 opened this issue Jun 20, 2021 · 6 comments
Closed

Yams is not compatible with Swift REPL #325

FunkyMonkey14 opened this issue Jun 20, 2021 · 6 comments

Comments

@FunkyMonkey14
Copy link

I am using Yams as a dependency for my swift package library. Yams is included as a swift package in a standard way. I use the command swift run --repl for running my library in REPL mode. This command runs the Swift interpreter with these arguments:

Launching Swift REPL with arguments: -I/Users/dan/Documents/Development/Projects/Toolkit/.build/x86_64-apple-macosx/debug -L/Users/dan/Documents/Development/Projects/Toolkit/.build/x86_64-apple-macosx/debug -lToolkit__REPL -I/Users/dan/Documents/Development/Projects/Toolkit/.build/checkouts/Yams/Sources/CYaml/include

Swift REPL session is started but unfortunately, the session in Swift REPL is often interrupted with error messages like (not only once but quite often)

warning: (x86_64) /Users/dan/Documents/Development/Projects/Toolkit/.build/x86_64-apple-macosx/debug/libToolkit__REPL.dylib unable to load swift module "Yams" (failed to get module "Toolkit" from AST context:
error: missing required module 'CYaml'
)

Besides these annoying errors, it looks like Yams somehow works. It mostly gives good results. I tried also different Yaml libraries for Swift, but as far as I know, only Yams supports Swift Codable.

When I am using Yams in Swift script, it often works. But sometimes it returns zsh: segmentation fault error. But usage with Swift REPL is still problematic. I would be more than happy if there is some solution. Using Yams with REPL is very useful. I know that problem with CYaml was already discussed in a different context. Do you have some tips on how to fix this, please? Thank you.

This is my Package.swift. It defines one library and two executables.

// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Toolkit",
    platforms: [
        .macOS(.v10_15)
    ],
    products: [
        .executable(name: "ToolkitTester", targets: ["ToolkitTester"]),
        .executable(name: "Timer", targets: ["Timer"]),
        .library(name: "Toolkit", targets: ["Toolkit"])
    ],
    dependencies: [
        .package(url: "https://github.com/JohnSundell/Files.git", .branch("master")),
        .package(url: "https://github.com/kareman/SwiftShell.git", .branch("master")),
        .package(url: "https://github.com/DanielCech/Moderator.git", .branch("master")),
        .package(url: "https://github.com/stencilproject/Stencil.git", from: "0.13.0"),
        .package(url: "https://github.com/SwiftGen/StencilSwiftKit.git", from: "2.7.2"),
        .package(url: "https://github.com/onevcat/Rainbow.git", from: "4.0.0"),
        // .package(name: "Yaml", url: "https://github.com/behrang/YamlSwift.git", .branch("master")),
        .package(url: "https://github.com/jpsim/Yams.git", from: "4.0.6")
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "Toolkit",
            dependencies: ["Files", "Moderator", "SwiftShell", "Stencil", "StencilSwiftKit", "Rainbow", "Yams"]
        ),
        .target(
            name: "ToolkitTester",
            dependencies: ["Files", "Moderator", "SwiftShell", "Stencil", "StencilSwiftKit", "Toolkit", "Rainbow", "Yams"]
        ),
        .target(
            name: "Timer",
            dependencies: ["Files", "Moderator", "SwiftShell", "Stencil", "StencilSwiftKit", "Toolkit", "Rainbow", "Yams"]
        ),
        .testTarget(
            name: "ToolkitTests",
            dependencies: ["Toolkit"]
        )
    ]
)
@jpsim
Copy link
Owner

jpsim commented Jun 20, 2021

Sounds like a Swift REPL bug to me, but I can’t file one there on your behalf without knowing more about it. For example, what steps can I take to reproduce the issue?

@FunkyMonkey14
Copy link
Author

FunkyMonkey14 commented Jun 21, 2021

Hey @jpsim, please try this minimalistic example:

  1. Create an empty directory, for example YamsTest and access it in terminal.
  2. Call swift package init for Swift package creation
  3. Please add Yams as dependency. The Package.swift will look like this:
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "YamsTest",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "YamsTest",
            targets: ["YamsTest"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/jpsim/Yams.git", from: "4.0.6")
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "YamsTest",
            dependencies: ["Yams"]),
        .testTarget(
            name: "YamsTestTests",
            dependencies: ["YamsTest"]),
    ]
)
  1. In terminal in YamsTest folder run: swift run --repl. It initializes Yams dependency and start the REPL.
  2. The warning is not visible immediately, only after accessing the Yams methods. The used pieces of code are from Yams README.md:
❯ swift run --repl
Fetching https://github.com/jpsim/Yams.git
Cloning https://github.com/jpsim/Yams.git
Resolving https://github.com/jpsim/Yams.git at 4.0.6
[25/25] Linking libYamsTest__REPL.dylib

* Build Completed!
Launching Swift REPL with arguments: -I/Users/dan/Documents/Development/Projects/YamsTest/.build/x86_64-apple-macosx/debug -L/Users/dan/Documents/Development/Projects/YamsTest/.build/x86_64-apple-macosx/debug -lYamsTest__REPL -I/Users/dan/Documents/Development/Projects/YamsTest/.build/checkouts/Yams/Sources/CYaml/include
Welcome to Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55).
Type :help for assistance.
  1> import Yams
  2> struct S: Codable {
  3.     var p: String
  4. }
  5> let s = S(p: "test")
s: S = {
  p = "test"
}
  6> let encoder = YAMLEncoder()
warning: (x86_64) /Users/dan/Documents/Development/Projects/YamsTest/.build/x86_64-apple-macosx/debug/libYamsTest__REPL.dylib unable to load swift module "Yams" (failed to get module "Yams" from AST context:
error: missing required module 'CYaml'
)
warning: (x86_64) /Users/dan/Documents/Development/Projects/YamsTest/.build/x86_64-apple-macosx/debug/libYamsTest__REPL.dylib unable to load swift module "YamsTest" (failed to get module "Yams" from AST context:
error: missing required module 'CYaml'
)
warning: (x86_64) /Users/dan/Documents/Development/Projects/YamsTest/.build/x86_64-apple-macosx/debug/libYamsTest__REPL.dylib unable to load swift module "Yams" (failed to get module "Yams" from AST context:
error: missing required module 'CYaml'
)
warning: (x86_64) /Users/dan/Documents/Development/Projects/YamsTest/.build/x86_64-apple-macosx/debug/libYamsTest__REPL.dylib unable to load swift module "YamsTest" (failed to get module "Yams" from AST context:
error: missing required module 'CYaml'
)
encoder: Yams.YAMLEncoder = {
  options = {
    canonical = false
    indent = 0
    width = 0
    allowUnicode = false
    lineBreak = ln
    explicitStart = false
    explicitEnd = false
    version = nil
    sortKeys = false
  }
}

You can see several warnings there but it somehow works. I also tried to run Swift REPL differently - not using the swift run --repl but using swift -I/Users/dan/Documents/Development/Projects/YamsTest/.build/x86_64-apple-macosx/debug -L/Users/dan/Documents/Development/Projects/YamsTest/.build/x86_64-apple-macosx/debug -lYamsTest__REPL -I/Users/dan/Documents/Development/Projects/YamsTest/.build/checkouts/Yams/Sources/CYaml/include The result is the same.

I am using Swift 5.2, Xcode 12.5, macOS Big Sur.

@jpsim
Copy link
Owner

jpsim commented Jun 21, 2021

Thanks for that info, I can reproduce the issue more simply now:

$ swift run --repl
[1/1] Linking libYams-325__REPL.dylib
Launching Swift REPL with arguments: -I/Users/jsimard/Projects/Yams-325/.build/x86_64-apple-macosx/debug -L/Users/jsimard/Projects/Yams-325/.build/x86_64-apple-macosx/debug -lYams-325__REPL -I/Users/jsimard/Projects/Yams-325/.build/checkouts/Yams/Sources/CYaml/include
Welcome to Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28).
Type :help for assistance.
  1> import Yams
  2> YamlError.no
warning: (x86_64) /Users/jsimard/Projects/Yams-325/.build/x86_64-apple-macosx/debug/libYams-325__REPL.dylib unable to load swift module "Yams" (failed to get module "Yams" from AST context:
error: missing required module 'CYaml'
)
warning: (x86_64) /Users/jsimard/Projects/Yams-325/.build/x86_64-apple-macosx/debug/libYams-325__REPL.dylib unable to load swift module "Yams_325" (failed to get module "Yams" from AST context:
error: missing required module 'CYaml'
)
$R0: Yams.YamlError = <invalid> (0x0)

I get similar issues when running with Swift 5.5 bundled with Xcode 13 beta 1. I'll file a bug at bugs.swift.org and report back.

@jpsim
Copy link
Owner

jpsim commented Jun 21, 2021

Minimal repro project: Yams-325.zip

In the root of the project, running the following 3 commands triggers the error: swift run --repl, import Yams, YamlError.no

@jpsim
Copy link
Owner

jpsim commented Jun 21, 2021

I've filed https://bugs.swift.org/browse/SR-14806, please follow along there.

@jpsim jpsim closed this as completed Jun 21, 2021
@FunkyMonkey14
Copy link
Author

@jpsim Great, thank you 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants