From b74aa31b876830a1617d24c3c1cad0f9d7ac4a9d Mon Sep 17 00:00:00 2001 From: Qubasa Date: Thu, 24 Jul 2025 17:29:09 +0700 Subject: [PATCH] clan-lib: Fix missing logging for flake.select execution --- docs/site/developer/contributing/debugging.md | 3 +- pkgs/clan-cli/clan_lib/flake/flake.py | 62 +++++++------------ 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/docs/site/developer/contributing/debugging.md b/docs/site/developer/contributing/debugging.md index a20ea8bca..a26c3bd1e 100644 --- a/docs/site/developer/contributing/debugging.md +++ b/docs/site/developer/contributing/debugging.md @@ -85,7 +85,8 @@ export CLAN_DEBUG_COMMANDS=1 These options help you pinpoint the source and context of print messages and debug logs during development. - +!!! Note + `CLAN_DEBUG_NIX_SELECTORS` and `CLAN_DEBUG_NIX_PREFETCH` will only print the command on a cache miss! ## Analyzing Performance diff --git a/pkgs/clan-cli/clan_lib/flake/flake.py b/pkgs/clan-cli/clan_lib/flake/flake.py index 1f20731ae..914c2f876 100644 --- a/pkgs/clan-cli/clan_lib/flake/flake.py +++ b/pkgs/clan-cli/clan_lib/flake/flake.py @@ -2,7 +2,6 @@ import json import logging import os import re -import textwrap from dataclasses import asdict, dataclass, field from enum import Enum from functools import cache @@ -729,7 +728,7 @@ class Flake: self.identifier, ] - trace_prefetch = os.environ.get("CLAN_DEBUG_NIX_PREFETCH", "0") == "1" + trace_prefetch = os.environ.get("CLAN_DEBUG_NIX_PREFETCH", False) == "1" if not trace_prefetch: log.debug(f"Prefetching flake {self.identifier}") flake_prefetch = run(nix_command(cmd), RunOpts(trace=trace_prefetch)) @@ -862,47 +861,11 @@ class Flake: ]; }} """ - if len(selectors) > 1 : - msg = textwrap.dedent(f""" - clan select "{selectors}" - """).lstrip("\n").rstrip("\n") - if os.environ.get("CLAN_DEBUG_NIX_SELECTORS"): - msg += textwrap.dedent(f""" - to debug run: - nix repl --expr 'rec {{ - flake = builtins.getFlake "{self.identifier}"; - selectLib = (builtins.getFlake "path:{select_source()}?narHash={select_hash}").lib; - query = [ - {" ".join( - [ - f"(selectLib.select ''{selector}'' flake)" - for selector in selectors - ] - )} - ]; - }}' - """).lstrip("\n") - log.debug(msg) - # fmt: on - elif len(selectors) == 1: - msg = textwrap.dedent(f""" - $ clan select "{selectors[0]}" - """).lstrip("\n").rstrip("\n") - if os.environ.get("CLAN_DEBUG_NIX_SELECTORS"): - msg += textwrap.dedent(f""" - to debug run: - nix repl --expr 'rec {{ - flake = builtins.getFlake "{self.identifier}"; - selectLib = (builtins.getFlake "path:{select_source()}?narHash={select_hash}").lib; - query = selectLib.select '"''{selectors[0]}''"' flake; - }}' - """).lstrip("\n") - log.debug(msg) - + trace = os.environ.get("CLAN_DEBUG_NIX_SELECTORS", False) == "1" build_output = Path( run( nix_build(["--expr", nix_code, *nix_options]), - RunOpts(log=Log.NONE, trace=False), + RunOpts(log=Log.NONE, trace=trace), ).stdout.strip() ) @@ -918,6 +881,22 @@ class Flake: if self.flake_cache_path: self._cache.save_to_file(self.flake_cache_path) + def log_selectors(self, selectors: list[str]) -> None: + if not selectors: + return + + if len(selectors) > 1: + log.debug("==== Printing multi selector command as multiple commands. ====") + + msg = "" + # Build base message + for selector in selectors: + msg += f'$ clan select "{selector}"' + if len(selectors) > 1: + msg += "\n" + + log.debug(msg) + def precache(self, selectors: list[str]) -> None: """ Ensures that the specified selectors are cached locally. @@ -929,6 +908,7 @@ class Flake: Args: selectors (list[str]): A list of attribute selectors to check and cache. """ + if self._cache is None: self.invalidate_cache() assert self._cache is not None @@ -956,6 +936,8 @@ class Flake: assert self._cache is not None assert self.flake_cache_path is not None + self.log_selectors([selector]) + if not self._cache.is_cached(selector): log.debug(f"Cache miss for {selector}") self.get_from_nix([selector])