Skip to content

Commit 55b3c0e

Browse files
committed
fix(cli): allow lsp to be started outside of a project root
1 parent 4c84043 commit 55b3c0e

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

src/vectorcode/lsp_main.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
from ast import parse
23
import asyncio
34
import os
45
import sys
@@ -75,27 +76,30 @@ async def execute_command(ls: LanguageServer, args: list[str]):
7576
)
7677
return
7778
if parsed_args.project_root is None:
78-
assert DEFAULT_PROJECT_ROOT is not None, (
79-
"Failed to automatically resolve project root!"
80-
)
81-
82-
parsed_args.project_root = DEFAULT_PROJECT_ROOT
79+
if DEFAULT_PROJECT_ROOT is not None:
80+
parsed_args.project_root = DEFAULT_PROJECT_ROOT
8381
elif DEFAULT_PROJECT_ROOT is None:
8482
DEFAULT_PROJECT_ROOT = str(parsed_args.project_root)
8583

86-
parsed_args.project_root = os.path.abspath(str(parsed_args.project_root))
87-
await make_caches(parsed_args.project_root)
88-
final_configs = await cached_project_configs[parsed_args.project_root].merge_from(
89-
parsed_args
90-
)
91-
final_configs.pipe = True
84+
if parsed_args.project_root is not None:
85+
parsed_args.project_root = os.path.abspath(str(parsed_args.project_root))
86+
await make_caches(parsed_args.project_root)
87+
final_configs = await cached_project_configs[
88+
parsed_args.project_root
89+
].merge_from(parsed_args)
90+
final_configs.pipe = True
91+
client = await get_client(final_configs)
92+
collection = await get_collection(
93+
client=client,
94+
configs=final_configs,
95+
make_if_missing=final_configs.action in {CliAction.vectorise},
96+
)
97+
else:
98+
final_configs = parsed_args
99+
client = await get_client(parsed_args)
100+
collection = None
92101
progress_token = str(uuid.uuid4())
93-
client = await get_client(final_configs)
94-
collection = await get_collection(
95-
client=client,
96-
configs=final_configs,
97-
make_if_missing=final_configs.action in {CliAction.vectorise},
98-
)
102+
99103
await ls.progress.create_async(progress_token)
100104
match final_configs.action:
101105
case CliAction.query:
@@ -108,9 +112,12 @@ async def execute_command(ls: LanguageServer, args: list[str]):
108112
)
109113
final_results = []
110114
try:
111-
final_results.extend(
112-
await build_query_results(collection, final_configs)
113-
)
115+
if collection is None:
116+
print("Please specify a project to search in.", file=sys.stderr)
117+
else:
118+
final_results.extend(
119+
await build_query_results(collection, final_configs)
120+
)
114121
finally:
115122
ls.progress.end(
116123
progress_token,

tests/test_lsp.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,13 @@ async def test_execute_command_no_default_project_root(
265265
global DEFAULT_PROJECT_ROOT
266266
DEFAULT_PROJECT_ROOT = None
267267
mock_config.project_root = None
268-
with patch(
269-
"vectorcode.lsp_main.parse_cli_args", new_callable=AsyncMock
270-
) as mock_parse_cli_args:
268+
with (
269+
patch(
270+
"vectorcode.lsp_main.parse_cli_args", new_callable=AsyncMock
271+
) as mock_parse_cli_args,
272+
patch("sys.stderr.write") as stderr,
273+
):
271274
mock_parse_cli_args.return_value = mock_config
272-
with pytest.raises(AssertionError) as excinfo:
273-
await execute_command(mock_language_server, ["query", "test"])
274-
assert "Failed to automatically resolve project root!" in str(excinfo.value)
275+
await execute_command(mock_language_server, ["query", "test"])
276+
stderr.assert_called()
275277
DEFAULT_PROJECT_ROOT = None # Reset the global variable

0 commit comments

Comments
 (0)