# Nav2 BT Navigator因YAML多行语法引入换行符导致XML加载失败

> 原文链接：https://robotics.hellobike.com/pitfalls/2026-11-09-navigation2-6755
> 频道：pitfalls | 更新日期：2026-11-09 | 标签：auto, github-issue, navigation2
> 核心摘要：Nav2启动报错无法打开BT XML文件，系YAML使用>多行语法在路径末尾引入换行符所致，改为单行写法即可解决。

# Nav2 BT Navigator因YAML多行语法引入换行符导致XML加载失败

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[BT Navigator can't load behavior trees](https://github.com/ros-navigation/navigation2/issues/6755)（GitHub ros-navigation/navigation2 #6755）

**状态**：有绕过办法

## 问题

在Ubuntu 24.04与ROS2 Jazzy环境下配置nav2_bt_navigator时，尽管XML文件存在且权限正确，启动仍报错`Couldn't open input XML file`与`Error loading XML file`。硬编码路径或使用RewrittenYaml均无效，日志显示解析后的文件路径末尾包含空行，导致文件打开失败。

## 环境

Ubuntu 24.04 x86_64 AMD PC；ROS2 Jazzy；ros-jazzy-navigation2 1.3.13-1noble.20260921.033624

## 解决办法

1. 修改nav2_params.yaml中`default_nav_to_pose_bt_xml`与`default_nav_through_poses_bt_xml`参数，移除YAML多行折叠块标量`>`及换行缩进
2. 将XML路径配置为单行格式：`default_nav_to_pose_bt_xml: $(find-pkg-share diff_drive_robot_navigation)/behavior_trees/navigate_to_pose_w_replanning_and_recovery.xml`
3. 同理将`default_nav_through_poses_bt_xml`改为单行写法，避免解析器在路径字符串末尾追加换行符
4. 重新编译并启动nav2，确认bt_navigator不再报XML加载错误

讨论原作者：@wineTGH 等，版权归原作者所有。
:::

## 原始参考来源 (Sources)

- [GitHub ros-navigation/navigation2 #6755：BT Navigator can't load behavior trees](https://github.com/ros-navigation/navigation2/issues/6755)