From f74ea4ac4f98aa04a1c342434b8b0e19559f4ea6 Mon Sep 17 00:00:00 2001 From: Daniel Biehl Date: Wed, 24 Jan 2024 12:11:10 +0100 Subject: [PATCH] fix(runner): environment vars are evaluated to late --- .../robotframework/protocol.py | 2 +- .../robot/src/robotcode/robot/config/model.py | 4 ++-- .../diagnostics/document_cache_helper.py | 2 +- .../runner/src/robotcode/runner/cli/libdoc.py | 17 ++++++++--------- .../runner/src/robotcode/runner/cli/rebot.py | 17 ++++++++--------- .../runner/src/robotcode/runner/cli/robot.py | 19 ++++++++++--------- .../src/robotcode/runner/cli/testdoc.py | 16 +++++++--------- 7 files changed, 37 insertions(+), 40 deletions(-) diff --git a/packages/language_server/src/robotcode/language_server/robotframework/protocol.py b/packages/language_server/src/robotcode/language_server/robotframework/protocol.py index 0254cb78d..e08af4cc4 100644 --- a/packages/language_server/src/robotcode/language_server/robotframework/protocol.py +++ b/packages/language_server/src/robotcode/language_server/robotframework/protocol.py @@ -215,7 +215,7 @@ def server_initialized(self, sender: Any) -> None: config: RobotConfig = self.workspace.get_configuration(RobotConfig, folder.uri) for k, v in (self.robot_profile.env or {}).items(): - os.environ[k] = v + os.environ[k] = str(v) for p in self.robot_profile.python_path or []: pa = Path(str(p)) diff --git a/packages/robot/src/robotcode/robot/config/model.py b/packages/robot/src/robotcode/robot/config/model.py index caf51aec3..87e5dc054 100644 --- a/packages/robot/src/robotcode/robot/config/model.py +++ b/packages/robot/src/robotcode/robot/config/model.py @@ -2155,7 +2155,7 @@ class RobotBaseProfile(CommonOptions, CommonExtendOptions, RobotOptions, RobotEx Corresponds to the `paths` argument of __robot__. """ ) - env: Optional[Dict[str, str]] = field( + env: Optional[Dict[str, Union[str, StringExpression]]] = field( description="""\ Define environment variables to be set before running tests. @@ -2202,7 +2202,7 @@ class RobotExtraBaseProfile(RobotBaseProfile): """ ) - extend_env: Optional[Dict[str, str]] = field( + extend_env: Optional[Dict[str, Union[str, StringExpression]]] = field( description="""\ Append extra environment variables to be set before tests. """ diff --git a/packages/robot/src/robotcode/robot/diagnostics/document_cache_helper.py b/packages/robot/src/robotcode/robot/diagnostics/document_cache_helper.py index 246b412c3..b98453b1d 100644 --- a/packages/robot/src/robotcode/robot/diagnostics/document_cache_helper.py +++ b/packages/robot/src/robotcode/robot/diagnostics/document_cache_helper.py @@ -462,7 +462,7 @@ def create_imports_manager(self, root_uri: Uri) -> ImportsManager: robot_config = self.workspace.get_configuration(RobotConfig, root_uri) cache_config = self.workspace.get_configuration(CacheConfig, root_uri) - environment = self.robot_profile.env or {} + environment = {k: str(v) for k, v in (self.robot_profile.env or {}).items()} if robot_config.env: environment.update(robot_config.env) diff --git a/packages/runner/src/robotcode/runner/cli/libdoc.py b/packages/runner/src/robotcode/runner/cli/libdoc.py index 343ab3029..654c8daa6 100644 --- a/packages/runner/src/robotcode/runner/cli/libdoc.py +++ b/packages/runner/src/robotcode/runner/cli/libdoc.py @@ -70,11 +70,15 @@ def libdoc(app: Application, robot_options_and_args: Tuple[str, ...]) -> None: robot_arguments, app.config.config_files, verbose_callback=app.verbose ) try: - profile = ( - load_robot_config_from_path(*config_files) - .combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose) - .evaluated() + profile = load_robot_config_from_path(*config_files).combine_profiles( + *(app.config.profiles or []), verbose_callback=app.verbose ) + + if profile.env: + for k, v in profile.env.items(): + os.environ[k] = str(v) + app.verbose(lambda: f"Set environment variable {k} to {v}") + profile = profile.evaluated() except (TypeError, ValueError) as e: raise click.ClickException(str(e)) from e @@ -86,11 +90,6 @@ def libdoc(app: Application, robot_options_and_args: Tuple[str, ...]) -> None: options = libdoc_options.build_command_line() - if profile.env: - for k, v in profile.env.items(): - os.environ[k] = v - app.verbose(lambda: f"Set environment variable {k} to {v}") - app.verbose( lambda: "Executing libdoc robot with the following options:\n " + " ".join(f'"{o}"' for o in (options + list(robot_options_and_args))) diff --git a/packages/runner/src/robotcode/runner/cli/rebot.py b/packages/runner/src/robotcode/runner/cli/rebot.py index 726833102..495df1803 100644 --- a/packages/runner/src/robotcode/runner/cli/rebot.py +++ b/packages/runner/src/robotcode/runner/cli/rebot.py @@ -71,11 +71,15 @@ def rebot(app: Application, robot_options_and_args: Tuple[str, ...]) -> None: ) try: - profile = ( - load_robot_config_from_path(*config_files) - .combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose) - .evaluated() + profile = load_robot_config_from_path(*config_files).combine_profiles( + *(app.config.profiles or []), verbose_callback=app.verbose ) + if profile.env: + for k, v in profile.env.items(): + os.environ[k] = str(v) + app.verbose(lambda: f"Set environment variable {k} to {v}") + profile = profile.evaluated() + except (TypeError, ValueError) as e: raise click.ClickException(str(e)) from e @@ -90,11 +94,6 @@ def rebot(app: Application, robot_options_and_args: Tuple[str, ...]) -> None: except (TypeError, ValueError) as e: raise click.ClickException(str(e)) from e - if profile.env: - for k, v in profile.env.items(): - os.environ[k] = v - app.verbose(lambda: f"Set environment variable {k} to {v}") - app.verbose( lambda: "Executing rebot with the following options:\n " + " ".join(f'"{o}"' for o in (options + list(robot_options_and_args))) diff --git a/packages/runner/src/robotcode/runner/cli/robot.py b/packages/runner/src/robotcode/runner/cli/robot.py index 9c3e17cef..993a1bdb5 100644 --- a/packages/runner/src/robotcode/runner/cli/robot.py +++ b/packages/runner/src/robotcode/runner/cli/robot.py @@ -108,11 +108,17 @@ def handle_robot_options( robot_arguments, app.config.config_files, verbose_callback=app.verbose ) try: - profile = ( - load_robot_config_from_path(*config_files) - .combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose) - .evaluated() + profile = load_robot_config_from_path(*config_files).combine_profiles( + *(app.config.profiles or []), verbose_callback=app.verbose ) + + if profile.env: + for k, v in profile.env.items(): + os.environ[k] = str(v) + app.verbose(lambda: f"Set environment variable {k} to {v}") + + profile = profile.evaluated() + except (TypeError, ValueError) as e: raise click.ClickException(str(e)) from e @@ -132,11 +138,6 @@ def handle_robot_options( f"robotcode.modifiers.ExcludedByLongName{sep}{sep.join(exclude_by_longname)}", ) - if profile.env: - for k, v in profile.env.items(): - os.environ[k] = v - app.verbose(lambda: f"Set environment variable {k} to {v}") - app.verbose( lambda: "Executing robot with following options:\n " + " ".join(f'"{o}"' for o in (cmd_options + list(robot_options_and_args))) diff --git a/packages/runner/src/robotcode/runner/cli/testdoc.py b/packages/runner/src/robotcode/runner/cli/testdoc.py index 332ddfb41..2fab23125 100644 --- a/packages/runner/src/robotcode/runner/cli/testdoc.py +++ b/packages/runner/src/robotcode/runner/cli/testdoc.py @@ -71,11 +71,14 @@ def testdoc(app: Application, robot_options_and_args: Tuple[str, ...]) -> None: ) try: - profile = ( - load_robot_config_from_path(*config_files) - .combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose) - .evaluated() + profile = load_robot_config_from_path(*config_files).combine_profiles( + *(app.config.profiles or []), verbose_callback=app.verbose ) + if profile.env: + for k, v in profile.env.items(): + os.environ[k] = str(v) + app.verbose(lambda: f"Set environment variable {k} to {v}") + profile = profile.evaluated() except (TypeError, ValueError) as e: raise click.ClickException(str(e)) from e @@ -87,11 +90,6 @@ def testdoc(app: Application, robot_options_and_args: Tuple[str, ...]) -> None: options = testdoc_options.build_command_line() - if profile.env: - for k, v in profile.env.items(): - os.environ[k] = v - app.verbose(lambda: f"Set environment variable {k} to {v}") - app.verbose( lambda: "Executing testdoc with the following options:\n " + " ".join(f'"{o}"' for o in (options + list(robot_options_and_args)))