{"record":{"id":"ab63c47fbb052e36","repo":"xai-org/x-algorithm","slug":"type-cannot-defined-in-root-scope-s","errorCode":null,"errorMessage":"type cannot defined in root scope: %s","messagePattern":"type cannot defined in root scope: (.+?)","errorType":"validation","errorClass":"SemanticCheckFailure","httpStatus":null,"severity":"error","filePath":"botmaker/src/java/com/twitter/botmaker/compiler/CompilerContext.java","lineNumber":292,"sourceCode":"      if (scope.isVariableDefined(variableText)) {\n        Parameter parameter = scope.getVariable(variableText);\n        return new Variable(variableText, parameter);\n      }\n    }\n    throw new SemanticCheckFailure(String.format(\"variable %s not defined\", variableText));\n  }\n\n  TypeContext getTypeContext() throws SemanticCheckFailure {\n    if (scopeList.isEmpty()) {\n      throw new SemanticCheckFailure(\"There is no type context\");\n    }\n\n    return scopeList.getFirst();\n  }\n\n  void defineType(ThriftType thriftType, String alias) throws SemanticCheckFailure {\n    if (scopeList.size() <= 1) {\n      throw new SemanticCheckFailure(\n          String.format(\"type cannot defined in root scope: %s\", alias));\n    }\n\n    CompilerScope scope = currentScope();\n    scope.defineType(thriftType, alias);\n  }\n\n  void defineType(ThriftStructType scroogeType, String alias) throws SemanticCheckFailure {\n    if (scopeList.size() <= 1) {\n      throw new SemanticCheckFailure(\n          String.format(\"type cannot defined in root scope: %s\", alias));\n    }\n\n    CompilerScope scope = currentScope();\n    scope.defineType(scroogeType, alias);\n  }\n\n  TestScope addTestScope() {","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/botmaker/src/java/com/twitter/botmaker/compiler/CompilerContext.java#L274-L310","documentation":"defineType(ThriftType, alias) rejects type-alias definitions made while only the root scope exists (scopeList.size() <= 1). Types can only be imported/aliased inside nested scopes such as function or module bodies.","triggerScenarios":"An 'import' or 'import as' statement appearing at the root/top level of a compilation unit, where only the root scope is active, triggering toImport/toImportAs to call defineType at root.","commonSituations":"Moving imports to the top of a file in the wrong construct; DSL changes that relaxed where imports may appear; custom transforms emitting imports outside scoped bodies.","solutions":["Move the import/type-alias inside a scoped construct (function/module body) where it is allowed","Verify the DSL grammar/semantics version for where imports are permitted","If writing a custom transform, emit the import after a scope has been pushed"],"exampleFix":"// before\nimport com.foo.BarType as T  // at root scope\n// after\nfn f() { import com.foo.BarType as T; ... }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"catch (SemanticCheckFailure e) { /* move the import inside a scoped body */ }","preventionTips":["Place imports/aliases only where the DSL allows (inside scoped constructs)","Validate import placement with a lint pass"],"tags":["botmaker","scope","import","type-alias"],"backgroundTag":"definition-outside-scope","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}