ros2 / launch

Tools for launching multiple processes and for writing tests involving multiple processes.
Apache License 2.0
126 stars 141 forks source link

Support scoping environment variables in GroupAction #597

Closed jacobperron closed 2 years ago

jacobperron commented 2 years ago

Feature request

Feature description

Currently, the SetEnvironmentVariable action applies globally. Consider the following launch file:

import launch
from launch.actions import GroupAction
from launch.actions import ExecuteProcess
from launch.actions import SetEnvironmentVariable

def generate_launch_description():
    return launch.LaunchDescription([
        SetEnvironmentVariable(name='my_env_var', value='1'),
        ExecuteProcess(cmd=['echo', '$my_env_var'], output='screen', shell=True),
        GroupAction([
            SetEnvironmentVariable(name='my_env_var', value='2'),
        ]),
    ])

Though I would expect it to output 1, it actually outputs 2.

My expectation is similar to what is described in the XML launch file design doc. Note, front-end launch files inherit the same behavior has the above Python example.

Implementation considerations

FWIW, I noticed that using the EnvironmentVariable substitution works as expected. For example,

import launch
from launch.actions import GroupAction
from launch.actions import ExecuteProcess
from launch.actions import SetEnvironmentVariable
from launch.substitutions import EnvironmentVariable

def generate_launch_description():
    return launch.LaunchDescription([
        SetEnvironmentVariable(name='my_env_var', value='1'),
        ExecuteProcess(cmd=['echo', EnvironmentVariable('my_env_var')], output='screen')
        GroupAction([
            SetEnvironmentVariable(name='my_env_var', value='2'),
        ]),
    ])

outputs 1 as I would expect. So, it seems the issue is specific to processes.