Skip to content

Add --check flag to smithy format#3094

Draft
yasmewad wants to merge 2 commits into
smithy-lang:mainfrom
yasmewad:yasmewad/format-check-mode
Draft

Add --check flag to smithy format#3094
yasmewad wants to merge 2 commits into
smithy-lang:mainfrom
yasmewad:yasmewad/format-check-mode

Conversation

@yasmewad
Copy link
Copy Markdown
Contributor

@yasmewad yasmewad commented May 4, 2026

The smithy format command previously only supported in-place formatting, with no way to verify formatting without modifying files. This made CI enforcement impossible without risking unintended writes in read-only pipelines.

Add a --check mode that compares each file's current content against its formatted output and exits with code 2 if any files differ. Refactor the file-walking logic into a shared walkSmithyFiles helper used by both formatFile and the new checkFile, fixing a Files.find stream leak with try-with-resources in the process.

Fixes #2317


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

The `smithy format` command previously only supported in-place
formatting, with no way to verify formatting without modifying files.
This made CI enforcement impossible without risking unintended writes
in read-only pipelines.

Add a `--check` mode that compares each file's current content against
its formatted output and exits with code 2 if any files differ. Refactor
the file-walking logic into a shared `walkSmithyFiles` helper used by
both `formatFile` and the new `checkFile`, fixing a `Files.find` stream
leak with try-with-resources in the process.
@yasmewad yasmewad force-pushed the yasmewad/format-check-mode branch from b12d5a5 to bb7f32c Compare May 4, 2026 16:41
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@Test
public void checkUnformattedFile() {
IntegUtils.run("bad-formatting", ListUtils.of("format", "--check", "model"), result -> {
assertThat(result.getExitCode(), equalTo(2));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
assertThat(result.getExitCode(), equalTo(2));
assertThat(result.getExitCode(), equalTo(1));

The CliError thrown for this will get turned into an exit 1

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.

Add option to fail the build on formatting violations

2 participants