Skip to content

Commit 13bf36b

Browse files
committed
fix(robot): fix ${CURDIR} replacement in variable values and unify handling
Extract replace_curdir_in_variable_values() helper into utils/variables.py to deduplicate the ${CURDIR} substitution logic from _MyResourceBuilder and NamespaceAnalyzer. Fixes incorrect double-backslash escaping in _MyResourceBuilder that caused ${CURDIR} resolution to fail on Windows. Closes #589
1 parent 34bd2a8 commit 13bf36b

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
from ..utils.markdownformatter import MarkDownFormatter
8080
from ..utils.match import normalize, normalize_namespace
8181
from ..utils.robot_patching import patch_variable_not_found
82-
from ..utils.variables import contains_variable, search_variable
82+
from ..utils.variables import contains_variable, replace_curdir_in_variable_values, search_variable
8383
from .entities import (
8484
ArgumentDefinition,
8585
Import,
@@ -3199,13 +3199,7 @@ def visit_Variable(self, node: Statement) -> None: # noqa: N802
31993199

32003200
values = node.get_values(Token.ARGUMENT)
32013201
has_value = bool(values)
3202-
value = tuple(
3203-
s.replace(
3204-
"${CURDIR}",
3205-
str(Path(self.source).parent).replace("\\\\", "\\\\\\\\"),
3206-
)
3207-
for s in values
3208-
)
3202+
value = replace_curdir_in_variable_values(values, self.source)
32093203

32103204
stripped_name_token = strip_variable_token(name_token, matcher=matcher, parse_type=True)
32113205

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from concurrent.futures import CancelledError
88
from dataclasses import dataclass
99
from io import StringIO
10-
from pathlib import Path
1110
from tokenize import TokenError, generate_tokens
1211
from typing import Any, Callable, Dict, Iterator, List, Optional, Set, Tuple, Union, cast
1312

@@ -63,6 +62,7 @@
6362
InvalidVariableError,
6463
VariableMatcher,
6564
contains_variable,
65+
replace_curdir_in_variable_values,
6666
search_variable,
6767
split_from_equals,
6868
)
@@ -283,13 +283,7 @@ def _visit_Variable(self, node: Variable) -> None: # noqa: N802
283283

284284
values = node.get_values(Token.ARGUMENT)
285285
has_value = bool(values)
286-
value = tuple(
287-
s.replace(
288-
"${CURDIR}",
289-
str(Path(self._source).parent).replace("\\", "\\\\"),
290-
)
291-
for s in values
292-
)
286+
value = replace_curdir_in_variable_values(values, self._source)
293287

294288
var_def = VariableDefinition(
295289
name=name,

packages/robot/src/robotcode/robot/utils/variables.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
2-
from typing import Any, Optional, Tuple, cast
2+
from pathlib import Path
3+
from typing import Any, Optional, Sequence, Tuple, cast
34

45
from robot.utils.escaping import split_from_equals as robot_split_from_equals
56
from robot.variables.search import contains_variable as robot_contains_variable
@@ -225,3 +226,9 @@ def search_variable(
225226
@functools.lru_cache(maxsize=1024)
226227
def split_from_equals(string: str) -> Tuple[str, Optional[str]]:
227228
return cast(Tuple[str, Optional[str]], robot_split_from_equals(string))
229+
230+
231+
def replace_curdir_in_variable_values(values: Sequence[str], source: str) -> Tuple[str, ...]:
232+
"""Replace ${CURDIR} in variable values with the escaped parent directory of source."""
233+
curdir = str(Path(source).parent).replace("\\", "\\\\")
234+
return tuple(s.replace("${CURDIR}", curdir) for s in values)

0 commit comments

Comments
 (0)