How to invoke a repl that loads your local swift package?

I was following along with this older blog post on loading a swift REPL for a package, and found that path no longer appears to work.

When I invoke it, it does the build and attempts to load a REPL, but then fails out:

Running the command swift run --repl results in a "you got the command wrong" kind of response:

Launching Swift REPL with arguments: -I/Users/heckj/src/Scale/.build/arm64-apple-macosx/debug -L/Users/heckj/src/Scale/.build/arm64-apple-macosx/debug -lscale__REPL -I/Users/heckj/src/Scale/.build/checkouts/swift-numerics/Sources/_NumericsShims/include

Welcome to Swift!

Subcommands:

  swift build      Build Swift packages
  swift package    Create and work on packages
  swift run        Run a program from a package
  swift test       Run package tests
  swift repl       Experiment with Swift code interactively

  Use `swift --help` for descriptions of available options and flags.

  Use `swift help <subcommand>` for more information about a subcommand.

When I invoke swift repl directly, that works - but my local package isn't loaded. Is there still a fast-path to set up a REPL that can use a local swift package?

(When I combine the details from swift run --repl into the CLI arguments for swift repl - that DOES seem to work)

1 Like

have you solved this problem? I've got the same problem as yours.
If you solved it, could you please share your solutions and help me out.

The only solution I found was to load and run a package I was immediately working on is the one I have at the very top - using the -I and -L options above to explicitly include the built packages (after a swift build)

If you don't need a repl, but just want to run something, then using swift run with a script of swift code does a decent job, but that doesn't leave you in a REPL where you can interactively explore and dynamically add commands.

For my "trying things out" while developing library code, I mostly tend to use the later, simply because it's much easier. In my python coding days, switching a REPL, loading code and exploring was a common pattern - but that doesn't appear to translate very well over to working in Swift.

Curious. swift run --repl seems to work OK for me in Swift 5.7.2 on macOS:

$ swift --version
swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: arm64-apple-macosx13.0

$ mkdir TestPackage
$ cd TestPackage
$ swift package init --type library
Creating library package: TestPackage
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/TestPackage/TestPackage.swift
Creating Tests/
Creating Tests/TestPackageTests/
Creating Tests/TestPackageTests/TestPackageTests.swift

$ swift run --repl
…
Welcome to Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51).
Type :help for assistance.
  1> import TestPackage
  2> TestPackage().text
$R0: String = "Hello, World!"
  3>

You can see that swift run --repl indeed launches the REPL and I can import the package's library and call its API.

2 Likes

That's nice to know, thanks for posting that it's working with Swift 5.7.2!