wip: hot reloading

Discriminate with module name

Fully working

Changes

Fix linux

changes

allow customizing xload path

Lint
This commit is contained in:
Kabir Oberai
2026-05-18 04:39:14 -04:00
Unverified
parent 4815e855b9
commit 3bebb76013
10 changed files with 1628 additions and 52 deletions
+10 -1
View File
@@ -1,5 +1,5 @@
{
"originHash" : "a67130742a321d259d3288347f30ac3f5520a64d98c183a85694b6bca44c4a64",
"originHash" : "9ec3992e7de864bad568faa552cdf4c9fff40eebb41c0c05faf964bc172df5d4",
"pins" : [
{
"identity" : "aexml",
@@ -379,6 +379,15 @@
"version" : "1.6.4"
}
},
{
"identity" : "swift-tools-protocols",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-tools-protocols",
"state" : {
"revision" : "d81024558da3bcd53748c4cfdaba10a49242442e",
"version" : "0.0.10"
}
},
{
"identity" : "swiftcli",
"kind" : "remoteSourceControl",
+3 -1
View File
@@ -23,7 +23,7 @@ let package = Package(
name: "xtool",
platforms: [
.iOS(.v16),
.macOS(.v13),
.macOS(.v14),
],
products: [
.library(
@@ -56,6 +56,7 @@ let package = Package(
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/swiftlang/swift-subprocess", .upToNextMinor(from: "0.4.0")),
.package(url: "https://github.com/swiftlang/swift-tools-protocols", exact: "0.0.10"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.23.0"),
.package(url: "https://github.com/swift-server/swift-openapi-async-http-client", from: "1.0.0"),
@@ -159,6 +160,7 @@ let package = Package(
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "Version", package: "Version"),
.product(name: "libunxip", package: "unxip"),
.product(name: "LanguageServerProtocolTransport", package: "swift-tools-protocols"),
],
cSettings: cSettings
),
+4
View File
@@ -0,0 +1,4 @@
#include "inotify_shim.h"
#include "stdout_shim.h"
#include "version.h"
#include "mobileprovision.h"
+21
View File
@@ -0,0 +1,21 @@
#ifndef CINOTIFY_SHIMS_H
#define CINOTIFY_SHIMS_H
#ifdef __linux__
#include <stdio.h>
#include <stdint.h>
#include <sys/inotify.h>
static inline const char *cin_event_name(const struct inotify_event *event) {
if (event->len)
return event->name;
else
return NULL;
}
static const uint32_t cin_all_events = IN_ALL_EVENTS;
#endif
#endif /* CINOTIFY_SHIMS_H */
+70 -49
View File
@@ -23,10 +23,30 @@ struct PackOperation {
var triple: String?
var buildOptions = BuildOptions(configuration: .debug)
var extraOptions: [String] = []
var watchMode = false
var xcode = false
func buildSettings() async throws -> BuildSettings {
try await BuildSettings(
configuration: buildOptions.configuration,
triple: triple ?? Self.defaultTriple,
options: extraOptions + (
watchMode
? [
"-Xlinker", "-interposable",
// https://kyleye.top/posts/debugreplaceableview-multiple-type-erasers/
"-Xswiftc", "-enable-experimental-feature", "-Xswiftc", "OpaqueTypeErasure",
// https://www.guardsquare.com/blog/behind-swiftui-previews
"-Xswiftc", "-Xfrontend", "-Xswiftc", "-enable-private-imports",
]
: []
)
)
}
@discardableResult
func run() async throws -> URL {
func run() async throws -> (Plan, URL) {
print("Planning...")
let schema: PackSchema
@@ -41,11 +61,7 @@ struct PackOperation {
""")
}
let buildSettings = try await BuildSettings(
configuration: buildOptions.configuration,
triple: triple ?? Self.defaultTriple,
options: []
)
let buildSettings = try await buildSettings()
let planner = Planner(
buildSettings: buildSettings,
@@ -55,7 +71,8 @@ struct PackOperation {
#if os(macOS)
if xcode {
return try await XcodePacker(plan: plan).createProject()
let url = try await XcodePacker(plan: plan).createProject()
return (plan, url)
}
#endif
@@ -68,28 +85,26 @@ struct PackOperation {
let productsWithEntitlements = plan
.allProducts
.compactMap { p in p.entitlementsPath.map { (p, $0) } }
if !productsWithEntitlements.isEmpty {
let mapping = try await withThrowingTaskGroup(of: (URL, Entitlements).self) { group in
for (product, path) in productsWithEntitlements {
group.addTask {
let data = try await Data(reading: URL(fileURLWithPath: path))
let decoder = PropertyListDecoder()
let entitlements = try decoder.decode(Entitlements.self, from: data)
return (product.directory(inApp: bundle), entitlements)
}
let mapping = try await withThrowingTaskGroup(of: (URL, Entitlements?).self) { group in
for (product, path) in productsWithEntitlements {
group.addTask {
let data = try await Data(reading: URL(fileURLWithPath: path))
let decoder = PropertyListDecoder()
let entitlements = try decoder.decode(Entitlements.self, from: data)
return (product.directory(inApp: bundle), entitlements)
}
return try await group.reduce(into: [:]) { $0[$1.0] = $1.1 }
}
print("Applying entitlements...")
try await Signer.first().sign(
app: bundle,
identity: .adhoc,
entitlementMapping: mapping,
progress: { _ in }
)
return try await group.reduce(into: [:]) { $0[$1.0] = $1.1 }
}
print("Applying entitlements...")
try await Signer.first().sign(
app: bundle,
identity: .adhoc,
entitlementMapping: mapping,
progress: { _ in }
)
return bundle
return (plan, bundle)
}
}
@@ -144,7 +159,7 @@ struct DevBuildCommand: AsyncParsableCommand {
signingAuthToken = nil
}
let url = try await PackOperation(
let (_, url) = try await PackOperation(
triple: triple,
buildOptions: packOptions
).run()
@@ -209,6 +224,13 @@ struct DevRunCommand: AsyncParsableCommand {
help: "Target the iOS Simulator"
) var simulator = false
@Flag(
name: .shortAndLong,
help: "Hot reload files on change. Requires simulator."
) var watch = false
@Option var xloadPath: String?
var triple: String? {
if simulator {
#if arch(arm64)
@@ -222,17 +244,37 @@ struct DevRunCommand: AsyncParsableCommand {
return nil
}
#else
var watch: Bool { false }
var triple: String? { nil }
#endif
@OptionGroup var connectionOptions: ConnectionOptions
func validate() throws {
#if os(macOS)
if watch && !simulator {
throw ValidationError("--watch requires --simulator")
}
#endif
}
func run() async throws {
let output = try await PackOperation(triple: triple, buildOptions: packOptions).run()
let operation = PackOperation(
triple: triple,
buildOptions: packOptions,
watchMode: watch,
)
let (plan, output) = try await operation.run()
#if os(macOS)
if simulator {
try await SimInstallOperation(path: output).run()
try await SimInstallOperation(
operation: operation,
plan: plan,
path: output,
watch: watch,
xLoadLibrary: xloadPath,
).run()
return
}
#endif
@@ -281,24 +323,3 @@ struct DevCommand: AsyncParsableCommand {
}
extension BuildConfiguration: ExpressibleByArgument {}
#if os(macOS)
import Subprocess
struct SimInstallOperation {
var path: URL
// TODO: allow customizing this
var simulator = "booted"
func run() async throws {
try await Subprocess.run(
.path("/usr/bin/xcrun"),
arguments: ["simctl", "install", simulator, path.path],
output: .discarded
)
.checkSuccess()
print("Installed to simulator")
}
}
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,140 @@
#if os(macOS)
import Subprocess
import PackLib
import Foundation
import XUtils
import Dependencies
import XKit
struct SimInstallOperation {
var operation: PackOperation
var plan: Plan
var path: URL
var watch = false
var xLoadLibrary: String?
// TODO: allow customizing this
var simulator = "booted"
func run() async throws {
try await Subprocess.run(
.path("/usr/bin/xcrun"),
arguments: ["simctl", "install", simulator, path.path],
output: .discarded
)
.checkSuccess()
print("Installed to simulator")
let tmp = URL(filePath: FileManager.default.currentDirectoryPath)
.appending(path: "xtool/.xload")
try? FileManager.default.removeItem(at: tmp)
defer { try? FileManager.default.removeItem(at: tmp) }
let watchTask: Task<Void, Error>?
let envVars: [Environment.Key: String?]
if watch {
let xLoadPath = if let xLoadLibrary {
URL(filePath: xLoadLibrary)
} else {
try await getXLoad(platform: "iphonesimulator")
}
try FileManager.default.createDirectory(at: tmp, withIntermediateDirectories: true)
guard let temporaryDirectoryPath = FilePath(tmp) else {
throw Console.Error("Bad temporaryDirectory path")
}
let buildDirectory = temporaryDirectoryPath.appending("build")
try FileManager.default.createDirectory(at: URL(filePath: buildDirectory)!, withIntermediateDirectories: true)
let outDirectory = temporaryDirectoryPath.appending("out")
try FileManager.default.createDirectory(at: URL(filePath: outDirectory)!, withIntermediateDirectories: true)
watchTask = Task {
do {
try await watch(buildDirectory: buildDirectory, outDirectory: outDirectory)
} catch {
print("Error: watch task failed: \(error)")
}
}
envVars = [
"SIMCTL_CHILD_DYLD_INSERT_LIBRARIES": xLoadPath.path,
"SIMCTL_CHILD_XLOAD_WATCH_DIR": outDirectory.string,
"SIMCTL_CHILD_XLOAD_INTERCEPT": "1",
]
} else {
watchTask = nil
envVars = [:]
}
try await Subprocess.run(
.path("/usr/bin/xcrun"),
arguments: [
"simctl", "launch",
"--console-pty",
simulator, plan.app.bundleID,
],
environment: .inherit.updating(envVars),
platformOptions: .withGracefulShutDown,
output: .standardOutput,
error: .standardError,
)
.checkSuccess()
watchTask?.cancel()
try await watchTask?.value
}
private func watch(
buildDirectory: FilePath,
outDirectory: FilePath,
) async throws {
let settings = try await operation.buildSettings()
let workspace = try await SwiftWorkspace(
buildDirectory: buildDirectory,
outDirectory: outDirectory,
buildSettings: settings,
)
@Dependency(\.fileSystemMonitor) var fileSystemMonitor
let events = try await fileSystemMonitor.watch(FilePath(FileManager.default.currentDirectoryPath))
for await event in events {
do {
try await workspace.fileDidChange(event.file)
} catch {
print("Reload failed: \(error)")
}
}
}
private func getXLoad(platform: String) async throws -> URL {
let version = "0.3.0"
let remote = URL(string: "https://github.com/xtool-org/xload/releases/download/v\(version)/libXLoad.\(platform).dylib")!
@Dependency(\.httpClient) var httpClient
@Dependency(\.persistentDirectory) var persistentDirectory
let platformDir = persistentDirectory.appending(path: "xload/\(platform)")
let versionDir = platformDir.appending(path: version)
let local = versionDir.appending(path: "libXLoad.dylib")
if local.exists { return local }
print("Downloading xload...")
try? FileManager.default.removeItem(at: platformDir)
try FileManager.default.createDirectory(at: versionDir, withIntermediateDirectories: true)
let data = try await httpClient.makeRequest(HTTPRequest(url: remote))
guard data.response.status.kind == .successful else {
throw StringError("Failed to download xload: got HTTP \(data.response.status)")
}
try data.body.write(to: local)
return local
}
}
#endif
+315
View File
@@ -0,0 +1,315 @@
import Foundation
import XUtils
import PackLib
import BuildServerProtocol
import LanguageServerProtocol
import LanguageServerProtocolTransport
import Subprocess
import Dependencies
actor SwiftWorkspace {
private let connection: JSONRPCConnection
private let buildDirectory: FilePath
private let outDirectory: FilePath
private var counter = 0
struct FileInfo {
let file: FilePath
let target: BuildTarget
let sources: SourcesItem
let source: SourceItem
init?(target: BuildTarget, sources: SourcesItem, source: SourceItem) {
guard let path = FilePath(source.uri.arbitrarySchemeURL) else { return nil }
self.file = path
self.target = target
self.sources = sources
self.source = source
}
}
private var filesToWatch: [FilePath: [FileInfo]] = [:]
private var sources: [(BuildTarget, SourcesItem)] {
didSet { updateFilesToWatch() }
}
private var cachedCommands: [FilePath: FrontendCommand] = [:]
struct FrontendCommand {
let commandLine: CommandInvocation
let output: FilePath
let module: String
}
init(
buildDirectory: FilePath,
outDirectory: FilePath,
buildSettings: BuildSettings,
) async throws {
guard let swiftURL = URL(filePath: try await BuildSettings.swiftURL()) else {
throw Console.Error("Swift URL invalid")
}
self.buildDirectory = buildDirectory
self.outDirectory = outDirectory
let handler = StreamingMessageHandler()
let (connection, _) = try JSONRPCConnection.start(
executable: swiftURL,
arguments: buildSettings.buildServerArguments,
name: "xtool",
protocol: .bspProtocol,
stderrLoggingCategory: "xtool-error",
client: handler,
terminationHandler: { print("Terminated: \($0)") }
)
self.connection = connection
_ = try await connection.send(InitializeBuildRequest(
displayName: "xtool",
version: "1.0.0",
bspVersion: "2.2.0",
rootUri: .init(URL(filePath: FileManager.default.currentDirectoryPath)),
capabilities: BuildClientCapabilities(
languageIds: [.swift, .c, .cpp, .objective_c, .objective_cpp],
)
))
connection.send(OnBuildInitializedNotification())
for await case _ as OnBuildTargetDidChangeNotification in handler.notifications { break }
let result = try await connection.send(WorkspaceBuildTargetsRequest())
let targets = Dictionary(uniqueKeysWithValues: result.targets.map { ($0.id, $0) })
let targetInfos = try await connection.send(BuildTargetSourcesRequest(
targets: result.targets.map(\.id)
))
sources = targetInfos.items.compactMap {
guard let target = targets[$0.target] else { return nil }
return (target, $0)
}
updateFilesToWatch()
}
deinit {
connection.close()
}
func updateFilesToWatch() {
filesToWatch = Dictionary(
grouping: sources.flatMap { target, sources in
sources.sources.compactMap { source in
// guard !target.id.uri.stringValue.contains("&targetGUID=PACKAGE-TARGET:") else { return nil }
FileInfo(target: target, sources: sources, source: source)
}
},
by: \.file
)
}
func fileDidChange(_ path: FilePath) async throws {
guard filesToWatch[path] != nil else { return }
print("Reloading \(path)")
_ = try await rebuild(path: path)
}
@discardableResult
func rebuild(path: FilePath) async throws -> FilePath {
let command = try await frontendCommand(for: path)
try await Subprocess.run(
.path(FilePath(String(command.commandLine.command))),
arguments: .init(command.commandLine.arguments.map { String($0) }),
output: .standardOutput,
error: .standardError,
)
.checkSuccess()
let sdk = try command.commandLine.value(for: "-sdk")
.orThrow(Console.Error("Could not find -sdk"))
let target = try command.commandLine.value(for: "-target")
.orThrow(Console.Error("Could not find -target"))
counter += 1
let outputFile = outDirectory
.appending("lib\(path.lastComponent!.stem).\(counter).dylib")
// link
try await Subprocess.run(
.path(try await BuildSettings.swiftcURL()),
arguments: [
command.output.string,
"-emit-library",
"-sdk", sdk, "-target", target,
"-Xlinker", "-undefined", "-Xlinker", "dynamic_lookup",
"-o", outputFile.string
],
output: .standardOutput,
error: .standardError,
)
.checkSuccess()
return outputFile
}
func frontendCommand(for path: FilePath) async throws -> FrontendCommand {
if let cached = cachedCommands[path] {
return cached
}
let targets = filesToWatch[path] ?? []
let target: FileInfo
switch targets.count {
case 0:
throw Console.Error("No targets for file")
case 1:
target = targets[0]
default:
print("warning: multiple targets contain this file: \(targets.map(\.target.id.uri))")
target = targets[0]
}
guard let fileURL = URL(filePath: path)?.absoluteURL, let lastComponent = path.lastComponent else {
throw Console.Error("Bad file path \(path.string)")
}
let options = try await connection.send(TextDocumentSourceKitOptionsRequest(
textDocument: .init(target.source.uri),
target: target.target.id,
language: .swift,
))
.orThrow(Console.Error("No options"))
guard let moduleNameIndex = options.compilerArguments.firstIndex(of: "-module-name"),
moduleNameIndex < options.compilerArguments.count - 2 else {
throw Console.Error("Could not determine module name for file at \(path)")
}
let moduleName = options.compilerArguments[moduleNameIndex + 1]
let moduleBuildDirectory = buildDirectory.appending(moduleName)
let moduleBuildDirectoryURL = try URL(filePath: moduleBuildDirectory).orThrow(Console.Error("Bad module name"))
try? FileManager.default.createDirectory(at: moduleBuildDirectoryURL, withIntermediateDirectories: true)
let swiftcOutput = try await Subprocess.run(
.path(try await BuildSettings.swiftcURL()),
arguments: .init(
["-c", "-driver-print-jobs"]
+ options.compilerArguments
+ ["-working-directory", moduleBuildDirectory.string]
),
output: .string(limit: .max),
error: .standardError
).checkSuccess()
let frontendCommands = (swiftcOutput.standardOutput ?? "").split(separator: "\n")
guard let invocation = frontendCommands.lazy.compactMap({ line -> CommandInvocation? in
guard let invocation = CommandInvocation(line) else { return nil }
guard invocation.value(for: "-primary-file") == fileURL.path else { return nil }
return invocation
}).first else { throw Console.Error("Could not find frontend command") }
let command = FrontendCommand(
commandLine: invocation,
output: moduleBuildDirectory.appending("\(lastComponent.stem).o"),
module: moduleName,
)
cachedCommands[path] = command
return command
}
private final class StreamingMessageHandler: MessageHandler {
private let (_notifications, onNotification) = AsyncStream<NotificationType>.makeStream()
var notifications: AsyncStream<NotificationType> { _notifications }
deinit { onNotification.finish() }
func handle(_ notification: some NotificationType) {
onNotification.yield(notification)
}
func handle<Request: RequestType>(
_ request: Request,
id: LanguageServerProtocol.RequestID,
reply: @escaping @Sendable (LanguageServerProtocol.LSPResult<Request.Response>) -> Void
) {
fatalError("Can't handle request \(Request.method)")
}
}
}
struct CommandInvocation {
var command: String
var arguments: [String]
init?(_ string: some StringProtocol) {
guard let all = try? CommandParser.parse(string), !all.isEmpty else { return nil }
command = all[0]
arguments = Array(all.dropFirst())
}
func value(for option: some StringProtocol) -> String? {
guard let optionIndex = arguments.firstIndex(where: { $0 == option }) else { return nil }
guard optionIndex < arguments.count - 2 else { return nil }
return arguments[optionIndex + 1]
}
}
enum CommandParser {
enum Errors: Error, Hashable {
case unclosedQuote
case unpairedEscape
}
static func parse(_ string: some StringProtocol) throws -> [String] {
try sequence(
state: (
current: string[...],
isInsideQuote: false,
)
) { state -> Token? in
while !state.current.isEmpty {
switch state.current.removeFirst() {
case "'":
state.isInsideQuote.toggle()
case "\\" where !state.isInsideQuote:
guard !state.current.isEmpty else { return .error(.unpairedEscape) }
let actual = state.current.removeFirst()
return .value(actual)
case " " where !state.isInsideQuote:
return .sentinelSpace
case let value:
return .value(value)
}
}
guard !state.isInsideQuote else {
state.isInsideQuote = false
return .error(.unclosedQuote)
}
return nil
}
.lazy
.split(separator: .sentinelSpace)
.map {
let characters = try $0.compactMap { token -> Character? in
switch token {
case .sentinelSpace: nil
case .error(let error): throw error
case .value(let value): value
}
}
return String(characters)
}
}
private enum Token: Hashable {
case sentinelSpace
case value(Character)
case error(Errors)
}
}
@@ -0,0 +1,25 @@
import Testing
@testable import XToolSupport
@Test func commandParser() throws {
func sut(_ string: some StringProtocol) throws -> [String] {
try CommandParser.parse(string)
}
#expect(try sut(#""#).isEmpty)
#expect(try sut(#"foo"#) == ["foo"])
#expect(try sut(#"foo "#) == ["foo"])
#expect(try sut(#"foo one two"#) == ["foo", "one", "two"])
#expect(try sut(#"foo 'one two' three"#) == ["foo", "one two", "three"])
#expect(try sut(#"foo 'one!two' three"#) == ["foo", "one!two", "three"])
#expect(try sut(#"foo one\!two three"#) == ["foo", "one!two", "three"])
#expect(try sut(#"foo one\'two three"#) == ["foo", "one'two", "three"])
#expect(try sut(#"foo one'two three'four five"#) == ["foo", "onetwo threefour", "five"])
#expect(throws: CommandParser.Errors.unclosedQuote) {
try sut(#"foo one two' three"#)
}
#expect(throws: CommandParser.Errors.unpairedEscape) {
try sut(#"foo a b \"#)
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ targets:
XToolMac:
type: application
platform: macOS
deploymentTarget: "13.0"
deploymentTarget: "14.0"
sources:
- path: XToolMac
- path: Resources/xtool