ros2 config_error ai_generated true

ros2 launch: Substitution error: Undefined variable 'my_var' in launch file

ID: ros2/launch-file-substitution-undefined-variable

Also available as: JSON · Markdown · 中文
90%Fix Rate
87%Confidence
1Evidence
2023-07-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Humble active
Iron active
Jazzy active
Rolling active

Root Cause

A launch file uses a variable substitution (e.g., $(var my_var)) that hasn't been declared or passed as an argument, causing a parse failure.

generic

中文

启动文件使用了未声明或未作为参数传递的变量替换(例如$(var my_var)),导致解析失败。

Official Documentation

https://docs.ros.org/en/rolling/Tutorials/Intermediate/Launch/Using-Substitutions.html

Workarounds

  1. 95% success Add a DeclareLaunchArgument for the variable in the launch file.
    Add a DeclareLaunchArgument for the variable in the launch file.
  2. 90% success Pass the variable as a command-line argument when launching.
    Pass the variable as a command-line argument when launching.
  3. 85% success Use a default value in the substitution to avoid undefined errors.
    Use a default value in the substitution to avoid undefined errors.

中文步骤

  1. Add a DeclareLaunchArgument for the variable in the launch file.
  2. Pass the variable as a command-line argument when launching.
  3. Use a default value in the substitution to avoid undefined errors.

Dead Ends

Common approaches that don't work:

  1. Using $(env my_var) instead of $(var my_var) without setting environment variable 60% fail

    Substitution type mismatch; $(env) expects an environment variable, not a launch argument.

  2. Adding the variable directly in the launch file without DeclareLaunchArgument 70% fail

    ROS2 launch requires explicit declaration; inline usage without declaration still fails.

  3. Setting the variable in a separate YAML config file without including it 50% fail

    YAML config files aren't automatically loaded; they must be included via IncludeLaunchDescription.