Repository: intellij-rust/intellij-rust
Issue: error: Unrecognized option: 'message-format'

adding `--message-format=json-diagnostic-rendered-ansi` to the end of the command may cause "Unrecorized option: 'message-format'" when there's more command line argument in between, for example this will fail:
```
cargo clippy --fix --allow-dirty --allow-staged -Z unstable-options -- -W clippy::all -W clippy::pedantic -W clippy::nursery --message-format=json-diagnostic-rendered-ansi
```
I suggest adding arg to the beginning of the cargo command, this works:
```
cargo clippy --message-format=json-diagnostic-rendered-ansi --fix --allow-dirty --allow-staged -Z unstable-options -- -W clippy::all -W clippy::pedantic -W clippy::nursery
```

Ref:
https://github.com/intellij-rust/intellij-rust/blob/3ee981094ed16b4958e1eb36539e8b05de63da0b/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildManager.kt#L293

Changed files:
src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildManager.kt, src/test/kotlin/org/rustSlowTests/ide/actions/RsBuildActionTest.kt, src/main/kotlin/org/rust/cargo/runconfig/Utils.kt, src/test/kotlin/org/rust/cargo/runconfig/CargoTestRunStateTest.kt

diff --git a/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt b/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
index bd9c2065c..70a72840b 100644
--- a/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
+++ b/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
@@ -112,7 +112,7 @@ fun addFormatJsonOption(additionalArguments: MutableList<String>, formatOption:
     } else if (indexArgWithValue != -1) {
         additionalArguments[indexArgWithValue] = formatJsonOption
     } else {
-        additionalArguments.add(formatJsonOption)
+        additionalArguments.add(0, formatJsonOption)
     }
 }
 
