{"record":{"id":"20186933cd66efd4","repo":"apache/beam","slug":"invalid-scope-flatten","errorCode":null,"errorMessage":"invalid scope","messagePattern":"invalid scope","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/flatten.go","lineNumber":39,"sourceCode":")\n\n// Flatten is a PTransform that takes either multiple PCollections of type 'A'\n// and returns a single PCollection of type 'A' containing all the elements in\n// all the input PCollections. The name \"Flatten\" suggests taking a list of lists\n// and flattening them into a single list.\n//\n// By default, the Coder of the output PCollection is the same as the Coder\n// of the first PCollection.\nfunc Flatten(s Scope, cols ...PCollection) PCollection {\n\treturn Must(TryFlatten(s, cols...))\n}\n\n// TryFlatten merges incoming PCollections of type 'A' to a single PCollection\n// of type 'A'. Returns an error indicating the set of PCollections that could\n// not be flattened.\nfunc TryFlatten(s Scope, cols ...PCollection) (PCollection, error) {\n\tif !s.IsValid() {\n\t\treturn PCollection{}, errors.New(\"invalid scope\")\n\t}\n\tfor i, in := range cols {\n\t\tif !in.IsValid() {\n\t\t\treturn PCollection{}, errors.Errorf(\"invalid pcollection to flatten: index %v\", i)\n\t\t}\n\t}\n\tif len(cols) == 0 {\n\t\treturn PCollection{}, errors.New(\"no input pcollections\")\n\t}\n\tif len(cols) == 1 {\n\t\treturn cols[0], nil // no-op\n\t}\n\n\tvar in []*graph.Node\n\tfor _, s := range cols {\n\t\tin = append(in, s.n)\n\t}\n\tedge, err := graph.NewFlatten(s.real, s.scope, in)","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/flatten.go#L21-L57","documentation":"TryFlatten (and its wrapper Flatten) rejects the merge of multiple PCollections when the supplied Scope is invalid (its pipeline/scope is zero-valued or uninitialized). A Scope identifies which pipeline and enclosing transform the flatten edge is inserted into; without a valid one the transform cannot be added to the pipeline graph, so the call fails fast with this error.","triggerScenarios":"Calling beam.Flatten(s, cols...) or beam.TryFlatten(s, cols...) where s was produced by a zero-value Scope{}, an invalid Scope returned from a failed operation, or a Scope from a pipeline that was never constructed via beam.NewPipeline() / a valid named scope (s.scope).","commonSituations":"Storing a Scope in a struct that is initialized with Go's zero value; using a Scope variable whose assignment path returned an error earlier and was ignored; calling Flatten outside any transform context after constructing a pipeline but passing an invalid nested scope.","solutions":["Create the pipeline with p := beam.NewPipeline() and pass a valid scope such as p or s := beam.Scope(\"name\") derived from it as the first argument to Flatten.","Check s.IsValid() (or log s) right before the call to confirm the scope is valid; if it is not, trace where the Scope came from and fix its construction.","Ensure any function that receives a Scope propagates error returns so a zero-value Scope is not silently used."],"exampleFix":"// before\nvar s beam.Scope\nout := beam.Flatten(s, a, b) // panic-free but errors: invalid scope\n\n// after\np := beam.NewPipeline()\nout := beam.Flatten(p, a, b)","handlingStrategy":"validation","validationCode":"if !s.IsValid() {\n\treturn fmt.Errorf(\"cannot Flatten: scope is invalid; build it from beam.NewPipeline() or beam.Scope(...)\")\n}\nout := beam.Flatten(s, a, b)","typeGuard":"func validScope(s beam.Scope) bool { return s.IsValid() }","tryCatchPattern":null,"preventionTips":["Always derive scopes from beam.NewPipeline() or beam.Scope(\"label\"), never store zero-value Scope structs.","Handle error returns of transform calls immediately instead of reusing their empty results.","Pass Scope explicitly through helper functions rather than via unassigned struct fields."],"tags":["go","apache-beam","pipeline-construction","invalid-scope"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}