Interacting with running application from command line (Vapor REPL)

I'm currently having a problem with my Vapor application: In my tests, and locally on my machine, an endpoint (returns an array of objects after filtering & sorting) works exactly as expected. But on the server (both on Heroku & in my Docker production environment) it responds with an empty array. Now I'd like to try out some database requests to determine the part that behaves differently than expected on the server – but I can't find an easy way to do it. I already set the LOG_LEVEL to trace on Heroku (my staging/dev server), now I can see the entire SQL query, but it all looks correct.

I really need to run some real Vapor db requests code in my server environment – is there an easy way to do it like with rails console in Ruby on Rails? If no, what's the closest thing to it? Is there a workaround? And can we add it to a future version of Vapor?

Have you tried REPL Support for Swift Packages?

1 Like

Thank you, I didn't know about that. But on first try that also didn't work, couldn't import Vapor or initialize a model or anything, basically:

MyDevice:MyServer me$ swift run --repl
Launching Swift REPL with arguments: -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug -L/Users/Me/MyServer/.build/x86_64-apple-macosx/debug -lMyServer__REPL -I/Users/Me/MyServer/.build/checkouts/swift-crypto/Sources/CCryptoBoringSSLShims/include -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CURLParser.build -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CNIOAtomics.build -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CBacktrace.build -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CNIOSHA1.build -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CNIOLinux.build -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CNIOHTTPParser.build -I/Users/Me/MyServer/.build/checkouts/swift-nio/Sources/CNIOWindows/include -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CNIOBoringSSLShims.build -I/Users/Me/MyServer/.build/checkouts/vapor/Sources/CBase32/include -I/Users/Me/MyServer/.build/checkouts/swift-crypto/Sources/CCryptoBoringSSL/include -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CNIODarwin.build -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CNIOExtrasZlib.build -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CMultipartParser.build -I/Users/Me/MyServer/.build/checkouts/vapor/Sources/CBcrypt/include -I/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CNIOBoringSSL.build
Welcome to Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28).
Type :help for assistance.
  1> import Vapor
warning: Swift error in fallback scratch context: error: /Users/Me/MyServer/.build/checkouts/swift-crypto/Sources/CCryptoBoringSSLShims/include/module.modulemap:1:8: error: redefinition of module 'CCryptoBoringSSLShims'
module CCryptoBoringSSLShims {
       ^

/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CCryptoBoringSSLShims.build/module.modulemap:1:8: note: previously defined here
module CCryptoBoringSSLShims {
       ^

error: /Users/Me/MyServer/.build/checkouts/swift-crypto/Sources/CCryptoBoringSSL/include/module.modulemap:1:8: error: redefinition of module 'CCryptoBoringSSL'
module CCryptoBoringSSL {
       ^

/Users/Me/MyServer/.build/x86_64-apple-macosx/debug/CCryptoBoringSSL.build/module.modulemap:1:8: note: previously defined here
module CCryptoBoringSSL {
       ^

error: could not build Objective-C module 'SwiftOverlayShims'


note: This error message is displayed only once. If the error displayed above is due to conflicting search paths to Clang modules in different images of the debugged executable, this can slow down debugging of Swift code significantly, since a fresh Swift context has to be created every time a conflict is encountered.

warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "App" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "AsyncHTTPClient" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "AsyncKit" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "Backtrace" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "COperatingSystem" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "ConsoleKit" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "Core" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "CoreMetrics" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "Crypto" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "Fluent" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "FluentKit" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "FluentPostgresDriver" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "FluentSQL" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "HandySwift" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "Logging" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "Metrics" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "MultipartKit" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIO" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOConcurrencyHelpers" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOExtras" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOFoundationCompat" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOHPACK" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOHTTP1" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOHTTP2" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOHTTPCompression" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOSSL" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOTLS" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOTransportServices" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "NIOWebSocket" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "PostgresKit" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "PostgresNIO" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "PublicHolidays" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "RoutingKit" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "SQLKit" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "Vapor" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
warning: (x86_64) /Users/Me/MyServer/.build/x86_64-apple-macosx/debug/libMyServer__REPL.dylib unable to load swift module "WebSocketKit" (failed to get module "App" from AST context:
error: missing required module 'CNIOAtomics'
)
expression failed to parse, unknown error

But maybe someone has some tips how to get it working with Vapor?

Unfortunately there won't really ever be a way to emulate rails console due to the compiled nature of Swift. And using the REPL won't work because it (currently) doesn't integrate with Swift packages. You can however start an interactive bash session on the production Docker container to test the DB connection etc. Have you checked that things actually exist in the production DB and run the same SQL query against the production DB? The fact you're getting an empty array instead of an error would indicate that everything is working correctly, there's just no data to return

@0xTim Yes, I agree, it seems there is something wrong with my request. But in my tests it works as expected, so I need to directly do things on the server to see what's going on there, that's what I needed the console for. But I will try to make direct DB requests then, I'm not sure though how I can print out the raw SQL query. I added the trace log level on the dev server and can see the SQL query (but with escaped \ characters), it's using $1, $2 etc all over the place though and I'm not sure if the provided binds array has values formatted the right way for PostgreSQL. On first try, it didn't work.

Is there a way to print the full raw SQL query so I can copy that and test things with it? Can't find it documented anywhere ...

UPDATE:
I just figured out that the binds is clearly not formatted correctly, so I can't just copy & paste them to replace the $1, $2, etc. – as an example: When filtering by a UUID field I had to put the UUID printed out in the logs into this wrapper: uuid('<my_uuid>'). Likewise, the dates formatted like 2021-03-22 00:00:00 +0000 does also not work directly. Have to still figure that out ... a full raw query output including all the variables with correct formatting would be very helpful!

After some trial and error, I finally found the issue – I was using the following VaporArray struct in my API client code:

/// Array wrapper for any kind of paginated collection response.
public struct VaporArray<T: ServerResponse>: Codable, Equatable {
  /// Metadata holding information about pagination context.
  public struct Metadata: Codable, Equatable {
    /// Current page number.
    public let page: Int
    /// Number of elements per page.
    public let per: Int
    /// Number of total items in collection.
    public let total: Int
  }

  /// The metadata object holding information about pagination.
  public let metadata: Metadata
  /// The requested item response objects.
  public let items: [T]
}

This always worked for all paginated endpoints. But on non-paginated ones (because I know they will have a maximum of 10 elements), I received the items array directly, without any metadata field. All this time, I was thinking the issue lies on the server, but my code was working fine (the array was not empty). I just had an expectation error. I must say, I find it inconsistent that the structure of lists can be different dependent on pagination turned on or not, but that's a separate topic I guess. :sweat_smile:

Just to confirm if you set the log level to debug and pin your PostgresNIO to 1.4.4 you should see the queries printed. (1.5 contained a very large number of improvements to Postgres and its state machine but broke printing out the SQL statements - there's an open issue for it on GH and it should be fixed soon)

1 Like