{"record":{"id":"1583523e6b524e02","repo":"elastic/elasticsearch","slug":"couldn-t-find-named-teardown-name-for-snippet","errorCode":null,"errorMessage":"Couldn't find named teardown $name for ${snippet}","messagePattern":"Couldn't find named teardown \\$name for (.+?)","errorType":"validation","errorClass":"InvalidUserDataException","httpStatus":null,"severity":"error","filePath":"build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/doc/RestTestsFromDocSnippetTask.java","lineNumber":306,"sourceCode":"            if (test.teardown() != null) {\n                teardown(test);\n            }\n        }\n\n        private void response(Snippet response) {\n            if (null == response.skip()) {\n                current.println(\"  - match:\");\n                current.println(\"      $body:\");\n                replaceBlockQuote(response.contents()).lines().forEach(line -> current.println(\"        \" + line));\n            }\n        }\n\n        private void teardown(final Snippet snippet) {\n            // insert a teardown defined outside of the docs\n            for (final String name : snippet.teardown().split(\",\")) {\n                final String teardown = getTeardowns().get().get(name);\n                if (teardown == null) {\n                    throw new InvalidUserDataException(\"Couldn't find named teardown $name for \" + snippet);\n                }\n                current.println(\"# Named teardown \" + name);\n                current.println(teardown);\n            }\n        }\n\n        private void testTearDown(Snippet snippet) {\n            if (previousTest != null && previousTest.testSetup() == false && lastDocsPath.equals(snippet.path())) {\n                throw new InvalidUserDataException(snippet + \" must follow test setup or be first\");\n            }\n            setupCurrent(snippet);\n            current.println(\"---\");\n            current.println(\"teardown:\");\n            body(snippet, true);\n        }\n\n        void emitDo(\n            String method,","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/doc/RestTestsFromDocSnippetTask.java#L288-L324","documentation":"TestBuilder.teardown iterates the comma-separated names in snippet.teardown() and looks each up in the teardowns MapProperty (getTeardowns().get()). If a name is not a key in that map, no teardown body exists to insert, so it throws. Note the message literally contains the text '$name' (un-interpolated) because the source uses string concatenation of the variable 'name' but the message template wrote the literal '$name' — the actual missing name is appended after 'for ' via the snippet toString.","triggerScenarios":"A console snippet declares // TEARDOWN[someName] but the build did not register 'someName' in the teardowns MapProperty configured on the RestTestsFromDocSnippetTask.","commonSituations":"Typo in the teardown name between the doc and the build.gradle registration; removing a teardown registration from the build while the doc still references it; adding a new TEARDOWN reference without registering it.","solutions":["Register the missing teardown in the task configuration: restTestsTask.getTeardowns().put('someName', teardownBody).","Fix the spelling of the name in the doc snippet to match the registered key exactly.","Remove the // TEARDOWN[someName] reference from the doc if the teardown is no longer needed."],"exampleFix":"// before — doc references an unregistered teardown\n// CONSOLE\n// TEARDOWN[cleanup]\nGET /_search\n\n// build.gradle (missing registration)\n\n// after — register in build.gradle\nrestTestsTask.getTeardowns().put('cleanup', '''\nDELETE /idx\n''')","handlingStrategy":"validation","validationCode":"// Verify every teardown name referenced in docs is registered in the build\nimport java.util.Map;\nimport java.util.Arrays;\n\nvoid checkTeardownsRegistered(List<SnippetInfo> snippets, Map<String,String> registered) {\n    for (SnippetInfo s : snippets) {\n        if (s.teardown != null) {\n            for (String name : s.teardown.split(\",\")) {\n                if (!registered.containsKey(name.trim())) {\n                    throw new IllegalStateException(\n                        \"Teardown '\" + name + \"' in \" + s.path + \" is not registered. \"\n                        + \"Add: restTestsTask.getTeardowns().put('\" + name + \"', <body>)\");\n                }\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Register every teardown name in build.gradle via getTeardowns().put(name, body).","Keep teardown names consistent between docs and build config — treat the registration as the source of truth.","When removing a teardown registration, grep the docs for references and remove them too."],"tags":["docs","teardown","named","registration","gradle"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}