Skip to content

Commit b5a76b3

Browse files
committed
fix(analyze): corrected checking if a resource is already imported
fixes: #353 #340
1 parent e69763c commit b5a76b3

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/core/src/robotcode/core/utils/path.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def normalized_path(path: "Union[str, os.PathLike[str]]") -> Path:
3939
return Path(p)
4040

4141

42-
def normalized_path_full(path: Union[str, "os.PathLike[Any]"]) -> Path:
42+
def normalized_path_full(path: Union[str, "os.PathLike[str]"]) -> Path:
4343
p = normalized_path(path)
4444

4545
orig_parents = list(reversed(p.parents))
@@ -59,3 +59,10 @@ def normalized_path_full(path: Union[str, "os.PathLike[Any]"]) -> Path:
5959
return Path(*parents, *[f.name for f in orig_parents[index:]])
6060

6161
return Path(*parents)
62+
63+
64+
def same_file(path1: Union[str, "os.PathLike[str]", Path], path2: Union[str, "os.PathLike[str]", Path]) -> bool:
65+
try:
66+
return os.path.samefile(path1, path2)
67+
except OSError:
68+
return False

packages/robot/src/robotcode/robot/diagnostics/namespace.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from robotcode.core.text_document import TextDocument
4545
from robotcode.core.uri import Uri
4646
from robotcode.core.utils.logging import LoggingDescriptor
47+
from robotcode.core.utils.path import same_file
4748

4849
from ..utils.ast import (
4950
range_from_node,
@@ -1242,8 +1243,37 @@ def _import(
12421243

12431244
source = self.imports_manager.find_resource(value.name, base_dir, variables=variables)
12441245

1245-
if source in self._resources_files:
1246+
allread_imported_resource = next(
1247+
(
1248+
v
1249+
for k, v in self._resources.items()
1250+
if v.library_doc.source is not None and same_file(v.library_doc.source, source)
1251+
),
1252+
None,
1253+
)
1254+
if allread_imported_resource is not None:
12461255
self._logger.debug(lambda: f"Resource '{value.name}' already imported.", context_name="import")
1256+
if top_level:
1257+
self.append_diagnostics(
1258+
range=value.range,
1259+
message=f"Resource '{value.name}' already imported.",
1260+
severity=DiagnosticSeverity.INFORMATION,
1261+
source=DIAGNOSTICS_SOURCE_NAME,
1262+
related_information=(
1263+
[
1264+
DiagnosticRelatedInformation(
1265+
location=Location(
1266+
uri=str(Uri.from_path(allread_imported_resource.import_source)),
1267+
range=allread_imported_resource.import_range,
1268+
),
1269+
message="",
1270+
)
1271+
]
1272+
if allread_imported_resource.import_source
1273+
else None
1274+
),
1275+
code=Error.RESOURCE_ALREADY_IMPORTED,
1276+
)
12471277
return None
12481278

12491279
if self.source == source:

0 commit comments

Comments
 (0)