Skip to content

Commit 12d7398

Browse files
committed
handle commandline parameters correctly in resolving variables
1 parent f1eabd7 commit 12d7398

File tree

3 files changed

+68
-28
lines changed

3 files changed

+68
-28
lines changed

robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ async def find_library(self, name: str, base_dir: str, variables: Optional[Dict[
628628
base_dir,
629629
self.config.python_path if self.config is not None else None,
630630
self.config.env if self.config is not None else None,
631-
variables if variables is not None else self.config.variables if self.config is not None else None,
631+
self.config.variables if self.config is not None else None,
632+
variables,
632633
),
633634
FIND_FILE_TIME_OUT,
634635
)
@@ -662,7 +663,8 @@ async def _get_libdoc() -> LibraryDoc:
662663
base_dir,
663664
self.config.python_path if self.config is not None else None,
664665
self.config.env if self.config is not None else None,
665-
variables if variables is not None else self.config.variables if self.config is not None else None,
666+
self.config.variables if self.config is not None else None,
667+
variables,
666668
),
667669
LOAD_LIBRARY_TIME_OUT,
668670
)
@@ -862,7 +864,8 @@ async def find_file(
862864
base_dir,
863865
self.config.python_path if self.config is not None else None,
864866
self.config.env if self.config is not None else None,
865-
variables if variables is not None else self.config.variables if self.config is not None else None,
867+
self.config.variables if self.config is not None else None,
868+
variables,
866869
file_type,
867870
),
868871
FIND_FILE_TIME_OUT,
@@ -946,7 +949,8 @@ async def complete_library_import(
946949
base_dir,
947950
self.config.python_path if self.config is not None else None,
948951
self.config.env if self.config is not None else None,
949-
variables if variables is not None else self.config.variables if self.config is not None else None,
952+
self.config.variables if self.config is not None else None,
953+
variables,
950954
),
951955
COMPLETE_LIBRARY_IMPORT_TIME_OUT,
952956
)
@@ -965,7 +969,8 @@ async def complete_resource_import(
965969
base_dir,
966970
self.config.python_path if self.config is not None else None,
967971
self.config.env if self.config is not None else None,
968-
variables if variables is not None else self.config.variables if self.config is not None else None,
972+
self.config.variables if self.config is not None else None,
973+
variables,
969974
),
970975
COMPLETE_RESOURCE_IMPORT_TIME_OUT,
971976
)
@@ -984,7 +989,8 @@ async def complete_variables_import(
984989
base_dir,
985990
self.config.python_path if self.config is not None else None,
986991
self.config.env if self.config is not None else None,
987-
variables if variables is not None else self.config.variables if self.config is not None else None,
992+
self.config.variables if self.config is not None else None,
993+
variables,
988994
),
989995
COMPLETE_VARIABLES_IMPORT_TIME_OUT,
990996
)
@@ -1001,7 +1007,10 @@ async def resolve_variable(
10011007
name,
10021008
str(self.folder.to_path()),
10031009
base_dir,
1004-
variables if variables is not None else self.config.variables if self.config is not None else None,
1010+
self.config.python_path if self.config is not None else None,
1011+
self.config.env if self.config is not None else None,
1012+
self.config.variables if self.config is not None else None,
1013+
variables,
10051014
ignore_errors,
10061015
),
10071016
COMPLETE_VARIABLES_IMPORT_TIME_OUT,

robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,22 @@ def is_library_by_path(path: str) -> bool:
626626
return path.lower().endswith((".py", ".java", ".class", "/", os.sep))
627627

628628

629+
def update_python_path_and_env(
630+
working_dir: str = ".", pythonpath: Optional[List[str]] = None, environment: Optional[Dict[str, str]] = None
631+
) -> None:
632+
os.chdir(Path(working_dir))
633+
634+
if pythonpath is not None:
635+
for p in pythonpath:
636+
absolute_path = str(Path(p).absolute())
637+
if absolute_path not in sys.path:
638+
sys.path.insert(0, absolute_path)
639+
640+
if environment:
641+
for k, v in environment.items():
642+
os.environ[k] = v
643+
644+
629645
__PRELOADED_MODULES: Optional[Set[ModuleType]] = None
630646

