Skip to content

Commit 15d701a

Browse files
committed
Propagate import errors from resources
1 parent 9bc1a3e commit 15d701a

File tree

6 files changed

+39
-18
lines changed

6 files changed

+39
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ All notable changes to the "robotcode" extension will be documented in this file
2929
- fixes: [#28](https://github.com/d-biehl/robotcode/issues/28)
3030
- Ignoring robotcode diagnostics
3131
- you can put a line comment to disable robotcode diagnostics (i.e errors or warnings) for a single line, like this:
32+
- Propagate import errors from resources
33+
- errors like: `Resource file with 'Test Cases' section is invalid` are shown at import statement
3234

3335
```robotcode
3436
*** Test cases ***

robotcode/language_server/robotframework/configuration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class RobotConfig(ConfigBase):
2020
env: Dict[str, str] = field(default_factory=dict)
2121
variables: Dict[str, Any] = field(default_factory=dict)
2222
output_dir: Optional[str] = None
23+
output_file: Optional[str] = None
24+
log_file: Optional[str] = None
25+
debug_file: Optional[str] = None
26+
log_level: Optional[str] = None
2327

2428

2529
@config_section("robotcode.syntax")

robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
final,
2424
)
2525

26-
from robotcode.language_server.robotframework.utils.robot_path import find_file_ex
27-
2826
from ....utils.async_tools import Lock, async_tasking_event, create_sub_task
2927
from ....utils.logging import LoggingDescriptor
3028
from ....utils.path import path_is_relative_to
@@ -35,6 +33,7 @@
3533
from ...common.text_document import TextDocument
3634
from ..configuration import RobotConfig
3735
from ..utils.async_ast import walk
36+
from ..utils.robot_path import find_file_ex
3837
from ..utils.version import get_robot_version
3938
from .entities import CommandLineVariableDefinition, VariableDefinition
4039

robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,14 +1090,18 @@ def _find_library_internal(
10901090
variables: Optional[Dict[str, Optional[Any]]] = None,
10911091
) -> Tuple[str, Any]:
10921092

1093+
from robot.errors import DataError
10931094
from robot.libraries import STDLIBS
10941095
from robot.utils.robotpath import find_file as robot_find_file
10951096

10961097
_update_env(working_dir, pythonpath, environment)
10971098

10981099
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
10991100

1100-
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=True)
1101+
try:
1102+
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=False)
1103+
except DataError as error:
1104+
raise DataError(f"Replacing variables from setting 'Library' failed: {error}")
11011105

11021106
if name in STDLIBS:
11031107
result = ROBOT_LIBRARY_PACKAGE + "." + name
@@ -1377,14 +1381,17 @@ def _find_variables_internal(
13771381
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
13781382
variables: Optional[Dict[str, Optional[Any]]] = None,
13791383
) -> Tuple[str, Any]:
1380-
1384+
from robot.errors import DataError
13811385
from robot.utils.robotpath import find_file as robot_find_file
13821386

13831387
_update_env(working_dir, pythonpath, environment)
13841388

13851389
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
13861390

1387-
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=True)
1391+
try:
1392+
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=False)
1393+
except DataError as error:
1394+
raise DataError(f"Replacing variables from setting 'Variables' failed: {error}")
13881395

13891396
result = name
13901397

@@ -1523,13 +1530,16 @@ def find_file(
15231530
variables: Optional[Dict[str, Optional[Any]]] = None,
15241531
file_type: str = "Resource",
15251532
) -> str:
1533+
from robot.errors import DataError
15261534
from robot.utils.robotpath import find_file as robot_find_file
15271535

15281536
_update_env(working_dir, pythonpath, environment)
15291537

15301538
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
1531-
1532-
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=True)
1539+
try:
1540+
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=False)
1541+
except DataError as error:
1542+
raise DataError(f"Replacing variables from setting '{file_type}' failed: {error}")
15331543

15341544
return cast(str, robot_find_file(name, base_dir or ".", file_type))
15351545

robotcode/language_server/robotframework/diagnostics/namespace.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ast
44
import asyncio
5+
import enum
56
import itertools
67
import re
78
import time
@@ -445,6 +446,13 @@ class VariablesEntry(LibraryEntry):
445446
variables: List[VariableDefinition] = field(default_factory=lambda: [])
446447

447448

449+
class DocumentType(enum.Enum):
450+
UNKNOWN = "unknown"
451+
GENERAL = "robot"
452+
RESOURCE = "resource"
453+
INIT = "init"
454+
455+
448456
class Namespace:
449457
_logger = LoggingDescriptor()
450458

@@ -456,6 +464,7 @@ def __init__(
456464
source: str,
457465
invalidated_callback: Callable[[Namespace], None],
458466
document: Optional[TextDocument] = None,
467+
document_type: Optional[DocumentType] = None,
459468
) -> None:
460469
super().__init__()
461470

@@ -467,6 +476,7 @@ def __init__(
467476
self.source = source
468477
self.invalidated_callback = invalidated_callback
469478
self._document = weakref.ref(document) if document is not None else None
479+
self.document_type: Optional[DocumentType] = document_type
470480
self._libraries: OrderedDict[str, LibraryEntry] = OrderedDict()
471481
self._libraries_matchers: Optional[Dict[KeywordMatcher, LibraryEntry]] = None
472482
self._resources: OrderedDict[str, ResourceEntry] = OrderedDict()
@@ -587,7 +597,11 @@ async def get_library_doc(self) -> LibraryDoc:
587597
async with self._library_doc_lock:
588598
if self._library_doc is None:
589599
self._library_doc = await self.imports_manager.get_libdoc_from_model(
590-
self.model, self.source, model_type="RESOURCE", append_model_errors=False
600+
self.model,
601+
self.source,
602+
model_type="RESOURCE",
603+
append_model_errors=self.document_type is not None
604+
and self.document_type in [DocumentType.RESOURCE],
591605
)
592606

593607
return self._library_doc

robotcode/language_server/robotframework/parts/documents_cache.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import ast
4-
import enum
54
import io
65
import weakref
76
from typing import (
@@ -27,7 +26,7 @@
2726
from ...common.text_document import TextDocument
2827
from ..configuration import RobotConfig
2928
from ..diagnostics.imports_manager import ImportsManager
30-
from ..diagnostics.namespace import Namespace
29+
from ..diagnostics.namespace import DocumentType, Namespace
3130
from ..utils.ast import Token
3231

3332
if TYPE_CHECKING:
@@ -40,13 +39,6 @@ class UnknownFileTypeError(Exception):
4039
pass
4140

4241

43-
class DocumentType(enum.Enum):
44-
UNKNOWN = "unknown"
45-
GENERAL = "robot"
46-
RESOURCE = "resource"
47-
INIT = "init"
48-
49-
5042
class DocumentsCache(RobotLanguageServerProtocolPart):
5143
def __init__(self, parent: RobotLanguageServerProtocol) -> None:
5244
super().__init__(parent)
@@ -233,7 +225,7 @@ async def __get_namespace_for_document_type(
233225
def invalidate(namespace: Namespace) -> None:
234226
create_sub_task(self.__invalidate_namespace(namespace))
235227

236-
return Namespace(imports_manager, model, str(document.uri.to_path()), invalidate, document)
228+
return Namespace(imports_manager, model, str(document.uri.to_path()), invalidate, document, document_type)
237229

238230
@property
239231
async def default_imports_manager(self) -> ImportsManager:

0 commit comments

Comments
 (0)