Skip to content

Commit 81928e3

Browse files
committed
feat(cli): hide $HOME when printing paths as output
1 parent 8050123 commit 81928e3

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

src/vectorcode/cli_utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,12 @@ async def expand_globs(
508508
return list(result)
509509

510510

511+
def cleanup_path(path: str):
512+
if os.path.isabs(path) and os.environ.get("HOME") is not None:
513+
return path.replace(os.environ["HOME"], "~")
514+
return path
515+
516+
511517
def config_logging(
512518
name: str = "vectorcode", stdio: bool = True, log_file: bool = False
513519
): # pragma: nocover

src/vectorcode/mcp_main.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from vectorcode.cli_utils import (
2525
Config,
26+
cleanup_path,
2627
config_logging,
2728
find_project_config_dir,
2829
get_project_config,
@@ -76,7 +77,7 @@ async def list_collections() -> list[str]:
7677
client = await get_client(await load_config_file())
7778
async for col in get_collections(client):
7879
if col.metadata is not None:
79-
names.append(str(col.metadata.get("path")))
80+
names.append(cleanup_path(str(col.metadata.get("path"))))
8081
logger.info("Retrieved the following collections: %s", names)
8182
return names
8283

@@ -92,6 +93,7 @@ async def query_tool(
9293
logger.info(
9394
f"query tool called with the following args: {n_query=}, {query_messages=}, {project_root=}"
9495
)
96+
project_root = os.path.expanduser(project_root)
9597
if not os.path.isdir(project_root):
9698
logger.error("Invalid project root: %s", project_root)
9799
raise McpError(

src/vectorcode/subcommands/ls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from chromadb.api import AsyncClientAPI
88
from chromadb.api.types import IncludeEnum
99

10-
from vectorcode.cli_utils import Config
10+
from vectorcode.cli_utils import Config, cleanup_path
1111
from vectorcode.common import get_client, get_collections
1212

1313
logger = logging.getLogger(name=__name__)
@@ -23,7 +23,7 @@ async def get_collection_list(client: AsyncClientAPI) -> list[dict]:
2323
)
2424
result.append(
2525
{
26-
"project-root": meta["path"],
26+
"project-root": cleanup_path(meta["path"]),
2727
"user": meta.get("username"),
2828
"hostname": socket.gethostname(),
2929
"collection_name": collection.name,

src/vectorcode/subcommands/query/__init__.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
from chromadb.errors import InvalidCollectionException, InvalidDimensionException
99

1010
from vectorcode.chunking import StringChunker
11-
from vectorcode.cli_utils import Config, QueryInclude, expand_globs, expand_path
11+
from vectorcode.cli_utils import (
12+
Config,
13+
QueryInclude,
14+
cleanup_path,
15+
expand_globs,
16+
expand_path,
17+
)
1218
from vectorcode.common import (
1319
get_client,
1420
get_collection,
@@ -130,6 +136,9 @@ async def build_query_results(
130136
logger.warning(
131137
f"{identifier} is no longer a valid file! Please re-run vectorcode vectorise to refresh the database.",
132138
)
139+
for result in structured_result:
140+
if result.get("path") is not None:
141+
result["path"] = cleanup_path(result["path"])
133142
return structured_result
134143

135144

tests/test_cli_utils.py

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
CliAction,
1111
Config,
1212
QueryInclude,
13+
cleanup_path,
1314
expand_envs_in_dict,
1415
expand_globs,
1516
expand_path,
@@ -489,3 +490,13 @@ async def test_hnsw_config_merge():
489490
assert merged_config.port == 8001
490491
assert merged_config.hnsw["space"] == "ip"
491492
assert merged_config.hnsw["ef_construction"] == 200
493+
494+
495+
def test_cleanup_path():
496+
home = os.environ.get("HOME")
497+
if home is None:
498+
return
499+
assert cleanup_path(os.path.join(home, "test_path")) == os.path.join(
500+
"~", "test_path"
501+
)
502+
assert cleanup_path("/etc/dir") == "/etc/dir"

0 commit comments

Comments
 (0)