55from pathlib import Path
66from typing import TYPE_CHECKING
77
8+ from tox .config .types import MissingRequiredConfigKeyError
89from tox .report import HandledError
910
1011from .legacy_toml import LegacyToml
@@ -59,21 +60,32 @@ def _locate_source() -> Source | None:
5960 for base in chain ([folder ], folder .parents ):
6061 for src_type in SOURCE_TYPES :
6162 candidate : Path = base / src_type .FILENAME
62- try :
63- return src_type (candidate )
64- except ValueError :
65- pass
63+ if candidate .exists ():
64+ try :
65+ return src_type (candidate )
66+ except MissingRequiredConfigKeyError as exc :
67+ msg = f"{ src_type .__name__ } skipped loading { candidate .resolve ()} due to { exc } "
68+ logging .info (msg )
69+ except ValueError as exc :
70+ msg = f"{ src_type .__name__ } failed loading { candidate .resolve ()} due to { exc } "
71+ raise HandledError (msg ) from exc
6672 return None
6773
6874
6975def _load_exact_source (config_file : Path ) -> Source :
7076 # if the filename matches to the letter some config file name do not fallback to other source types
77+ if not config_file .exists ():
78+ msg = f"config file { config_file } does not exist"
79+ raise HandledError (msg )
7180 exact_match = [s for s in SOURCE_TYPES if config_file .name == s .FILENAME ] # pragma: no cover
7281 for src_type in exact_match or SOURCE_TYPES : # pragma: no branch
7382 try :
7483 return src_type (config_file )
75- except ValueError : # noqa: PERF203
84+ except MissingRequiredConfigKeyError : # noqa: PERF203
7685 pass
86+ except ValueError as exc :
87+ msg = f"{ src_type .__name__ } failed loading { config_file .resolve ()} due to { exc } "
88+ raise HandledError (msg ) from exc
7789 msg = f"could not recognize config file { config_file } "
7890 raise HandledError (msg )
7991
@@ -88,7 +100,7 @@ def _create_default_source(root_dir: Path | None) -> Source:
88100 else : # if not set use where we find pyproject.toml in the tree or cwd
89101 empty = root_dir
90102 names = " or " .join ({i .FILENAME : None for i in SOURCE_TYPES })
91- logging .warning ("No %s found, assuming empty tox.ini at %s" , names , empty )
103+ logging .warning ("No loadable %s found, assuming empty tox.ini at %s" , names , empty )
92104 return ToxIni (empty / "tox.ini" , content = "" )
93105
94106
0 commit comments