Skip to content

Commit 14b2bae

Browse files
committed
Move rereading project config and collection actions in start_runners_with_presets, because these runners are always started only for this purpose
1 parent df8e57b commit 14b2bae

File tree

9 files changed

+59
-66
lines changed

9 files changed

+59
-66
lines changed

src/finecode/cli_app/dump_config.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,6 @@ async def dump_config(workdir_path: pathlib.Path, project_name: str):
6161
f"Starting runners with presets failed: {exception.message}"
6262
)
6363

64-
try:
65-
await read_configs.read_project_config(
66-
project=project, ws_context=ws_context
67-
)
68-
collect_actions.collect_actions(
69-
project_path=project.dir_path, ws_context=ws_context
70-
)
71-
except config_models.ConfigurationError as exception:
72-
raise DumpFailed(
73-
f"Rereading project config with presets and collecting actions in {project.dir_path} failed: {exception.message}"
74-
)
75-
7664
try:
7765
await proxy_utils.start_required_environments(
7866
actions_by_projects, ws_context

src/finecode/cli_app/prepare_envs.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,6 @@ async def prepare_envs(workdir_path: pathlib.Path, recreate: bool) -> None:
8484
f"Starting runners with presets failed: {exception.message}"
8585
)
8686

87-
for project in projects:
88-
try:
89-
await read_configs.read_project_config(
90-
project=project, ws_context=ws_context
91-
)
92-
collect_actions.collect_actions(
93-
project_path=project.dir_path, ws_context=ws_context
94-
)
95-
except config_models.ConfigurationError as exception:
96-
raise PrepareEnvsFailed(
97-
f"Rereading project config with presets and collecting actions in {project.dir_path} failed: {exception.message}"
98-
)
99-
10087
# now all 'dev_workspace' envs are valid, run 'prepare_runners' in them to create
10188
# venvs and install runners and presets in them
10289
actions_by_projects: dict[pathlib.Path, list[str]] = {

src/finecode/cli_app/run.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,6 @@ async def run_actions(
125125
logger.error("Unexpected exception:")
126126
logger.exception(exception)
127127

128-
# 2. Collect actions in relevant projects
129-
for project in projects:
130-
try:
131-
await read_configs.read_project_config(
132-
project=project, ws_context=ws_context
133-
)
134-
collect_actions.collect_actions(
135-
project_path=project.dir_path, ws_context=ws_context
136-
)
137-
except config_models.ConfigurationError as exception:
138-
raise RunFailed(f"Found configuration problem: {exception.message}")
139-
140128
actions_by_projects: dict[pathlib.Path, list[str]] = {}
141129
if projects_names is not None:
142130
# check that all projects have all actions to detect problem and provide

src/finecode/find_project.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from finecode import domain
66
from finecode.context import WorkspaceContext
7+
from finecode.runner import manager as runner_manager
78

89

910
class FileNotInWorkspaceException(BaseException): ...
@@ -12,7 +13,7 @@ class FileNotInWorkspaceException(BaseException): ...
1213
class FileHasNotActionException(BaseException): ...
1314

1415

15-
def find_project_with_action_for_file(
16+
async def find_project_with_action_for_file(
1617
file_path: Path,
1718
action_name: str,
1819
ws_context: WorkspaceContext,
@@ -70,10 +71,22 @@ def find_project_with_action_for_file(
7071
if project.status == domain.ProjectStatus.NO_FINECODE:
7172
continue
7273
else:
73-
raise ValueError(
74-
f"Action is related to project {project_dir_path} but its action "
75-
f"cannot be resolved({project.status})"
76-
)
74+
if project.status == domain.ProjectStatus.CONFIG_VALID:
75+
try:
76+
await runner_manager.get_or_start_runners_with_presets(project_dir_path=project_dir_path, ws_context=ws_context)
77+
except runner_manager.RunnerFailedToStart as exception:
78+
raise ValueError(
79+
f"Action is related to project {project_dir_path} but runner "
80+
f"with presets failed to start in it: {exception.message}"
81+
)
82+
83+
assert project.actions is not None
84+
project_actions = project.actions
85+
else:
86+
raise ValueError(
87+
f"Action is related to project {project_dir_path} but its action "
88+
f"cannot be resolved({project.status})"
89+
)
7790

7891
try:
7992
next(action for action in project_actions if action.name == action_name)

src/finecode/lsp_server/endpoints/document_sync.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async def document_did_open(
2424
projects_paths = [
2525
project_path
2626
for project_path, project in global_state.ws_context.ws_projects.items()
27-
if project.status == domain.ProjectStatus.RUNNING
27+
if project.status == domain.ProjectStatus.CONFIG_VALID
2828
and file_path.is_relative_to(project_path)
2929
]
3030

@@ -34,17 +34,17 @@ async def document_did_open(
3434
try:
3535
async with asyncio.TaskGroup() as tg:
3636
for project_path in projects_paths:
37-
runners_by_env = global_state.ws_context.ws_projects_extension_runners[
38-
project_path
39-
]
37+
runners_by_env = global_state.ws_context.ws_projects_extension_runners.get(project_path, {})
4038
for runner in runners_by_env.values():
4139
tg.create_task(
4240
runner_client.notify_document_did_open(
4341
runner=runner, document_info=document_info
4442
)
4543
)
46-
except ExceptionGroup as e:
47-
logger.error(f"Error while sending opened document: {e}")
44+
except ExceptionGroup as eg:
45+
for exception in eg.exceptions:
46+
logger.exception(exception)
47+
logger.error(f"Error while sending opened document: {eg}")
4848

4949

5050
async def document_did_close(

src/finecode/lsp_server/services.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,6 @@ async def add_workspace_dir(
7878
except runner_manager.RunnerFailedToStart as exception:
7979
raise ValueError(f"Starting runners with presets failed: {exception.message}")
8080

81-
for project in new_projects:
82-
try:
83-
await read_configs.read_project_config(
84-
project=project, ws_context=global_state.ws_context
85-
)
86-
collect_actions.collect_actions(
87-
project_path=project.dir_path, ws_context=global_state.ws_context
88-
)
89-
except config_models.ConfigurationError as exception:
90-
raise ValueError(
91-
f"Rereading project config with presets and collecting actions in {project.dir_path} failed: {exception.message}"
92-
)
93-
9481
return schemas.AddWorkspaceDirResponse()
9582

9683

src/finecode/proxy_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
from finecode.services import ActionRunFailed
1515

1616

17-
def find_action_project(
17+
async def find_action_project(
1818
file_path: pathlib.Path, action_name: str, ws_context: context.WorkspaceContext
1919
) -> pathlib.Path:
2020
try:
21-
project_path = find_project.find_project_with_action_for_file(
21+
project_path = await find_project.find_project_with_action_for_file(
2222
file_path=file_path,
2323
action_name=action_name,
2424
ws_context=ws_context,
@@ -51,7 +51,7 @@ async def find_action_project_and_run(
5151
params: dict[str, Any],
5252
ws_context: context.WorkspaceContext,
5353
) -> runner_client.RunActionResponse:
54-
project_path = find_action_project(
54+
project_path = await find_action_project(
5555
file_path=file_path, action_name=action_name, ws_context=ws_context
5656
)
5757
project = ws_context.ws_projects[project_path]
@@ -227,7 +227,7 @@ async def find_action_project_and_run_with_partial_results(
227227
ws_context: context.WorkspaceContext,
228228
) -> collections.abc.AsyncIterator[runner_client.RunActionRawResult]:
229229
logger.trace(f"Run {action_name} on {file_path}")
230-
project_path = find_action_project(
230+
project_path = await find_action_project(
231231
file_path=file_path, action_name=action_name, ws_context=ws_context
232232
)
233233
return run_with_partial_results(

src/finecode/runner/manager.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def stop_extension_runner_sync(runner: runner_info.ExtensionRunnerInfo) -> None:
202202
async def start_runners_with_presets(
203203
projects: list[domain.Project], ws_context: context.WorkspaceContext
204204
) -> None:
205+
# start runners with presets in projects, resolve presets and read project actions
205206
new_runners_tasks: list[asyncio.Task] = []
206207
try:
207208
# first start runner in 'dev_workspace' env to be able to resolve presets for
@@ -236,6 +237,35 @@ async def start_runners_with_presets(
236237
raise RunnerFailedToStart(
237238
"Failed to initialize runner(s). See previous logs for more details"
238239
)
240+
241+
for project in projects:
242+
try:
243+
await read_configs.read_project_config(
244+
project=project, ws_context=ws_context
245+
)
246+
collect_actions.collect_actions(
247+
project_path=project.dir_path, ws_context=ws_context
248+
)
249+
except config_models.ConfigurationError as exception:
250+
raise RunnerFailedToStart(
251+
f"Reading project config with presets and collecting actions in {project.dir_path} failed: {exception.message}"
252+
)
253+
254+
255+
async def get_or_start_runners_with_presets(project_dir_path: Path, ws_context: context.WorkspaceContext) -> runner_info.ExtensionRunnerInfo:
256+
# project is expected to have status `ProjectStatus.CONFIG_VALID`
257+
has_dev_workspace_runner = 'dev_workspace' in ws_context.ws_projects_extension_runners[project_dir_path]
258+
if not has_dev_workspace_runner:
259+
project = ws_context.ws_projects[project_dir_path]
260+
await start_runners_with_presets([project], ws_context)
261+
dev_workspace_runner = ws_context.ws_projects_extension_runners[project_dir_path]['dev_workspace']
262+
if dev_workspace_runner.status == runner_info.RunnerStatus.RUNNING:
263+
return dev_workspace_runner
264+
elif dev_workspace_runner.status == runner_info.RunnerStatus.INITIALIZING:
265+
await dev_workspace_runner.initialized_event.wait()
266+
return dev_workspace_runner
267+
else:
268+
raise RunnerFailedToStart(f'Status of dev_workspace runner: {dev_workspace_runner.status}, logs: {dev_workspace_runner.logs_path}')
239269

240270

241271
async def start_runner(

src/finecode/watch_and_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def watch_and_run(
3131
# and lint?
3232
for action in ["lint", "format"]:
3333
# TODO: this can be cached
34-
project_root = find_project.find_project_with_action_for_file(
34+
project_root = await find_project.find_project_with_action_for_file(
3535
file_path=path_to_apply_on,
3636
action_name=action,
3737
ws_context=ws_context,

0 commit comments

Comments
 (0)