631647

@@ -640,23 +656,12 @@ def _update_env(
640656
top = file.parents[3]
641657
for p in filter(lambda v: path_is_relative_to(v, top), sys.path.copy()):
642658
sys.path.remove(p)
643-
wd = Path(working_dir)
644659

645660
importlib.invalidate_caches()
646661

647662
gc.collect()
648663

649-
os.chdir(wd)
650-
651-
if pythonpath is not None:
652-
for p in pythonpath:
653-
absolute_path = str(Path(p).absolute())
654-
if absolute_path not in sys.path:
655-
sys.path.insert(0, absolute_path)
656-
657-
if environment:
658-
for k, v in environment.items():
659-
os.environ[k] = v
664+
update_python_path_and_env(working_dir, pythonpath, environment)
660665

661666

662667
def unload_preloaded_modules() -> None:
@@ -885,7 +890,10 @@ def report_invalid_syntax(self, message: str, level: str = "ERROR") -> None:
885890

886891

887892
def resolve_robot_variables(
888-
working_dir: str = ".", base_dir: str = ".", variables: Optional[Dict[str, Optional[Any]]] = None
893+
working_dir: str = ".",
894+
base_dir: str = ".",
895+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
896+
variables: Optional[Dict[str, Optional[Any]]] = None,
889897
) -> Any:
890898
from robot.variables import Variables
891899

@@ -928,6 +936,10 @@ def resolve_robot_variables(
928936
}.items():
929937
result[k] = v
930938

939+
if command_line_variables:
940+
for k, v in command_line_variables.items():
941+
result[f"${{{k}}}"] = v
942+
931943
if variables is not None:
932944

933945
vars = [_Variable(k, v) for k, v in variables.items() if v is not None]
@@ -942,10 +954,15 @@ def resolve_variable(
942954
name: str,
943955
working_dir: str = ".",
944956
base_dir: str = ".",
957+
pythonpath: Optional[List[str]] = None,
958+
environment: Optional[Dict[str, str]] = None,
959+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
945960
variables: Optional[Dict[str, Optional[Any]]] = None,
946961
ignore_errors: bool = True,
947962
) -> Any:
948-
robot_variables = resolve_robot_variables(working_dir, base_dir, variables)
963+
update_python_path_and_env(working_dir, pythonpath, environment)
964+
965+
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
949966

950967
return robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=ignore_errors)
951968

@@ -978,6 +995,7 @@ def _find_library_internal(
978995
base_dir: str = ".",
979996
pythonpath: Optional[List[str]] = None,
980997
environment: Optional[Dict[str, str]] = None,
998+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
981999
variables: Optional[Dict[str, Optional[Any]]] = None,
9821000
) -> Tuple[str, Any]:
9831001

@@ -986,7 +1004,7 @@ def _find_library_internal(
9861004

9871005
_update_env(working_dir, pythonpath, environment)
9881006

989-
robot_variables = resolve_robot_variables(working_dir, base_dir, variables)
1007+
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
9901008

9911009
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=True)
9921010

@@ -1007,10 +1025,13 @@ def find_library(
10071025
base_dir: str = ".",
10081026
pythonpath: Optional[List[str]] = None,
10091027
environment: Optional[Dict[str, str]] = None,
1028+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
10101029
variables: Optional[Dict[str, Optional[Any]]] = None,
10111030
) -> str:
10121031

1013-
return _find_library_internal(name, working_dir, base_dir, pythonpath, environment, variables)[0]
1032+
return _find_library_internal(
1033+
name, working_dir, base_dir, pythonpath, environment, command_line_variables, variables
1034+
)[0]
10141035

10151036

