Keep file content when the file is virtual (#6519)

Virtual files are not stored on disk and can thus also not be read
from there again. Their content needs to stay in memory and be modified
right there.
This commit is contained in:
Danny Mösch
2026-02-28 09:39:37 +01:00
committed by GitHub
parent b1ebd94410
commit b0cd950a35
3 changed files with 9 additions and 3 deletions
+4
View File
@@ -12,6 +12,10 @@
### Enhancements
* Print fixed code read from stdin to stdout.
[SimplyDanny](https://github.com/SimplyDanny)
[#6501](https://github.com/realm/SwiftLint/issues/6501)
* Add `ignored_literal_argument_functions` option to the `force_unwrapping` rule
to skip violations for configurable function calls when all arguments are
literal values (e.g. `URL(string: "https://example.com")!`). Defaults
@@ -182,7 +182,9 @@ extension SwiftLintFile {
/// Invalidates all cached data for this file.
public func invalidateCache() {
file.clearCaches()
if !isVirtual {
file.clearCaches()
}
responseCache.invalidate(self)
assertHandlerCache.invalidate(self)
structureDictionaryCache.invalidate(self)
@@ -134,6 +134,7 @@ extension SwiftLintFile {
guard string.isNotEmpty else {
return
}
defer { invalidateCache() }
file.contents += string
if isVirtual {
return
@@ -147,13 +148,13 @@ extension SwiftLintFile {
_ = fileHandle.seekToEndOfFile()
fileHandle.write(stringData)
fileHandle.closeFile()
invalidateCache()
}
public func write<S: StringProtocol>(_ string: S) {
guard string != contents else {
return
}
defer { invalidateCache() }
file.contents = String(string)
if isVirtual {
return
@@ -169,7 +170,6 @@ extension SwiftLintFile {
} catch {
queuedFatalError("can't write file to \(path)")
}
invalidateCache()
}
public func ruleEnabled(violatingRanges: [NSRange], for rule: some Rule) -> [NSRange] {