Add Wasm builds to pull_request.yml#1078
Conversation
|
cc @kkebo |
|
To build swift-format for WASI, we need some fixes. The main blockers are:
|
Certainly happy to dust off #675 again if it makes this requirement go away. |
|
Yes, removing the Dispatch dependency would be greatly appreciated! 🙏 |
Is this required for in-place formatting? I wonder if a better near-term option is just to disable directly in-place formatting in files, and IIUC in-place formatting in memory would still be possible when |
Yes, it is.
That's one option. Or, how about using non-atomic file writing for WASI?
What does "in-place formatting in memory" mean? IIUC swift-format with in-place disabled will only print the formatting results. |
IMO that's a significant change in behavior with potential for unexpected data loss. I'd prefer that to be a new CLI flag, like
I'm referring to the |
Only the |
It's cherry-picked now to |
Explicit `void` is required for functions taking no arguments when targeting Wasm.
|
Now with |
Now, Toolchains for which this change is available:
|
|
So, the current minimal required change (without tests) is like: diff --git a/Sources/_SwiftFormatInstructionCounter/include/InstructionsExecuted.h b/Sources/_SwiftFormatInstructionCounter/include/InstructionsExecuted.h
index af9d9a4..164f441 100644
--- a/Sources/_SwiftFormatInstructionCounter/include/InstructionsExecuted.h
+++ b/Sources/_SwiftFormatInstructionCounter/include/InstructionsExecuted.h
@@ -14,4 +14,4 @@
/// On macOS returns the number of instructions the process has executed since
/// it was launched, on all other platforms returns 0.
-uint64_t getInstructionsExecuted();
+uint64_t getInstructionsExecuted(void);
diff --git a/Sources/swift-format/Frontend/FormatFrontend.swift b/Sources/swift-format/Frontend/FormatFrontend.swift
index 0e8fca9..d5e1675 100644
--- a/Sources/swift-format/Frontend/FormatFrontend.swift
+++ b/Sources/swift-format/Frontend/FormatFrontend.swift
@@ -63,7 +63,11 @@ class FormatFrontend: Frontend {
if buffer != source {
let bufferData = buffer.data(using: .utf8)! // Conversion to UTF-8 cannot fail
+ #if !os(WASI)
try bufferData.write(to: url, options: .atomic)
+ #else
+ // `.atomic` is not yet supported on WASI.
+ try bufferData.write(to: url)
+ #endif
}
} else {
try formatter.format(
diff --git a/Sources/swift-format/Frontend/Frontend.swift b/Sources/swift-format/Frontend/Frontend.swift
index aaa24ad..587b2a8 100644
--- a/Sources/swift-format/Frontend/Frontend.swift
+++ b/Sources/swift-format/Frontend/Frontend.swift
@@ -287,12 +287,16 @@ class Frontend {
)
if parallel {
+ #if !os(WASI)
let filesToProcess =
FileIterator(urls: urls, followSymlinks: lintFormatOptions.followSymlinks)
.compactMap(openAndPrepareFile)
DispatchQueue.concurrentPerform(iterations: filesToProcess.count) { index in
processFile(filesToProcess[index])
}
+ #else
+ fatalError("`--parallel` is not yet implemented on WASI.")
+ #endif
} else {
FileIterator(urls: urls, followSymlinks: lintFormatOptions.followSymlinks)
.lazy
diff --git a/Sources/swift-format/Utilities/StderrDiagnosticPrinter.swift b/Sources/swift-format/Utilities/StderrDiagnosticPrinter.swift
index eb4f79c..6c97f10 100644
--- a/Sources/swift-format/Utilities/StderrDiagnosticPrinter.swift
+++ b/Sources/swift-format/Utilities/StderrDiagnosticPrinter.swift
@@ -10,9 +10,12 @@
//
//===----------------------------------------------------------------------===//
-import Dispatch
import Foundation
+#if !os(WASI)
+import Dispatch
+#endif
+
/// Manages printing of diagnostics to standard error.
final class StderrDiagnosticPrinter {
/// Determines how colors are used in printed diagnostics.
@@ -38,8 +41,10 @@ final class StderrDiagnosticPrinter {
case reset = "0"
}
+ #if !os(WASI)
/// The queue used to synchronize printing uninterrupted diagnostic messages.
private let printQueue = DispatchQueue(label: "com.apple.swift-format.StderrDiagnosticPrinter")
+ #endif
/// Indicates whether colors should be used when printing diagnostics.
private let useColors: Bool
@@ -58,7 +63,18 @@ final class StderrDiagnosticPrinter {
/// Prints a diagnostic to standard error.
func printDiagnostic(_ diagnostic: Diagnostic) {
+ #if !os(WASI)
printQueue.sync {
+ _printDiagnostic(diagnostic)
+ }
+ #else
+ // This is safe because swift-format can't process files in parallel on WASI.
+ _printDiagnostic(diagnostic)
+ #endif
+ }
+
+ /// Prints a diagnostic to standard error.
+ private func _printDiagnostic(_ diagnostic: Diagnostic) {
let stderr = FileHandleTextOutputStream(FileHandle.standardError)
stderr.write("\(ansiSGR(.reset))\(description(of: diagnostic.location)): ")
@@ -74,7 +90,6 @@ final class StderrDiagnosticPrinter {
}
stderr.write("\(ansiSGR(.reset))\(ansiSGR(.bold))\(diagnostic.message)\(ansiSGR(.reset))\n")
}
- }
/// Returns a string representation of the given diagnostic location, or a fallback string if the
/// location was not known. |
This ensures that builds of
swift-formatfor WASI don't regress.