10161037
def get_library_doc(
@@ -1020,6 +1041,7 @@ def get_library_doc(
10201041
base_dir: str = ".",
10211042
pythonpath: Optional[List[str]] = None,
10221043
environment: Optional[Dict[str, str]] = None,
1044+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
10231045
variables: Optional[Dict[str, Optional[Any]]] = None,
10241046
) -> LibraryDoc:
10251047

@@ -1070,6 +1092,7 @@ def get_test_library(
10701092
base_dir=base_dir,
10711093
pythonpath=pythonpath,
10721094
environment=environment,
1095+
command_line_variables=command_line_variables,
10731096
variables=variables,
10741097
)
10751098

@@ -1301,14 +1324,15 @@ def find_file(
13011324
base_dir: str = ".",
13021325
pythonpath: Optional[List[str]] = None,
13031326
environment: Optional[Dict[str, str]] = None,
1327+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
13041328
variables: Optional[Dict[str, Optional[Any]]] = None,
13051329
file_type: str = "Resource",
13061330
) -> str:
13071331
from robot.utils.robotpath import find_file as robot_find_file
13081332

13091333
_update_env(working_dir, pythonpath, environment)
13101334

1311-
robot_variables = resolve_robot_variables(working_dir, base_dir, variables)
1335+
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
13121336

13131337
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=True)
13141338

@@ -1394,6 +1418,7 @@ def complete_library_import(
13941418
base_dir: str = ".",
13951419
pythonpath: Optional[List[str]] = None,
13961420
environment: Optional[Dict[str, str]] = None,
1421+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
13971422
variables: Optional[Dict[str, Optional[Any]]] = None,
13981423
) -> Optional[List[CompleteResult]]:
13991424

@@ -1409,7 +1434,7 @@ def complete_library_import(
14091434
]
14101435

14111436
if name is not None:
1412-
robot_variables = resolve_robot_variables(working_dir, base_dir, variables)
1437+
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
14131438

14141439
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=True)
14151440

@@ -1460,6 +1485,7 @@ def complete_resource_import(
14601485
base_dir: str = ".",
14611486
pythonpath: Optional[List[str]] = None,
14621487
environment: Optional[Dict[str, str]] = None,
1488+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
14631489
variables: Optional[Dict[str, Optional[Any]]] = None,
14641490
) -> Optional[List[CompleteResult]]:
14651491

@@ -1468,7 +1494,7 @@ def complete_resource_import(
14681494
result: List[CompleteResult] = []
14691495

14701496
if name is not None:
1471-
robot_variables = resolve_robot_variables(working_dir, base_dir, variables)
1497+
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
14721498

14731499
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=True)
14741500

@@ -1519,6 +1545,7 @@ def complete_variables_import(
15191545
base_dir: str = ".",
15201546
pythonpath: Optional[List[str]] = None,
15211547
environment: Optional[Dict[str, str]] = None,
1548+
command_line_variables: Optional[Dict[str, Optional[Any]]] = None,
15221549
variables: Optional[Dict[str, Optional[Any]]] = None,
15231550
) -> Optional[List[CompleteResult]]:
15241551

@@ -1527,7 +1554,7 @@ def complete_variables_import(
15271554
result: List[CompleteResult] = []
15281555

15291556
if name is not None:
1530-
robot_variables = resolve_robot_variables(working_dir, base_dir, variables)
1557+
robot_variables = resolve_robot_variables(working_dir, base_dir, command_line_variables, variables)
15311558

15321559
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=True)
15331560

robotcode/language_server/robotframework/diagnostics/namespace.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,11 @@ async def yield_variables(
701701
async def get_unresolved_variables(
702702
self, nodes: Optional[List[ast.AST]] = None, position: Optional[Position] = None
703703
) -> Dict[str, Any]:
704-
return {v.name: v.value async for k, v in self.yield_variables(nodes, position) if v.has_value}
704+
return {
705+
v.name: v.value
706+
async for k, v in self.yield_variables(nodes, position, skip_commandline_variables=True)
707+
if v.has_value
708+
}
705709

706710
@_logger.call
707711
async def get_variable_definitions(

0 commit comments

Comments
 (0)