Skip to content

Commit c20bc43

Browse files
committed
feat: handle sqlfluff cache invalidation in pre and post 3.2
1 parent 80bfa16 commit c20bc43

1 file changed

Lines changed: 42 additions & 16 deletions

File tree

src/dbt_core_interface/project.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -690,35 +690,61 @@ def get_sqlfluff_configuration(
690690
**kwargs: t.Any,
691691
) -> FluffConfig:
692692
"""Load the SQLFluff configuration for a given path, otherwise for the project itself."""
693-
from sqlfluff.core.config import ConfigLoader, FluffConfig
693+
import sqlfluff.core.config as sqlfluff_config
694694

695695
overrides = {k: kwargs[k] for k in kwargs if kwargs[k] is not None}
696696
overrides["dialect"] = self.runtime_config.credentials.type
697697
overrides["processes"] = 1
698698

699-
loader = ConfigLoader.get_global()
700-
loader_cache = loader._config_cache # pyright: ignore[reportPrivateUsage]
701-
for p in list(loader_cache):
702-
p_obj = Path(p)
703-
last_mtime = self._sqlfluff_mtime_cache.get(p_obj, 0.0)
704-
curr_mtime = p_obj.stat().st_mtime if p_obj.exists() else 0.0
705-
if curr_mtime > last_mtime:
706-
del loader_cache[p]
699+
conf_files = [
700+
"setup.cfg",
701+
"tox.ini",
702+
"pep8.ini",
703+
".sqlfluff",
704+
".sqlfluffignore",
705+
"pyproject.toml",
706+
]
707+
invalidate_caches = False
708+
709+
path = Path(path or self.project_root).expanduser().resolve()
710+
for parent in path.parents:
711+
for conf_file in conf_files:
712+
f = parent / conf_file
713+
if f.exists():
714+
last_mtime = self._sqlfluff_mtime_cache.get(f, 0.0)
715+
curr_mtime = f.stat().st_mtime
716+
if curr_mtime > last_mtime:
717+
invalidate_caches = True
718+
self._sqlfluff_mtime_cache[f] = curr_mtime
719+
if path == Path.home() or path == path.root:
720+
break
707721

708722
if extra_config_path:
709-
_ = loader_cache.pop(str(extra_config_path), None)
723+
explicit_conf = Path(extra_config_path).expanduser().resolve()
724+
if explicit_conf.exists():
725+
last_mtime = self._sqlfluff_mtime_cache.get(explicit_conf, 0.0)
726+
curr_mtime = explicit_conf.stat().st_mtime if explicit_conf.exists() else 0.0
727+
if curr_mtime > last_mtime:
728+
invalidate_caches = True
729+
self._sqlfluff_mtime_cache[explicit_conf] = curr_mtime
730+
731+
if invalidate_caches:
732+
if hasattr(sqlfluff_config, "clear_config_caches"):
733+
# SQLFLuff 3.2+
734+
sqlfluff_config.clear_config_caches()
735+
else:
736+
# SQLFLuff 3.1 and earlier
737+
loader = sqlfluff_config.ConfigLoader.get_global()
738+
loader_cache: dict[str, str] = getattr(loader, "_config_cache", {})
739+
loader_cache.clear()
710740

711-
fluff_conf = FluffConfig.from_path(
712-
path=str(path or self.project_root),
741+
fluff_conf = sqlfluff_config.FluffConfig.from_path(
742+
path=str(path),
713743
extra_config_path=str(extra_config_path) if extra_config_path else None,
714744
ignore_local_config=ignore_local_config,
715745
overrides=overrides,
716746
)
717747

718-
for p in loader_cache:
719-
p_obj = Path(p)
720-
self._sqlfluff_mtime_cache[p_obj] = p_obj.stat().st_mtime if p_obj.exists() else 0.0
721-
722748
return fluff_conf
723749

724750
def lint(

0 commit comments

Comments
 (0)