Repository navigation
Description
Note
Authorship: the content below was drafted by an AI coding agent and filed via gh under @msto's account. It reflects the agent's analysis, not a statement authored by @msto.
Summary
import latch_cli.utils — and ~26 latch_cli submodules that depend on it — raise
ImportError: cannot import name 'get_auth_header' from partially initialized module 'latch_cli.utils' (most likely due to a circular import) when imported before the
top-level latch package. The packaged CLI imports latch before latch_cli.utils,
so latch <cmd> still works today, but the coupling is latent and fragile, and direct
or programmatic imports of these modules fail outright.
Reproduction
$ python -c "import latch_cli.utils"
ImportError: cannot import name 'get_auth_header' from partially initialized module
'latch_cli.utils' (most likely due to a circular import)Also fails cold: latch_cli.utils.path, latch_cli.services.docker.utils,
latch_cli.exceptions.handler, and ~22 others.
Root cause
latch_cli/utils/__init__.py:22 runs from latch.utils import current_workspace at
module load. Python must execute latch/__init__.py before it can bind that name, so
the line drags the entire latch package init into the middle of
latch_cli.utils.__init__. That init chain loops straight back into latch_cli.utils:
latch_cli/utils/__init__.py:22
→ latch/__init__.py → latch.resources.tasks → latch.resources.dynamic
→ latch/executions.py:9 (from latch_cli.utils.path import normalize_path)
→ latch_cli/utils/path.py:10 (from latch_cli.utils import get_auth_header) 💥
get_auth_header is defined at __init__.py:73 — after line 22 — so it isn't bound
yet, and the import fails. Whichever of latch_cli.utils / latch starts loading first
decides the outcome; the CLI wins the race only because main.py imports latch
(line 22) before latch_cli.utils (line 30).
Why it surfaced in 2.77.0
The cycle is long-standing — the three import lines date to 2023–2024 and 2.77.0 did not
touch them. But 2.77.0 added latch_cli.services.docker.utils (backing latch image),
a new module that imports latch_cli.utils before latch — a fresh trip-wire onto the
latent cycle, and the likely reason a circular-import report appeared now.
Blast radius
26 latch_cli modules fail cold import through this same path, including
latch_cli.utils, latch_cli.utils.path, latch_cli.docker_utils,
latch_cli.services.docker.utils, latch_cli.exceptions.handler,
latch_cli.services.cp.glob, most of latch_cli.services.init.*, and
latch_cli.services.k8s.{attach,execute}. (A separate self-cycle in
latch_cli.snakemake.config.utils is a distinct, unrelated issue.)
Proposed fix
current_workspace is used in exactly one place — hash_directory
(latch_cli/utils/__init__.py:172). Drop the module-level import and defer it to that
call site, so latch_cli.utils no longer triggers latch's init during its own load:
def hash_directory(dir_path: Path, *, silent: bool = False) -> str:
from latch.utils import current_workspace # deferred: breaks latch ⇄ latch_cli.utils cycle
...This severs the cycle at its origin and fixes every affected module regardless of import
order. A regression guard that cold-imports each module in a fresh interpreter
(python -c "import <mod>") prevents this class of bug from returning.
Activity