Repository: sqlfluff/sqlfluff
Pull request: #716: Support configurable file extensions
This resolves #716.

This was originally configurable through a kwarg of the `Linter()` class, but nothing ever used that kwarg. I've moved the configuration of that value to the `FluffConfig` so that it can be driven by the config files. I've also added `.dml`, `.ddl` and `.sql.j2` files to the default search.

Problem statement:
Add Support for Alternate File Types/Extensions
It would be awesome if sqlfluff could support configuring a list of file types/extensions to be considered by the `lint` and `fix` commands. Ideally, the configuration could be done in the `.sqlfluff` file.

One example would be Jinja2 files that are templated SQL statements. Using the convention of `.sql.j2	 for the file extension would allow these files to be targeted for formatting along side `.sql` files.

Thanks!

Hints from issue discussion:

Commit message: #716: Support configurable file extensions

Patch to review:
diff --git a/src/sqlfluff/core/default_config.cfg b/src/sqlfluff/core/default_config.cfg
index 04d3bef4205..f59e5593e97 100644
--- a/src/sqlfluff/core/default_config.cfg
+++ b/src/sqlfluff/core/default_config.cfg
@@ -9,6 +9,9 @@ recurse = 0
 output_line_length = 80
 runaway_limit = 10
 ignore_templated_areas = True
+# Comma seperated list of file extentions to lint.
+# NB: This config will only apply in the root folder.
+sql_file_exts = .sql,.sql.j2,.dml,.ddl
 
 [sqlfluff:indentation]
 indented_joins = False
diff --git a/src/sqlfluff/core/linter.py b/src/sqlfluff/core/linter.py
index bb3399509b4..9ef8ec521cd 100644
--- a/src/sqlfluff/core/linter.py
+++ b/src/sqlfluff/core/linter.py
@@ -703,14 +703,12 @@ class Linter:
 
     def __init__(
         self,
-        sql_exts: Tuple[str, ...] = (".sql",),
         config: Optional[FluffConfig] = None,
         formatter: Any = None,
         dialect: Optional[str] = None,
         rules: Optional[Union[str, List[str]]] = None,
         user_rules: Optional[Union[str, List[str]]] = None,
     ) -> None:
-        self.sql_exts = sql_exts
         # Store the config object
         self.config = FluffConfig.from_kwargs(
             config=config, dialect=dialect, rules=rules
@@ -1186,7 +1184,7 @@ def paths_from_path(
                 # that the ignore file is processed after the sql file.
 
                 # Scan for remaining files
-                for ext in self.sql_exts:
+                for ext in self.config.get("sql_file_exts", default=".sql").split(","):
                     # is it a sql file?
                     if fname.endswith(ext):
                         buffer.append(fpath)
diff --git a/test/core/linter_test.py b/test/core/linter_test.py
index 441030a05ac..15a80a568ef 100644
--- a/test/core/linter_test.py
+++ b/test/core/linter_test.py
@@ -28,6 +28,22 @@ def test__linter__path_from_paths__dir():
     }
 
 
+def test__linter__path_from_paths__default():
+    """Test .sql files are found by default."""
+    lntr = Linter()
+    paths = normalise_paths(lntr.paths_from_path("test/fixtures/linter"))
+    assert "test.fixtures.linter.passing.sql" in paths
+    assert "test.fixtures.linter.discovery_file.txt" not in paths
+
+
+def test__linter__path_from_paths__exts():
+    """Test configuration of file discovery."""
+    lntr = Linter(config=FluffConfig(overrides={"sql_file_exts": ".txt"}))
+    paths = normalise_paths(lntr.paths_from_path("test/fixtures/linter"))
+    assert "test.fixtures.linter.passing.sql" not in paths
+    assert "test.fixtures.linter.discovery_file.txt" in paths
+
+
 def test__linter__path_from_paths__file():
     """Test extracting paths from a file path."""
     lntr = Linter()
@@ -51,7 +67,7 @@ def test__linter__path_from_paths__not_exist_ignore():
 
 def test__linter__path_from_paths__explicit_ignore():
     """Test ignoring files that were passed explicitly."""
-    lntr = Linter(config=FluffConfig())
+    lntr = Linter()
     paths = lntr.paths_from_path(
         "test/fixtures/linter/sqlfluffignore/path_a/query_a.sql",
         ignore_non_existent_files=True,
diff --git a/test/fixtures/linter/discovery_file.txt b/test/fixtures/linter/discovery_file.txt
new file mode 100644
index 00000000000..85fbbd31442
--- /dev/null
+++ b/test/fixtures/linter/discovery_file.txt
@@ -0,0 +1,2 @@
+-- This is a text file to test discovery of configured file extensions.
+SELECT 1
\ No newline at end of file


Code review comments:
diff_hunk: @@ -9,6 +9,9 @@ recurse = 0
 output_line_length = 80
 runaway_limit = 10
 ignore_templated_areas = True
+# Comma seperated list of file extentions to lint.
path: src/sqlfluff/core/default_config.cfg
text: @user1:
```suggestion
# Comma separated list of file extensions to lint.

```