diff --git a/docs/getting-started/configure.md b/docs/getting-started/configure.md index 39b6e45..44a2526 100644 --- a/docs/getting-started/configure.md +++ b/docs/getting-started/configure.md @@ -22,13 +22,13 @@ It says where each thing is: | What | Where | Change it with | | --- | --- | --- | -| the graph (`graph.db`, SQLite) | the nearest folder, starting at the current one, that has a `packs/` folder or a `graph.db`; the current folder if none does | `--db`, `$ANALECTS_DB` | +| the graph (`graph.db`, SQLite) | the nearest folder, starting at the current one, that has a `graph.db` or a `packs/` folder holding a pack; the current folder if none does | `--db`, `$ANALECTS_DB` | | the config (`analects.yaml`) | `~/.analects/analects.yaml` | `$ANALECTS_CONFIG` | | the packs | the ones Analects ships with, plus a project's own `packs/` folder (`analects schema new`) | `$ANALECTS_PACKS` | -Any folder called `packs/` counts, even one that holds no Analects packs, so the -graph can land in a folder above the one you are in. `analects init` prints the path -it chose: check it. +A `packs/` folder counts only when it holds a pack (a `@/pack.yaml`), +so a folder of other files that happens to be called `packs/` is ignored. +`analects init` prints the path it chose. Analects was called kg. An old `~/.kg/kg.yaml`, the `kg` command and any `$KG_…` setting still work; a `$ANALECTS_…` setting wins over its `$KG_…` twin diff --git a/src/analects/core/config.py b/src/analects/core/config.py index 2ac17f1..d9ebcae 100644 --- a/src/analects/core/config.py +++ b/src/analects/core/config.py @@ -112,7 +112,7 @@ def packs_dir(override: str | None = None) -> Path: if env_set("PACKS"): return Path(env("PACKS")) local = repo_root() / "packs" - return local if local.is_dir() else builtin_packs() + return local if holds_packs(local) else builtin_packs() def pack_roots(override: str | None = None) -> list[Path]: @@ -161,12 +161,18 @@ def builtin_packs() -> Path: return Path(__file__).resolve().parents[1] / "packs" / "builtin" +def holds_packs(folder: Path) -> bool: + """True when `folder` is a project's packs folder: it holds at least one pack, + a `@/pack.yaml`. A folder that is only called packs/ does not.""" + return folder.is_dir() and any(folder.glob("*/pack.yaml")) + + def repo_root() -> Path: """Nearest ancestor holding a project's `packs/` or its `graph.db`, else the current directory.""" here = Path.cwd().resolve() for candidate in (here, *here.parents): - if (candidate / "packs").is_dir() or (candidate / "graph.db").is_file(): + if holds_packs(candidate / "packs") or (candidate / "graph.db").is_file(): return candidate return here diff --git a/tests/unit/test_graph_packs.py b/tests/unit/test_graph_packs.py index 47d757f..db9e7ab 100644 --- a/tests/unit/test_graph_packs.py +++ b/tests/unit/test_graph_packs.py @@ -222,3 +222,20 @@ def test_a_broken_pack_stops_only_the_graphs_that_use_it(store, tmp_path, monkey assert out.exit_code == 0, out.output shown = analects("schema", "show") assert shown.exit_code == 0 and "politician-uk@0.1:" in shown.output, shown.output + + +def test_a_packs_folder_without_packs_does_not_move_the_graph( + packs_dir, tmp_path, monkeypatch): + # A folder called packs/ that holds no pack, such as one of CSVs, is not a + # project's packs: init keeps the graph where it was run. + (tmp_path / "packs").mkdir() + (tmp_path / "packs" / "prices.csv").write_text("year,price\n2026,3\n") + here = tmp_path / "work" + here.mkdir() + monkeypatch.chdir(here) + monkeypatch.delenv("ANALECTS_DB", raising=False) + monkeypatch.setenv("ANALECTS_CONFIG", str(tmp_path / "analects.yaml")) + monkeypatch.setenv("ANALECTS_AS", "tester") + assert analects("init").exit_code == 0 + assert (here / "graph.db").is_file() + assert not (tmp_path / "graph.db").exists()