Skip to content

Commit 3f38707

Browse files
authored
Fix fmt sub-command exit code to mirror that of the underlying scalafmt (#2463)
1 parent 9d47089 commit 3f38707

File tree

3 files changed

+12
-9
lines changed
  • modules
  • website/docs/commands

3 files changed

+12
-9
lines changed

modules/cli/src/main/scala/scala/cli/commands/fmt/Fmt.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ object Fmt extends ScalaCommand[FmtOptions] {
115115
sourceFiles.map(_.toString) ++
116116
options.scalafmtCliOptions ++
117117
Seq("--config", scalaFmtConfPath.toString)
118-
Runner.maybeExec(
118+
val process = Runner.maybeExec(
119119
"scalafmt",
120120
command,
121121
logger,
122122
cwd = Some(workspace)
123-
).waitFor()
123+
)
124+
sys.exit(process.waitFor())
124125
}
125126
}
126127
}

modules/integration/src/test/scala/scala/cli/integration/FmtTests.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ class FmtTests extends ScalaCliSuite {
8585

8686
test("with --check") {
8787
simpleInputs.fromRoot { root =>
88-
val out = os.proc(TestUtil.cli, "fmt", "--check").call(cwd = root, check = false).out.text()
88+
val res = os.proc(TestUtil.cli, "fmt", "--check").call(cwd = root, check = false)
89+
expect(res.exitCode == 1)
90+
val out = res.out.text()
8991
val updatedContent = noCrLf(os.read(root / "Foo.scala"))
9092
expect(updatedContent == noCrLf(simpleInputsUnformattedContent))
9193
expect(noCrLf(out) == "error: --test failed\n")

website/docs/commands/fmt.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ import {ChainedSnippets} from "../../src/components/MarkdownComponents.js";
88
Scala CLI supports formatting your code using [Scalafmt](https://scalameta.org/scalafmt/):
99

1010
```bash
11-
scala-cli fmt
11+
scala-cli fmt .
1212
```
1313

1414
Under the hood, Scala CLI downloads and runs Scalafmt on your code.
1515

1616
If you’re setting up a continuous integration (CI) server, Scala CLI also has you covered.
1717
You can check formatting correctness using a `--check` flag:
1818

19-
```bash
20-
scala-cli fmt --check
19+
```bash fail
20+
scala-cli fmt --check .
2121
```
2222

2323
### Scalafmt version and dialect
2424

2525
Scala CLI `fmt` command supports passing the `scalafmt` **version** and **dialect** directly from the command line, using the `--scalafmt-dialect` and `--scalafmt-version` options respectively:
2626
```
27-
scala-cli fmt --scalafmt-dialect scala3 --scalafmt-version 3.5.8
27+
scala-cli fmt . --scalafmt-dialect scala3 --scalafmt-version 3.5.8
2828
```
2929
You can skip passing either of those, which will make Scala CLI infer a default value:
3030
- If a `.scalafmt.conf` file is present in the workspace and it has the field defined, the value will be read from there, unless explicitly specified with Scala CLI options.
@@ -40,7 +40,7 @@ runner.dialect = scala212
4040
```
4141

4242
```bash
43-
scala-cli fmt --scalafmt-dialect scala213
43+
scala-cli fmt --scalafmt-dialect scala213 .
4444
```
4545

4646
For the setup above, `fmt` will use:
@@ -54,7 +54,7 @@ version = "2.7.5"
5454
```
5555

5656
```bash
57-
scala-cli fmt --scalafmt-version 3.5.8
57+
scala-cli fmt --scalafmt-version 3.5.8 .
5858
```
5959

6060
For the setup above, `fmt` will use:

0 commit comments

Comments
 (0)