ROS 2 Launch Environment Variables: Case-Sensitivity Across Platforms
0 reputation · 10 Jul 2022, 06:11 UTC
0 reputation · 10 Jul 2022, 06:11 UTC
The ROS 2 launch system allows the definition of environment variables through actions like SetEnvironmentVariable. These variables are expanded during the execution of launch descriptions to configure node behavior or system paths.
A compatibility boundary exists because the launch system delegates environment variable resolution to the underlying operating system. This creates a discrepancy between POSIX-compliant systems, where environment variables are case-sensitive, and Windows, where they are case-insensitive.
When developing portable launch files intended for cross-platform deployment, it is unclear how the launch system should normalize these keys to ensure consistent behavior without risking collisions on Linux or failures on Windows.
launch package provide a mechanism to enforce case-sensitivity on Windows?34275 reputation · 10 Jul 2022, 16:35 UTC
1. Does the launch package enforce case‑sensitivity on Windows?
No. The ROS 2 launch system passes environment variable names directly to the underlying operating system. On POSIX systems the names are case‑sensitive, while on Windows they are case‑insensitive. The launch package has no flag or option to change this behaviour.
2. Recommended pattern for cross‑platform variable definition
• Use a single, consistent spelling for every variable. Avoid names that differ only by case.
• Prefer all‑uppercase names (e.g., MY_VAR) to match the common convention on both platforms.
• Add a unique prefix for your package or project (e.g., MYPROJECT_FOO) to prevent accidental collisions with other software.
• When reading a variable with EnvironmentVariable or LaunchConfiguration, use the same spelling that was set.
• If you need to support legacy names, set a single canonical name and expose an alias only in the documentation, not in the launch file itself.
On each target OS run the following Python script after a launch file has set two variables that differ only by case. The script prints the environment dictionary.
import os
print('FOO:', os.environ.get('FOO'))
print('foo:', os.environ.get('foo'))
Example launch snippet (Python launch file):
from launch import LaunchDescription
from launch.actions import SetEnvironmentVariable
ld = LaunchDescription([
SetEnvironmentVariable(name='FOO', value='value1'),
SetEnvironmentVariable(name='foo', value='value2'),
# launch a simple process that prints the environment
# e.g., a Python node that imports os and prints the two keys
])
return ld
On Linux you should see two distinct values. On Windows you will see that FOO and foo refer to the same entry, with the value from the last assignment.
By following the single‑spelling, uppercase, prefixed convention, you guarantee that the same name resolves to the same value regardless of the host operating system.
Use comments to ask for clarification. Post a solution as an answer.
34,275 reputation · 10 Jul 2022, 17:06 UTC
Since the launch package relies on Python's os.environ, it inherits the underlying OS behavior: POSIX systems treat environment variables as case-sensitive, while Windows does not. There is no built-in abstraction in ROS 2 to enforce case-sensitivity on Windows.
To prevent silent overwrites or resolution failures during cross-platform deployment, consider these practical constraints:
UPPER_CASE convention for all custom variables to avoid collisions that only manifest on Windows.MY_VAR and my_var). On Ubuntu, both will persist; on Windows, the second will overwrite the first.Relying on case-differentiation for logic is a high-risk pattern that will inevitably lead to runtime failures when migrating from Linux to Windows environments.