diff --git a/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildManager.kt b/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildManager.kt
index 52196e607..5ee1dbb7c 100644
--- a/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildManager.kt
+++ b/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildManager.kt
@@ -90,7 +90,7 @@ object CargoBuildManager {
         ServiceManager.getService(project, BuildContentManager::class.java)
 
         if (isUnitTestMode) {
-            lastBuildCommandLine = state.commandLine
+            lastBuildCommandLine = state.prepareCommandLine()
         }
 
         return execute(CargoBuildContext(
diff --git a/src/test/kotlin/org/rust/cargo/runconfig/CargoTestRunStateTest.kt b/src/test/kotlin/org/rust/cargo/runconfig/CargoTestRunStateTest.kt
index c1c50036e..29f7a82f3 100644
--- a/src/test/kotlin/org/rust/cargo/runconfig/CargoTestRunStateTest.kt
+++ b/src/test/kotlin/org/rust/cargo/runconfig/CargoTestRunStateTest.kt
@@ -55,12 +55,12 @@ class CargoTestRunStatePatchArgsTest(
         @Parameterized.Parameters(name = "{index}: {0}")
         @JvmStatic
         fun data(): Collection<Array<String>> = listOf(
-            arrayOf("", "--no-fail-fast -- -Z unstable-options --format=json"),
-            arrayOf("foo", "foo --no-fail-fast -- -Z unstable-options --format=json"),
-            arrayOf("foo bar", "foo bar --no-fail-fast -- -Z unstable-options --format=json"),
-            arrayOf("--", "--no-fail-fast -- -Z unstable-options --format=json"),
+            arrayOf("", "--no-fail-fast -- --format=json -Z unstable-options"),
+            arrayOf("foo", "foo --no-fail-fast -- --format=json -Z unstable-options"),
+            arrayOf("foo bar", "foo bar --no-fail-fast -- --format=json -Z unstable-options"),
+            arrayOf("--", "--no-fail-fast -- --format=json -Z unstable-options"),
 
-            arrayOf("-- -Z unstable-options", "--no-fail-fast -- -Z unstable-options --format=json"),
+            arrayOf("-- -Z unstable-options", "--no-fail-fast -- --format=json -Z unstable-options"),
             arrayOf("-- --format=json", "--no-fail-fast -- --format=json -Z unstable-options"),
             arrayOf("-- --format json", "--no-fail-fast -- --format json -Z unstable-options"),
             arrayOf("-- --format pretty", "--no-fail-fast -- --format json -Z unstable-options"),
diff --git a/src/test/kotlin/org/rustSlowTests/ide/actions/RsBuildActionTest.kt b/src/test/kotlin/org/rustSlowTests/ide/actions/RsBuildActionTest.kt
index 4cb6c572f..dd7aa8de1 100644
--- a/src/test/kotlin/org/rustSlowTests/ide/actions/RsBuildActionTest.kt
+++ b/src/test/kotlin/org/rustSlowTests/ide/actions/RsBuildActionTest.kt
@@ -9,6 +9,7 @@ package org.rustSlowTests.ide.actions
 
 import com.intellij.build.events.impl.SuccessResultImpl
 import com.intellij.execution.RunManager
+import com.intellij.execution.configuration.EnvironmentVariablesData
 import com.intellij.execution.impl.RunManagerImpl
 import com.intellij.openapi.actionSystem.ActionManager
 import com.intellij.testFramework.TestDataProvider
@@ -75,7 +76,12 @@ class RsBuildActionTest : CargoBuildTest() {
         mockBuildProgressListener!!.waitFinished()
 
         val actualCommandLine = lastBuildCommandLine!!
-        val expectedCommandLine = CargoCommandLine("build", actualCommandLine.workingDirectory, listOf("--package", "first", "--bin", "first"))
+        val expectedCommandLine = CargoCommandLine(
+            "build",
+            actualCommandLine.workingDirectory,
+            listOf("--message-format=json-diagnostic-rendered-ansi", "--package", "first", "--bin", "first"),
+            environmentVariables = BUILD_VARIABLES
+        )
         assertEquals(expectedCommandLine, actualCommandLine)
 
         checkEvents(
@@ -147,7 +153,12 @@ class RsBuildActionTest : CargoBuildTest() {
         mockBuildProgressListener!!.waitFinished()
 
         val actualCommandLine = lastBuildCommandLine!!
-        val expectedCommandLine = CargoCommandLine("build", actualCommandLine.workingDirectory, listOf("--package", "second", "--bin", "second"))
+        val expectedCommandLine = CargoCommandLine(
+            "build",
+            actualCommandLine.workingDirectory,
+            listOf("--message-format=json-diagnostic-rendered-ansi", "--package", "second", "--bin", "second"),
+            environmentVariables = BUILD_VARIABLES
+        )
         assertEquals(expectedCommandLine, actualCommandLine)
 
         checkEvents(
@@ -221,7 +232,12 @@ class RsBuildActionTest : CargoBuildTest() {
         mockBuildProgressListener!!.waitFinished()
 
         val actualCommandLine = lastBuildCommandLine!!
-        val expectedCommandLine = CargoCommandLine("build", actualCommandLine.workingDirectory, listOf("--all", "--all-targets"))
+        val expectedCommandLine = CargoCommandLine(
+            "build",
+            actualCommandLine.workingDirectory,
+            listOf("--message-format=json-diagnostic-rendered-ansi", "--all", "--all-targets"),
+            environmentVariables = BUILD_VARIABLES
+        )
         assertEquals(expectedCommandLine, actualCommandLine)
 
         checkEvents(
@@ -271,4 +287,15 @@ class RsBuildActionTest : CargoBuildTest() {
         runManager.addConfiguration(settings)
         runManager.selectedConfiguration = settings
     }
+
+    companion object {
+        private val BUILD_VARIABLES: EnvironmentVariablesData =
+            EnvironmentVariablesData.create(
+                mapOf(
+                    "CARGO_TERM_PROGRESS_WHEN" to "always",
+                    "CARGO_TERM_PROGRESS_WIDTH" to "1000"
+                ),
+                true
+            )
+    }
 }