Repository: prettier/prettier (JavaScript)
Date: 2018-07-29 09:40:41+00:00
Task type: Bug Fix

Issue:
prettier traverses .git files and tries to format them
**Environments:**
- Prettier Version: 1.13.7
- Running Prettier via: CLI
- Runtime: node v8.9.4, npm v5.6.0, yarn v1.5.1
- Operating System: Linux

**Steps to reproduce:**
- In a git repository with `prettier` installed
- Create a git branch that ends in `.js`
- Run `prettier --write '**/*.js'`

I created a repository with everything ready to reproduce ([link](https://github.com/gillchristian/prettier-report)).

**Expected behavior:**
`.git/` directory should be ignored, `prettier` should run without errors.

**Actual behavior:**
`.git/` directory is traversed in files and `prettier` throws syntax error (obviously because `.git/` files aren't JS files).

---

I'm aware of #2882 but `.git/*` files are still parsed.

Also, I'm up to work on opening a PR solving this issue. But I believe it's better to ask first.


Issue discussion:
Thanks for the repro repo, it's much appreciated
Maybe we should add `.git`, `.hg`, `.svn` to `exclude` when expanding globs?

Solution patch:
diff --git a/src/cli/util.js b/src/cli/util.js
index 497f35b932bf..b5ffadfdca35 100644
--- a/src/cli/util.js
+++ b/src/cli/util.js
@@ -399,10 +399,12 @@ function createIgnorerFromContextOrDie(context) {
 }
 
 function eachFilename(context, patterns, callback) {
+  // The '!./' globs are due to https://github.com/prettier/prettier/issues/2110
   const ignoreNodeModules = context.argv["with-node-modules"] !== true;
   if (ignoreNodeModules) {
     patterns = patterns.concat(["!**/node_modules/**", "!./node_modules/**"]);
   }
+  patterns = patterns.concat(["!**/.{git,svn,hg}/**", "!./.{git,svn,hg}/**"]);
 
   try {
     const filePaths = globby


Test patch:
diff --git a/tests_integration/__tests__/__snapshots__/ignore-vcs-files.js.snap b/tests_integration/__tests__/__snapshots__/ignore-vcs-files.js.snap
new file mode 100644
index 000000000000..c265128e8874
--- /dev/null
+++ b/tests_integration/__tests__/__snapshots__/ignore-vcs-files.js.snap
@@ -0,0 +1,10 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`ignores files in version control systems (stderr) 1`] = `""`;
+
+exports[`ignores files in version control systems (stdout) 1`] = `
+"file.js
+"
+`;
+
+exports[`ignores files in version control systems (write) 1`] = `Array []`;
diff --git a/tests_integration/__tests__/__snapshots__/multiple-patterns.js.snap b/tests_integration/__tests__/__snapshots__/multiple-patterns.js.snap
index b790ddec7a1b..9f984bcc18a9 100644
--- a/tests_integration/__tests__/__snapshots__/multiple-patterns.js.snap
+++ b/tests_integration/__tests__/__snapshots__/multiple-patterns.js.snap
@@ -72,7 +72,7 @@ directory/nested-directory/nested-directory-file.js
 exports[`multiple patterns with non exists pattern (write) 1`] = `Array []`;
 
 exports[`multiple patterns, throw error and exit with non zero code on non existing files (stderr) 1`] = `
-"[error] No matching files. Patterns tried: non-existent.js other-non-existent.js !**/node_modules/** !./node_modules/**
+"[error] No matching files. Patterns tried: non-existent.js other-non-existent.js !**/node_modules/** !./node_modules/** !**/.{git,svn,hg}/** !./.{git,svn,hg}/**
 "
 `;
 
diff --git a/tests_integration/__tests__/ignore-vcs-files.js b/tests_integration/__tests__/ignore-vcs-files.js
new file mode 100644
index 000000000000..0bb13ce3f4f2
--- /dev/null
+++ b/tests_integration/__tests__/ignore-vcs-files.js
@@ -0,0 +1,16 @@
+"use strict";
+
+const runPrettier = require("../runPrettier");
+
+expect.addSnapshotSerializer(require("../path-serializer"));
+
+describe("ignores files in version control systems", () => {
+  runPrettier("cli/ignore-vcs-files", [
+    ".svn/file.js",
+    ".hg/file.js",
+    "file.js",
+    "-l"
+  ]).test({
+    status: 1
+  });
+});
diff --git a/tests_integration/cli/ignore-vcs-files/.hg/file.js b/tests_integration/cli/ignore-vcs-files/.hg/file.js
new file mode 100644
index 000000000000..ad9a93a7c160
--- /dev/null
+++ b/tests_integration/cli/ignore-vcs-files/.hg/file.js
@@ -0,0 +1,1 @@
+'use strict';
diff --git a/tests_integration/cli/ignore-vcs-files/.svn/file.js b/tests_integration/cli/ignore-vcs-files/.svn/file.js
new file mode 100644
index 000000000000..3d0ca1bd1785
--- /dev/null
+++ b/tests_integration/cli/ignore-vcs-files/.svn/file.js
@@ -0,0 +1,2 @@
+/* eslint-disable */
+'use strict';
diff --git a/tests_integration/cli/ignore-vcs-files/file.js b/tests_integration/cli/ignore-vcs-files/file.js
new file mode 100644
index 000000000000..ad9a93a7c160
--- /dev/null
+++ b/tests_integration/cli/ignore-vcs-files/file.js
@@ -0,0 +1,1 @@
+'use strict';