Repository: prettier/prettier (JavaScript)
Date: 2018-05-30 12:52:27+00:00
Task type: Bug Fix

Issue:
v1.13.0 erroneously reports non-zero exit code
reduced test case:

```
$ prettier *.js --debug-check --list-different
```

* with v1.12.1, this reports exit status 0 if the code is properly formatted
* with v1.13.0, this reports exit status 1 even though the code is properly formatted

----

original test case: https://gist.github.com/FND/8cdcc6c60bd5430b2204fe930acc3eec/revisions

with v1.12.1, the following works as expected:

1.  actively reformat source code:

    ```
    $ prettier --write *.js
    ```

2.  ensure source code is formatted properly (e.g. as part of the test suite, continuous integration etc.):

    ```
    $ prettier --write *.js --debug-check --list-different --no-write
    ```

    here `--no-write` overrides `--write`, which is useful because we don't have to modify #1's base command - particularly in `package.json`:

    ```json
    "scripts": {
        "test": "npm run format -- --debug-check --list-different --no-write",
        "format": "prettier --write '*.js'"
    }
    ```

however, with v1.13.0, that second command fails with exit code 1, despite the source code being properly formatted


Issue discussion:

Solution patch:
diff --git a/src/cli/util.js b/src/cli/util.js
index cb97678228f6..8bcf4506f0d9 100644
--- a/src/cli/util.js
+++ b/src/cli/util.js
@@ -120,11 +120,13 @@ function logFileInfoOrDie(context) {
   );
 }
 
-function writeOutput(result, options) {
+function writeOutput(context, result, options) {
   // Don't use `console.log` here since it adds an extra newline at the end.
-  process.stdout.write(result.formatted);
+  process.stdout.write(
+    context.argv["debug-check"] ? result.filepath : result.formatted
+  );
 
-  if (options.cursorOffset >= 0) {
+  if (options && options.cursorOffset >= 0) {
     process.stderr.write(result.cursorOffset + "\n");
   }
 }
@@ -195,7 +197,7 @@ function format(context, input, opt) {
         );
       }
     }
-    return { formatted: opt.filepath || "(stdin)\n" };
+    return { formatted: pp, filepath: opt.filepath || "(stdin)\n" };
   }
 
   return prettier.formatWithCursor(input, opt);
@@ -308,7 +310,7 @@ function formatStdin(context) {
 
   thirdParty.getStream(process.stdin).then(input => {
     if (relativeFilepath && ignorer.filter([relativeFilepath]).length === 0) {
-      writeOutput({ formatted: input }, {});
+      writeOutput(context, { formatted: input });
       return;
     }
 
@@ -319,7 +321,7 @@ function formatStdin(context) {
         return;
       }
 
-      writeOutput(format(context, input, options), options);
+      writeOutput(context, format(context, input, options), options);
     } catch (error) {
       handleError(context, "stdin", error);
     }
@@ -410,7 +412,7 @@ function formatFiles(context) {
     }
 
     if (fileIgnored) {
-      writeOutput({ formatted: input }, options);
+      writeOutput(context, { formatted: input }, options);
       return;
     }
 
@@ -465,13 +467,13 @@ function formatFiles(context) {
         context.logger.log(`${chalk.grey(filename)} ${Date.now() - start}ms`);
       }
     } else if (context.argv["debug-check"]) {
-      if (output) {
-        context.logger.log(output);
+      if (result.filepath) {
+        context.logger.log(result.filepath);
       } else {
         process.exitCode = 2;
       }
     } else if (!context.argv["list-different"]) {
-      writeOutput(result, options);
+      writeOutput(context, result, options);
     }
   });
 }


Test patch:
diff --git a/tests_integration/__tests__/__snapshots__/debug-check.js.snap b/tests_integration/__tests__/__snapshots__/debug-check.js.snap
index 0f63c4825fee..4231b4bc9a62 100644
--- a/tests_integration/__tests__/__snapshots__/debug-check.js.snap
+++ b/tests_integration/__tests__/__snapshots__/debug-check.js.snap
@@ -4,6 +4,15 @@ exports[`checks stdin with --debug-check (write) 1`] = `Array []`;
 
 exports[`doesn't crash when --debug-check is passed (write) 1`] = `Array []`;
 
+exports[`should not exit non-zero for already prettified code with --debug-check + --list-different (stderr) 1`] = `""`;
+
+exports[`should not exit non-zero for already prettified code with --debug-check + --list-different (stdout) 1`] = `
+"issue-4599.js
+"
+`;
+
+exports[`should not exit non-zero for already prettified code with --debug-check + --list-different (write) 1`] = `Array []`;
+
 exports[`show diff for 2+ error files with --debug-check (stderr) 1`] = `
 "[error] a.debug-check: prettier(input) !== prettier(prettier(input))
 [error] Index: 
diff --git a/tests_integration/__tests__/debug-check.js b/tests_integration/__tests__/debug-check.js
index 9e02eb208806..d0641ee64053 100644
--- a/tests_integration/__tests__/debug-check.js
+++ b/tests_integration/__tests__/debug-check.js
@@ -30,3 +30,13 @@ describe("show diff for 2+ error files with --debug-check", () => {
     status: "non-zero"
   });
 });
+
+describe("should not exit non-zero for already prettified code with --debug-check + --list-different", () => {
+  runPrettier("cli/debug-check", [
+    "issue-4599.js",
+    "--debug-check",
+    "--list-different"
+  ]).test({
+    status: 0
+  });
+});
diff --git a/tests_integration/cli/debug-check/issue-4599.js b/tests_integration/cli/debug-check/issue-4599.js
new file mode 100644
index 000000000000..aa92ee1f9d8c
--- /dev/null
+++ b/tests_integration/cli/debug-check/issue-4599.js
@@ -0,0 +1,1 @@
+console.log("…");