diff --git a/src/user/user_model.cc b/src/user/user_model.cc index 6337f9cd..d71ca7c9 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -5962,15 +5962,14 @@ void mjCModel::ResolvePlugin(mjCBase* obj, const std::string& plugin_name, else if (!*plugin_instance) { *plugin_instance = static_cast(FindObject(mjOBJ_PLUGIN, plugin_instance_name)); - (*plugin_instance)->plugin_slot = plugin_slot; if (!*plugin_instance) { throw mjCError( obj, "unrecognized name '%s' for plugin instance", plugin_instance_name.c_str()); } + (*plugin_instance)->plugin_slot = plugin_slot; if (plugin_slot != -1 && plugin_slot != (*plugin_instance)->plugin_slot) { throw mjCError( obj, "'plugin' attribute does not match that of the instance"); } - plugin_slot = (*plugin_instance)->plugin_slot; } } diff --git a/test/user/CMakeLists.txt b/test/user/CMakeLists.txt index 3b073c3d..87ce1d90 100644 --- a/test/user/CMakeLists.txt +++ b/test/user/CMakeLists.txt @@ -15,6 +15,9 @@ mujoco_test( user_model_test ADDITIONAL_LINK_LIBRARIES absl::str_format compare_model + PROPERTIES + ENVIRONMENT + "MUJOCO_PLUGIN_DIR=$" ) mujoco_test(user_objects_test) diff --git a/test/user/user_model_test.cc b/test/user/user_model_test.cc index 52446a3a..c247d492 100644 --- a/test/user/user_model_test.cc +++ b/test/user/user_model_test.cc @@ -872,6 +872,43 @@ TEST_F(LengthRangeTest, LengthRangeThreading) { mj_deleteSpec(spec); } + +TEST_F(MujocoTest, ResolvePluginMissingInstanceThrowsError) { + // Instance name="my_pid_config" dos not match actuator plugin's instance="pid_config", + // this should throw an appropriate error + static constexpr char xml_mismatch[] = R"( + + + + + + + + + + + + + + + + + )"; + + std::array error_buffer; + mjSpec* spec = mj_parseXMLString(xml_mismatch, 0, error_buffer.data(), error_buffer.size()); + ASSERT_THAT(spec, NotNull()) << error_buffer.data(); + + mjModel* model = mj_compile(spec, nullptr); + EXPECT_THAT(model, IsNull()); + + std::string error_msg = mjs_getError(spec); + EXPECT_THAT(error_msg, HasSubstr("unrecognized name 'pid_config' for plugin instance")); + + if (model) mj_deleteModel(model); + mj_deleteSpec(spec); +} + // ----------------------------- test modeldir -------------------------------- TEST_F(MujocoTest, Modeldir) {