Skip to content

Commit 4183391

Browse files
committed
fix: set env and pythonpath erlier in lifecycle to prevent that sometime analyses fails because of python path is not correct
1 parent 593f546 commit 4183391

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

robotcode/language_server/common/parts/documents.py

+2
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ async def _text_document_did_open(self, text_document: TextDocumentItem, *args:
271271
text_changed = document.text() != normalized_text
272272
if text_changed:
273273
document.apply_full_change(text_document.version, normalized_text)
274+
else:
275+
document.version = text_document.version
274276

275277
document.opened_in_editor = True
276278

robotcode/language_server/common/protocol.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
rpc_method,
1313
)
1414
from ...jsonrpc2.server import JsonRPCServer
15-
from ...utils.async_tools import async_event
15+
from ...utils.async_tools import Event, async_event
1616
from ...utils.logging import LoggingDescriptor
1717
from .has_extend_capabilities import HasExtendCapabilities
1818
from .lsp_types import (
@@ -132,7 +132,7 @@ def __init__(self, server: JsonRPCServer[Any]):
132132
)
133133

134134
self._trace = TraceValue.OFF
135-
self._is_initialized = False
135+
self.is_initialized = Event()
136136

137137
@async_event
138138
async def on_shutdown(sender) -> None: # pragma: no cover, NOSONAR
@@ -244,11 +244,7 @@ async def on_initialize(sender, initialization_options: Optional[Any] = None) ->
244244
async def _initialized(self, params: InitializedParams, *args: Any, **kwargs: Any) -> None:
245245
await self.on_initialized(self)
246246

247-
self._is_initialized = True
248-
249-
@property
250-
def is_initialized(self) -> bool:
251-
return self._is_initialized
247+
self.is_initialized.set()
252248

253249
@async_event
254250
async def on_initialized(sender) -> None: # pragma: no cover, NOSONAR

robotcode/language_server/robotframework/protocol.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
from dataclasses import dataclass
3+
from dataclasses import dataclass, field
44
from pathlib import Path
55
from typing import TYPE_CHECKING, Any, Dict, List, Optional
66

@@ -73,6 +73,8 @@ def check_robotframework() -> None:
7373
class Options(Model):
7474
storage_uri: Optional[str] = None
7575
global_storage_uri: Optional[str] = None
76+
python_path: List[str] = field(default_factory=list)
77+
env: Dict[str, str] = field(default_factory=dict)
7678

7779

7880
@symbol_information_label("robotframework")
@@ -137,16 +139,31 @@ async def _on_shutdown(self, sender: Any) -> None:
137139

138140
@_logger.call
139141
async def _on_initialize(self, sender: Any, initialization_options: Optional[Any] = None) -> None:
142+
if initialization_options is not None:
143+
self.options = from_dict(initialization_options, Options)
144+
145+
if self.options.env:
146+
for k, v in self.options.env.items():
147+
os.environ[k] = v
148+
149+
if self.options.python_path:
150+
for folder in self.workspace.workspace_folders:
151+
for p in self.options.python_path:
152+
pa = Path(p)
153+
if not pa.is_absolute():
154+
pa = Path(folder.uri.to_path(), pa)
155+
156+
absolute_path = str(pa.absolute())
157+
if absolute_path not in sys.path:
158+
sys.path.insert(0, absolute_path)
159+
140160
try:
141161
check_robotframework()
142162
except RobotCodeException as e:
143163
raise JsonRPCErrorException(
144164
JsonRPCErrors.INTERNAL_ERROR, f"Can't start language server: {e}", InitializeError(retry=False)
145165
) from e
146166

147-
if initialization_options is not None:
148-
self.options = from_dict(initialization_options, Options)
149-
150167
self.workspace.did_change_configuration.add(self._on_did_change_configuration)
151168

152169
self._logger.critical(f"initialized with {repr(self.options)}")

vscode-client/languageclientsmanger.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -352,20 +352,16 @@ export class LanguageClientsManager {
352352
documentSelector:
353353
// TODO: use SUPPORTED_LANGUAGES here
354354
vscode.workspace.workspaceFolders?.length === 1
355-
? [
356-
{ scheme: "file", language: "robotframework" },
357-
{ scheme: "file", language: "feature" },
358-
]
359-
: [
360-
{ scheme: "file", language: "robotframework", pattern: `${workspaceFolder.uri.fsPath}/**/*` },
361-
{ scheme: "file", language: "feature", pattern: `${workspaceFolder.uri.fsPath}/**/*` },
362-
],
363-
synchronize: {
364-
configurationSection: [CONFIG_SECTION],
365-
},
355+
? [{ scheme: "file", language: "robotframework" }]
356+
: [{ scheme: "file", language: "robotframework", pattern: `${workspaceFolder.uri.fsPath}/**/*` }],
357+
// synchronize: {
358+
// configurationSection: [CONFIG_SECTION],
359+
// },
366360
initializationOptions: {
367361
storageUri: this.extensionContext?.storageUri?.toString(),
368362
globalStorageUri: this.extensionContext?.globalStorageUri?.toString(),
363+
pythonPath: config.get<string[]>("robot.pythonPath", []),
364+
env: config.get<object>("robot.env", []),
369365
},
370366
revealOutputChannelOn: RevealOutputChannelOn.Never, // TODO: should we make this configurable?
371367
initializationFailedHandler: (error: ResponseError<InitializeError> | Error | undefined) => {

0 commit comments

Comments
 (0)