From cc81681de5961c991e7a7aca26ce7bdec843501b Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Thu, 16 Feb 2023 08:14:20 +0200 Subject: [PATCH] config: don't set default cache dir Setting default values is very untypical for all other applications and the info config has is not enough to make a decision on the default cache dir, especially when dealing with gitfs. Fixes #8705 --- dvc/cachemgr.py | 24 ++++++++++++++++++++++-- dvc/commands/cache.py | 13 +++++++++++-- dvc/config.py | 4 ---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/dvc/cachemgr.py b/dvc/cachemgr.py index eeb4fe76d6a..0aea79ba9b7 100644 --- a/dvc/cachemgr.py +++ b/dvc/cachemgr.py @@ -1,3 +1,5 @@ +import os + from dvc.fs import GitFileSystem, Schemes from dvc_data.hashfile.db import get_odb @@ -28,16 +30,34 @@ def __init__(self, repo): self.config = config = repo.config["cache"] self._odb = {} + default = None + if repo and repo.dvc_dir: + if isinstance(repo.fs, GitFileSystem): + relparts = () + if repo.root_dir != "/": + # subrepo + relparts = repo.fs.path.relparts(repo.root_dir, "/") + dvc_dir = os.path.join( + repo.scm.root_dir, + *relparts, + repo.DVC_DIR, + ) + if os.path.exists(dvc_dir): + default = os.path.join(dvc_dir, self.CACHE_DIR) + else: + default = repo.fs.path.join(repo.dvc_dir, self.CACHE_DIR) + local = config.get("local") if local: settings = {"name": local} - elif "dir" not in config: + elif "dir" not in config and not default: settings = None else: from dvc.config_schema import LOCAL_COMMON - settings = {"url": config["dir"]} + url = config.get("dir") or default + settings = {"url": url} for opt in LOCAL_COMMON: if opt in config: settings[str(opt)] = config.get(opt) diff --git a/dvc/commands/cache.py b/dvc/commands/cache.py index cab80053575..e21b2081dc4 100644 --- a/dvc/commands/cache.py +++ b/dvc/commands/cache.py @@ -1,4 +1,5 @@ import argparse +import os from dvc.cli import completion from dvc.cli.utils import append_doc_link, fix_subparsers @@ -9,13 +10,21 @@ class CmdCacheDir(CmdConfig): def run(self): if self.args.value is None and not self.args.unset: + from dvc.config import ConfigError + if self.args.level: conf = self.config.read(level=self.args.level) else: # Use merged config with default values conf = self.config - self._check(conf, False, "cache", "dir") - ui.write(conf["cache"]["dir"]) + try: + self._check(conf, False, "cache", "dir") + path = conf["cache"]["dir"] + except ConfigError: + if not self.config.dvc_dir or self.args.level: + raise + path = os.path.join(self.config.dvc_dir, "cache") + ui.write(path) return 0 with self.config.edit(level=self.args.level) as conf: if self.args.unset: diff --git a/dvc/config.py b/dvc/config.py index 112caef7eee..e8524fc67ef 100644 --- a/dvc/config.py +++ b/dvc/config.py @@ -170,10 +170,6 @@ def load(self, validate: bool = True, config: Optional["DictStrAny"] = None): self.clear() self.update(conf) - # Add resolved default cache.dir - if not self["cache"].get("dir") and self.dvc_dir: - self["cache"]["dir"] = os.path.join(self.dvc_dir, "cache") - def _get_fs(self, level): # NOTE: this might be a Gitfs, which doesn't see things outside of # the repo.