Skip to content

Commit f6a51b7

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

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

src/vectorcode/lsp_main.py

+26-20
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,30 @@ async def execute_command(ls: LanguageServer, args: list[str]):
7575
)
7676
return
7777
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
78+
if DEFAULT_PROJECT_ROOT is not None:
79+
parsed_args.project_root = DEFAULT_PROJECT_ROOT
8380
elif DEFAULT_PROJECT_ROOT is None:
8481
DEFAULT_PROJECT_ROOT = str(parsed_args.project_root)
8582

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
83+
if parsed_args.project_root is not None:
84+
parsed_args.project_root = os.path.abspath(str(parsed_args.project_root))
85+
await make_caches(parsed_args.project_root)
86+
final_configs = await cached_project_configs[
87+
parsed_args.project_root
88+
].merge_from(parsed_args)
89+
final_configs.pipe = True
90+
client = await get_client(final_configs)
91+
collection = await get_collection(
92+
client=client,
93+
configs=final_configs,
94+
make_if_missing=final_configs.action in {CliAction.vectorise},
95+
)
96+
else:
97+
final_configs = parsed_args
98+
client = await get_client(parsed_args)
99+
collection = None
92100
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-
)
101+
99102
await ls.progress.create_async(progress_token)
100103
match final_configs.action:
101104
case CliAction.query:
@@ -108,9 +111,12 @@ async def execute_command(ls: LanguageServer, args: list[str]):
108111
)
109112
final_results = []
110113
try:
111-
final_results.extend(
112-
await build_query_results(collection, final_configs)
113-
)
114+
if collection is None:
115+
print("Please specify a project to search in.", file=sys.stderr)
116+
else:
117+
final_results.extend(
118+
await build_query_results(collection, final_configs)
119+
)
114120
finally:
115121
ls.progress.end(
116122
progress_token,

tests/test_lsp.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,14 @@ 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+
patch("vectorcode.lsp_main.get_client", new_callable=AsyncMock),
274+
):
271275
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)
276+
await execute_command(mock_language_server, ["query", "test"])
277+
stderr.assert_called()
275278
DEFAULT_PROJECT_ROOT = None # Reset the global variable

0 commit comments

Comments
 (0)