Support backtraces for crashing Swift binaries#290
Conversation
3d6178b to
68e0e09
Compare
Codecov Report❌ Patch coverage is @@ Coverage Diff @@
## main #290 +/- ##
==========================================
- Coverage 13.11% 13.04% -0.07%
==========================================
Files 137 137
Lines 25461 25603 +142
==========================================
+ Hits 3338 3340 +2
- Misses 22123 22263 +140
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 2 files with indirect coverage changes 🚀 New features to boost your workflow:
|
…171) This is needed for swift-backtrace #154 . Following PR should be able to address that once we figure out https://forums.swift.org/t/where-to-source-swift-backtrace-binaries/84499 Motivation ---------- #154 ability to see backtraces for swift binaries. But generally it's also useful to set environment variables in containers for certain use cases. Modifications ------------- Allow passing in `--env`, so you can direct `SWIFT_BACKTRACE`. Result ------ Swift backtrace is possible on the end-user side. Example: wendylabsinc/wendy-agent#290 Test Plan --------- I did manual testing already in wendy-agent. You can trivially test this by passing `--env` and reading it out in a swift app with the following code: ```swift import Foundation print(ProcessInfo.processInfo.environment) ```
|
PR upstream is merged |
|
Just recalled something, we'll want to add a check for the adopted swift-container-plugin version. Needs to be 1.3.0 or higher |
Add warning if too low, and omit swift-backtrace
There was a problem hiding this comment.
Pull request overview
Adds support for Swift crash backtraces when running/building Swift apps in Wendy-built containers, gated behind a newer swift-container-plugin version, and adds a small example app to validate crash behavior.
Changes:
- Preserve image-provided environment variables in the agent by merging image config env with Wendy’s base env.
- Detect
swift-container-pluginversion (cache it) and pass extra--env/--resourcestoswift package build-container-imageto enableswift-backtrace. - Bump example dependency version and add a new “HelloCrash” example package for validating backtraces.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| Sources/WendyAgent/Services/WendyContainerService.swift | Merge image config env with Wendy base env when generating OCI spec. |
| Sources/Wendy/cli/commands/RunCommand.swift | Detect plugin version (cached) and attempt to include swift-backtrace + env during wendy run. |
| Sources/Wendy/cli/commands/PackageCache.swift | Cache container plugin version alongside previous package analysis results. |
| Sources/Wendy/cli/commands/BuildCommand.swift | Enforce/upgrade plugin version and attempt to include swift-backtrace + env during wendy build. |
| Sources/Wendy/SwiftPM.swift | Add --env passthrough to container build; add dependency search + semver compare helper; add updateDependencies(). |
| Examples/HelloHTTP/Package.swift | Bump swift-container-plugin minimum version to 1.3.0. |
| Examples/HelloCrash/Sources/main.swift | New crash-on-launch sample to validate backtrace behavior. |
| Examples/HelloCrash/Package.swift | New minimal Swift package for the crash example. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 10 out of 11 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 12 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
# Conflicts: # Sources/Wendy/cli/commands/BuildCommand.swift # Sources/Wendy/cli/commands/RunCommand.swift
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 10 out of 11 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Add swift-backtrace binaries for crash reporting | ||
| findBacktrace: for binaryName in [ | ||
| "swift-backtrace-static-linux-arm64", | ||
| "swift-backtrace-linux-arm64", | ||
| ] where backtraceAvailable { | ||
| let destination = "/swift-backtrace" | ||
| if let backtraceUrl = Bundle.module.url( | ||
| forResource: binaryName, | ||
| withExtension: nil | ||
| ) { | ||
| resources.append((source: backtraceUrl.path(), destination: destination)) | ||
| additionalEnv.append( | ||
| "SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no,swift-backtrace=/swift-backtrace" | ||
| ) | ||
| break findBacktrace | ||
| } | ||
| let backtraceUrl = URL(fileURLWithPath: CommandLine.arguments[0]) | ||
| .deletingLastPathComponent() | ||
| .appending(path: "wendy-agent_wendy.bundle") | ||
| .appending(path: "Contents") | ||
| .appending(path: "Resources") | ||
| .appending(path: "Resources") | ||
| .appending(component: binaryName) | ||
|
|
||
| if FileManager.default.fileExists(atPath: backtraceUrl.path()) { | ||
| resources.append( | ||
| (source: backtraceUrl.path(), destination: destination) | ||
| ) | ||
| additionalEnv.append( | ||
| "SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no,swift-backtrace=/swift-backtrace" | ||
| ) | ||
| break findBacktrace | ||
| } | ||
| } |
There was a problem hiding this comment.
When backtraceAvailable is true but neither backtrace binary is found, the build proceeds silently without backtraces. Consider emitting a warning in that case (similar to the ds2 handling below) to make missing resources diagnosable.
d557da3 to
e8eff90
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 10 out of 11 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| entry[entry.index(after: separatorIndex)...] | ||
| ) | ||
| } | ||
| let env = envDict.map { "\($0.key)=\($0.value)" } |
There was a problem hiding this comment.
The environment variable dictionary is converted to an array using .map, which does not guarantee a stable ordering. This could lead to non-deterministic behavior when comparing environment variables or debugging. Consider sorting the output for deterministic results: let env = envDict.map { "\($0.key)=\($0.value)" }.sorted()
| let env = envDict.map { "\($0.key)=\($0.value)" } | |
| let env = envDict.map { "\($0.key)=\($0.value)" }.sorted() |
Depends on a PR for swift-container-plugin.