Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/getting-started/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<name>@<version>/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
Expand Down
10 changes: 8 additions & 2 deletions src/analects/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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 `<name>@<version>/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

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_graph_packs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading