Skip to content

Keep the file mode when formatting in place - #1286

Merged
rintaro merged 2 commits into
swiftlang:mainfrom
lenamonj:in-place-keep-file-mode
Sep 9, 2026
Merged

rintaro merged 2 commits into
swiftlang:mainfrom
lenamonj:in-place-keep-file-mode

Conversation

@lenamonj

@lenamonj lenamonj commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

swift-format format --in-place writes with Data.write(to:options:.atomic), which replaces the file rather than rewriting it, so the result is a new file created at the process umask. A source file the user had at 0600 came back 0644, and a 0444 one lost its read-only bit.

The mode is read before the write and restored after it.

On main, formatting a 0600, 0444 or 0755 file in place leaves it at 0644; with this change each keeps its mode.

@rintaro

rintaro commented Sep 9, 2026

Copy link
Copy Markdown
Member

The implementation looks fine to me. But could you write some test cases?

The test target depends on the SwiftFormat library and cannot reach the executable target, so the read, write and restore moved into an @_spi(Internal) Data extension that FormatFrontend calls. The tests cover 0600, 0444 and 0755 and a file that did not exist; without the restore all three come back 0644.
@lenamonj

lenamonj commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Added tests. They cover 0600, 0444 and 0755 files keeping their mode after the write, with the content replaced, and a file that did not exist; without the restore all three come back 0644. Because SwiftFormatTests depends on the SwiftFormat library and cannot reach the executable target, the read, write and restore moved into an @_spi(Internal) Data extension in the library that FormatFrontend calls. Compiled out on Windows.

@rintaro
rintaro merged commit fcc5a79 into swiftlang:main Sep 9, 2026
40 of 41 checks passed
@rintaro

rintaro commented Sep 9, 2026

Copy link
Copy Markdown
Member

Thank you Jeff!

@lenamonj
lenamonj deleted the in-place-keep-file-mode branch September 9, 2026 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants