{"record":{"id":"d5c551e7afc5185a","repo":"apache/beam","slug":"invalid-scope-pipeline","errorCode":null,"errorMessage":"Invalid Scope","messagePattern":"Invalid Scope","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"sdks/go/pkg/beam/pipeline.go","lineNumber":46,"sourceCode":"// monitoring and visualization purposes.\ntype Scope struct {\n\t// parent is the scoped insertion point for composite transforms.\n\tscope *graph.Scope\n\t// real is the enclosing graph.\n\treal *graph.Graph\n}\n\n// IsValid returns true iff the Scope is valid. Any use of an invalid Scope\n// will result in a panic.\nfunc (s Scope) IsValid() bool {\n\treturn s.real != nil && s.scope != nil\n}\n\n// Scope returns a sub-scope with the given name. The name provided may\n// be augmented to ensure uniqueness.\nfunc (s Scope) Scope(name string) Scope {\n\tif !s.IsValid() {\n\t\tpanic(\"Invalid Scope\")\n\t}\n\tscope := s.real.NewScope(s.scope, name)\n\treturn Scope{scope: scope, real: s.real}\n}\n\n// WithContext creates a named subscope with an attached context for the\n// represented composite transform. Values from that context may be\n// extracted and added to the composite PTransform or generate a new\n// environment for scoped transforms.\n//\n// If you're not sure whether these apply to your transform, use Scope\n// instead, and do not set a context.\nfunc (s Scope) WithContext(ctx context.Context, name string) Scope {\n\tnewS := s.Scope(name)\n\tnewS.scope.Context = ctx\n\treturn newS\n}\n","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/pipeline.go#L28-L64","documentation":"Scope.Scope panics with \"Invalid Scope\" when the receiver Scope is invalid (zero value or nil-backed). Scopes name sub-sections of the pipeline graph; deriving a sub-scope from an invalid parent would corrupt the graph, so the SDK panics immediately. Public graph builders like ApplyTransform, Read, and user DoFn helpers (FilterWords, PlaysForWords, BelowGlobalMean) all go through here when creating named sub-scopes.","triggerScenarios":"Calling s.Scope(name) (directly or via ApplyTransform/applyTransform/Read) on a zero-value beam.Scope, e.g. a Scope variable never initialized or returned empty from an error path.","commonSituations":"Declaring var s beam.Scope and using it without s := p.Root(); a helper function that returns beam.Scope{} on error and the caller then derives child scopes from it.","solutions":["Check s.IsValid() before calling Scope/ApplyTransform.","Initialize scopes from a real pipeline root: s := p.Root().","Fix helpers that return beam.Scope{} on failure; return an error alongside the scope instead."],"exampleFix":"// before\nvar s beam.Scope\nchild := s.Scope(\"myStep\") // panics: Invalid Scope\n\n// after\ns := p.Root()\nchild := s.Scope(\"myStep\")","handlingStrategy":"validation","validationCode":"if !s.IsValid() {\n    return fmt.Errorf(\"scope is invalid; derive scopes from p.Root()\")\n}","typeGuard":"func validScope(s beam.Scope) bool { return s.IsValid() }","tryCatchPattern":"if !s.IsValid() { return errInvalidScope }","preventionTips":["Always create scopes via p.Root() or Scope(\"name\") on a validated parent.","Never use a declared-but-uninitialized beam.Scope variable.","Return errors from helpers that fail to produce a valid scope."],"tags":["go","apache-beam","panic","scope"],"backgroundTag":"invalid-state-transition","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}