Skip to content

Commit

Permalink
config: don't set default cache dir
Browse files Browse the repository at this point in the history
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
  • Loading branch information
efiop committed Feb 16, 2023
1 parent ddeb32e commit db6215c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
20 changes: 18 additions & 2 deletions dvc/cachemgr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from dvc.fs import GitFileSystem, Schemes
from dvc_data.hashfile.db import get_odb

Expand Down Expand Up @@ -28,16 +30,30 @@ 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):
dvc_dir = os.path.join(
repo.scm.root_dir,
*repo.fs.path.relparts(repo.root_dir, "/"),
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)
Expand Down
13 changes: 11 additions & 2 deletions dvc/commands/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import os

from dvc.cli import completion
from dvc.cli.utils import append_doc_link, fix_subparsers
Expand All @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit db6215c

Please sign in to comment.