swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
https://swiftpackageindex.com/swift-server/swift-aws-lambda-runtime
Apache License 2.0
1.15k stars 106 forks source link

Add Support For Copying All Resources Into Final Executable #386

Closed jrosen081 closed 1 month ago

jrosen081 commented 1 month ago

Adds support for copying all resources (from all dependencies) into the final executable when packaging for aws lambda.

Motivation:

Closes #367 (and I believe #385 as well).

When an external dependency has resources in SPM, the packager would not copy them into the final executable. This caused an issue on launch where swift would try to create the resources, but this would fail to package. This fixes this.

Modifications:

Updated the packager to go through the build artifact and copy all .resources files into the new zip. This also adds a dependency to the LocalDebugging package so that I could test with that.

I also made some small updates to the packager to get them working locally with the new package directory setup (changing from 2 levels deep of folder nesting to 3).

Result:

I couldn't find any automated tests for the packager (I might have missed them), but to test this locally, I applied the following diff (to make the project build):

diff --git a/Examples/LocalDebugging/MyLambda/Lambda.swift b/Examples/LocalDebugging/MyLambda/Lambda.swift
index 397ece3..7d0cdee 100644
--- a/Examples/LocalDebugging/MyLambda/Lambda.swift
+++ b/Examples/LocalDebugging/MyLambda/Lambda.swift
@@ -14,12 +14,18 @@

 import AWSLambdaRuntime
 import Shared
+import Foundation

 // set LOCAL_LAMBDA_SERVER_ENABLED env variable to "true" to start
 // a local server simulator which will allow local debugging

 @main
-struct MyLambda: SimpleLambdaHandler {
+struct MyLambda: LambdaHandler {
+    static func main() async throws {
+        try await LambdaRuntime.init { (codable: Request, context) in
+            // Do nothing for now
+        }.run()
+    }
     func handle(_ request: Request, context: LambdaContext) async throws -> Response {
         // TODO: something useful
         Response(message: "Hello, \(request.name)!")
diff --git a/Examples/LocalDebugging/MyLambda/Package.swift b/Examples/LocalDebugging/MyLambda/Package.swift
index facef27..9c27957 100644
--- a/Examples/LocalDebugging/MyLambda/Package.swift
+++ b/Examples/LocalDebugging/MyLambda/Package.swift
@@ -1,4 +1,4 @@
-// swift-tools-version:5.7
+// swift-tools-version:6.0

 import PackageDescription

@@ -7,7 +7,7 @@ import class Foundation.ProcessInfo  // needed for CI to test the local version
 let package = Package(
     name: "MyLambda",
     platforms: [
-        .macOS(.v12)
+        .macOS(.v15)
     ],
     products: [
         .executable(name: "MyLambda", targets: ["MyLambda"])
@@ -30,7 +30,7 @@ let package = Package(
 )

 // for CI to test the local version of the library
-if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil {
+if true {// ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil {
     package.dependencies = [
         .package(name: "swift-aws-lambda-runtime", path: "../../.."),
         .package(name: "Shared", path: "../Shared"),

and then ran LAMBDA_USE_LOCAL_DEPS=true swift package archive --disable-sandbox in the MyLambda folder. This then created a zip that had both the bootstrap and the Shared_Shared.resources in it.

sebsto commented 1 month ago

Thank for the quick reaction about this. The local debugging example was based on v1 and will disappear.

I will test your changes on a test project I have. If all goes well, I will merge this PR and later remove the LocalDebugging Example

sebsto commented 1 month ago

@jrosen081 can you run our Swift Format script and add the changes to the commit ? We use teh script from the Swift NIO project, here is how I run it (I'll work on a more integrated / automatic version as soon as I manage to land the last 34 PRs)

.build/checkouts/swift-nio/scripts/check-swift-format.sh .

Thank you

sebsto commented 1 month ago

@jrosen081 I just tested against #385 and this PR solves it also. Thank you As soon as you fix the swift-format issue. I'll merge the changes

jrosen081 commented 1 month ago

.build/checkouts/swift-nio/scripts/check-swift-format.sh

Hmmm when trying to run it, I get the following error:

/Users/jackrosen/code/swift-aws-lambda-runtime/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift:30:26: error: unexpected code in parameter clause

Looks like it complains about sending Handler, but that should be fine in Swift 6 I think. And I am using Swift 6 based on the response I get from swift --version:

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)

I'm not sure how to proceed from here

jrosen081 commented 1 month ago

.build/checkouts/swift-nio/scripts/check-swift-format.sh

Hmmm when trying to run it, I get the following error:

/Users/jackrosen/code/swift-aws-lambda-runtime/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift:30:26: error: unexpected code in parameter clause

Looks like it complains about sending Handler, but that should be fine in Swift 6 I think. And I am using Swift 6 based on the response I get from swift --version:

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)

I'm not sure how to proceed from here

It actually did run the formatter, but failed after my changes. PR is updated with formatting