Skip to content

Commit 58971d5

Browse files
authored
feat: store relative path instead of absolute path (#28)
1 parent 199325b commit 58971d5

3 files changed

Lines changed: 12 additions & 9 deletions

File tree

src/cocoindex_code/indexer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from cocoindex.resources.file import PatternFilePathMatcher
88
from cocoindex.resources.id import IdGenerator
99

10-
from .config import config
11-
from .shared import SQLITE_DB, CodeChunk, embedder
10+
from .shared import CODEBASE_DIR, SQLITE_DB, CodeChunk, embedder
1211

1312
# File patterns for supported languages
1413
INCLUDED_PATTERNS = [
@@ -128,7 +127,7 @@ async def app_main() -> None:
128127

129128
# Walk source directory
130129
files = localfs.walk_dir(
131-
config.codebase_root_path,
130+
coco.use_context(CODEBASE_DIR),
132131
recursive=True,
133132
path_matcher=PatternFilePathMatcher(
134133
included_patterns=INCLUDED_PATTERNS,

src/cocoindex_code/shared.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import cocoindex as coco
1111
from cocoindex.connectors import sqlite
12+
from cocoindex.connectors.localfs import FilePath, register_base_dir
1213
from numpy.typing import NDArray
1314

1415
if TYPE_CHECKING:
@@ -54,6 +55,8 @@
5455

5556
# Context key for SQLite database (connection managed in lifespan)
5657
SQLITE_DB = coco.ContextKey[sqlite.SqliteDatabase]("sqlite_db")
58+
# Context key for codebase root directory (provided in lifespan)
59+
CODEBASE_DIR = coco.ContextKey[FilePath]("codebase_dir")
5760

5861

5962
@coco.lifespan
@@ -65,6 +68,9 @@ def coco_lifespan(builder: coco.EnvironmentBuilder) -> Iterator[None]:
6568
# Set CocoIndex state database path
6669
builder.settings.db_path = config.cocoindex_db_path
6770

71+
# Provide codebase root directory to environment
72+
builder.provide(CODEBASE_DIR, register_base_dir("codebase", config.codebase_root_path))
73+
6874
# Connect to SQLite with vector extension
6975
conn = sqlite.connect(str(config.target_sqlite_db_path), load_vec="auto")
7076
builder.provide(SQLITE_DB, sqlite.register_db("index_db", conn))

tests/test_e2e.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ async def test_filter_by_file_path_glob(
365365
setup_multi_lang_codebase(test_codebase_root)
366366
await app.update(report_to_stdout=False)
367367

368-
results = await query_codebase("function", limit=50, paths=["*/lib/*"])
368+
results = await query_codebase("function", limit=50, paths=["lib/*"])
369369
assert len(results) > 0
370-
assert all("/lib/" in r.file_path for r in results)
370+
assert all(r.file_path.startswith("lib/") for r in results)
371371

372372
@pytest.mark.asyncio(loop_scope="session")
373373
async def test_filter_by_file_path_wildcard_extension(
@@ -390,12 +390,10 @@ async def test_filter_by_both_language_and_file_path(
390390
await app.update(report_to_stdout=False)
391391

392392
# Filter for Python files under lib/
393-
results = await query_codebase(
394-
"function", limit=50, languages=["python"], paths=["*/lib/*"]
395-
)
393+
results = await query_codebase("function", limit=50, languages=["python"], paths=["lib/*"])
396394
assert len(results) > 0
397395
assert all(r.language == "python" for r in results)
398-
assert all("/lib/" in r.file_path for r in results)
396+
assert all(r.file_path.startswith("lib/") for r in results)
399397

400398
@pytest.mark.asyncio(loop_scope="session")
401399
async def test_no_filter_returns_all_languages(

0 commit comments

Comments
 (0)