Skip to content

Omit help text on runtime errors in xcprojformatter - #31

Open
zachshallbetter wants to merge 2 commits into
apple:mainfrom
zachshallbetter:fix/xcprojformatter-error-output
Open

zachshallbetter wants to merge 2 commits into
apple:mainfrom
zachshallbetter:fix/xcprojformatter-error-output

Conversation

@zachshallbetter

@zachshallbetter zachshallbetter commented Nov 24, 2026 •

Copy link
Copy Markdown

Type of change

  • Bug fix
  • xcprojformatter (CLI) change
  • Test addition

Summary

Separates argument-parsing errors from runtime/decode/I/O errors in xcprojformatter so that decode, malformed-input, and unreadable-file failures print only the error diagnostic to stderr without appending the separator and full --help usage text.

Also replaces try! writes to stderr with try? so broken stderr pipes don't trap, and returns exit status 1 instead of -1 (255) on errors. Kept independent of #2 and #30.

Related issue

Fixes #27

Related (not bundled): #2, #30

Changes

  • Sources/Tool/main.swift:
    • Separated Arguments(commandLine:) error handling (which still prints --help to stderr) from readInputData, prettyPrint, and writeOutput errors (which now print only the diagnostic).
    • Replaced try! when writing to stderr with try? and changed error exit status from -1 to 1.
  • Sources/Tests/FormatterErrorOutputTests.swift: Added focused tests (guarded for macOS || Linux || Windows) covering decode failure (Missing required value for key “name” at “/targets[0]”), malformed JSON, unreadable input, and invalid CLI arguments.

Testing

  • Existing tests pass (swift test) — 35 tests in 7 suites passed (TestPerformance skipped in debug configuration), plus swift build -c release succeeded.
  • New tests added for new functionality (FormatterErrorOutputTests: 4 tests).
  • Tested manually: Verified via FormatterErrorOutputTests against the built xcprojformatter binary.

Checklist

  • I have read CONTRIBUTING.md
  • My change is in scope (see CONTRIBUTING.md)
  • Code follows the project's style guidelines
  • I have reviewed my own code and can explain all changes in detail
  • Documentation updated where applicable

Comment thread Sources/Tool/main.swift Outdated
} catch {
let error = error as NSError
let message = error.localizedDescriptionCombinedWithRecoverySuggestionIfPresent + "\n\n----------------------------\n\n"
try? FileHandle.standardError.write(contentsOf: message.utf8.asData)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could wrap this in a printError() as the code is duplicated on the new 37/38 line as well.

}

private func runFormatter(_ arguments: [String], stdin: Data? = nil) throws -> ProcessResult {
let process = Process()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to take a dependency on swift-subprocess? @jakepetroules

There are a handful of other PRs also exercising this "find build products and run the executable behavior." (#30, #2) Ideally, if possible, all 3 of these new tests don't copy the same code everywhere just to get something specific from stdio or exit statuses.

@zachshallbetter

Copy link
Copy Markdown
Author

Thanks, Declan. I pulled the duplicated write into printError() in c2e4d59. swift test passed with 35 tests. Sharing the executable lookup and process runner across #2, #30, and this PR makes sense to me. I’ll wait for Jake’s take on swift-subprocess before changing the tests.

@zachshallbetter

Copy link
Copy Markdown
Author

Could someone approve the CI run for c2e4d59 when you get a chance? Thanks!

@zachshallbetter

Copy link
Copy Markdown
Author

Thanks for starting CI. The Linux and Windows nightly jobs couldn’t find xcprojformatter or xcprojformatter.exe, so the CLI tests never reached the assertions. Would it make sense to share the executable lookup fix across #2, #30, and #31 while Jake weighs in on swift-subprocess?

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.

[Bug]: xcprojformatter prints redundant/non-helpful information after a decode failure

2 participants