Skip to content

Commit 547bc08

Browse files
committed
Fix Windows-specific issues: line endings and file locking
Two key Windows compatibility fixes: 1. Normalize line endings: Windows Swift outputs \r\n but tests expect \n. Added normalizeLineEndings() function to convert all output to Unix-style line endings for cross-platform consistency. 2. Fix file locking errors: On Windows, atomically:true when writing files can cause Win32Error code 32 (file in use). Now we skip atomic writes on Windows by passing atomically:!isWindows to String.write(). This affects both: - ExecutionContext.swift (executeViaInterpreter and executeViaCompilation) - SourceLocationTests.swift (runSwiftScript helper) These changes allow tests to pass on Windows while maintaining existing behavior on Unix platforms.
1 parent ce5ef5b commit 547bc08

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Sources/gyb-swift/ExecutionContext.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ struct CodeGenerator {
135135
}
136136

137137
do {
138-
try swiftCode.write(to: temp, atomically: true, encoding: .utf8)
138+
// On Windows, atomically: true can cause file locking issues
139+
try swiftCode.write(to: temp, atomically: !isWindows, encoding: .utf8)
139140
} catch {
140141
throw Failure("Failed to write temporary Swift file to '\(temp.path)'", error)
141142
}
@@ -146,7 +147,7 @@ struct CodeGenerator {
146147
throw GYBError.executionFailed(filename: filename, errorOutput: result.stderr)
147148
}
148149

149-
return result.stdout
150+
return normalizeLineEndings(result.stdout)
150151
}
151152

152153
/// Executes `swiftCode` by compiling and running the executable.
@@ -155,7 +156,8 @@ struct CodeGenerator {
155156
defer { cleanupTempFiles(tempFiles) }
156157

157158
do {
158-
try swiftCode.write(to: tempFiles.source, atomically: true, encoding: .utf8)
159+
// On Windows, atomically: true can cause file locking issues
160+
try swiftCode.write(to: tempFiles.source, atomically: !isWindows, encoding: .utf8)
159161
} catch {
160162
throw Failure("Failed to write temporary Swift file to '\(tempFiles.source.path)'", error)
161163
}
@@ -221,7 +223,7 @@ struct CodeGenerator {
221223
throw GYBError.executionFailed(filename: filename, errorOutput: result.stderr)
222224
}
223225

224-
return result.stdout
226+
return normalizeLineEndings(result.stdout)
225227
}
226228

227229
/// Returns the start position of `nodes`'s first element.
@@ -306,3 +308,10 @@ private func formatSourceLocation(_ template: String, filename: String, line: In
306308
.replacingOccurrences(of: #"\(file)"#, with: filename)
307309
.replacingOccurrences(of: #"\(line)"#, with: "\(line)")
308310
}
311+
312+
/// Normalizes line endings to Unix style (`\n`) for cross-platform consistency.
313+
///
314+
/// On Windows, Swift's print() outputs `\r\n` line endings, but our tests expect `\n`.
315+
private func normalizeLineEndings(_ text: String) -> String {
316+
return text.replacingOccurrences(of: "\r\n", with: "\n")
317+
}

Tests/gyb-swiftTests/SourceLocationTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ private func runSwiftScript(
123123
#endif
124124

125125
do {
126-
try swiftCode.write(toFile: tempFile, atomically: true, encoding: .utf8)
127126
defer { try? FileManager.default.removeItem(atPath: tempFile) }
128127

128+
// On Windows, atomically: true can cause file locking issues
129+
try swiftCode.write(toFile: tempFile, atomically: !isWindows, encoding: .utf8)
130+
129131
let result = try runProcess("swift", arguments: [tempFile])
130132

131133
if result.exitStatus != 0 {

0 commit comments

Comments
 (0)