import pathlib


def resolve_nested_dir(path: pathlib.Path, root: pathlib.Path) -> pathlib.Path:
  """Returns the path that exists, out of foo/... and foo/foo/..., with root."""
  nested = path.parts[0] / path
  root_path = root / path
  root_nested_path = root / nested
  if root_path.exists():
    return root_path
  elif root_nested_path.exists():
    return root_nested_path
  raise ValueError(f'Could not find {path} or {nested}')