{"record":{"id":"18dae8062bfa0e20","repo":"thoughtbot/factory_bot","slug":"sequence-sequence-uri-manager-first-failed-to-18dae8","errorCode":null,"errorMessage":"Sequence '#{sequence.uri_manager.first}' failed to return a value. Perhaps it needs a scope to operate? (scope: <object>)","messagePattern":"Sequence '#(.+?)' failed to return a value\\. Perhaps it needs a scope to operate\\? \\(scope: <object>\\)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/factory_bot/syntax/methods.rb","lineNumber":176,"sourceCode":"      private\n\n      ##\n      # Increments the given sequence and returns the value.\n      #\n      # Arguments:\n      #   sequence:\n      #     The sequence instance\n      #   scope: (object)(optional)\n      #     The object the sequence should be evaluated within\n      #\n      def increment_sequence(sequence, scope: nil)\n        value = sequence.next(scope)\n\n        raise if value.respond_to?(:start_with?) && value.start_with?(\"#<FactoryBot::Declaration\")\n\n        value\n      rescue\n        raise ArgumentError, \"Sequence '#{sequence.uri_manager.first}' failed to \" \\\n                            \"return a value. Perhaps it needs a scope to operate? (scope: <object>)\"\n      end\n    end\n  end\nend\n","sourceCodeStart":158,"sourceCodeEnd":182,"githubUrl":"https://github.com/thoughtbot/factory_bot/blob/18ae8b581bf55de681c8adb5f74d32787fd2157f/lib/factory_bot/syntax/methods.rb#L158-L182","documentation":"ArgumentError raised by the private Syntax::Methods#increment_sequence (methods.rb:169-178) that backs the public generate/generate_list API. Here scope defaults to nil, so a sequence block written against instance state (`sequence(:info) { |n| \"#{name}:#{age + n}\" }`) executes with no scope and fails; the bare rescue rewrites any exception from sequence.next into this generic ArgumentError, naming the sequence's URI from sequence.uri_manager (e.g. 'user/info'). The underlying exception is hidden.","triggerScenarios":"Per spec/acceptance/sequence_spec.rb:203-219: define `factory :user do sequence(:info) { |n| \"#{name}:#{age + n}\" } end`; then `generate(:user, :info)` raises \"Sequence 'user/info' failed to return a value. Perhaps it needs a scope to operate?\", while `generate(:user, :info, scope: jester)` with an object that has name/age succeeds. Any other exception inside the block (typo, nil arithmetic) reached via generate/generate_list produces the same error.","commonSituations":"Calling global generate for a sequence originally written against factory instance state; refactoring an inline factory sequence into a shared/global one; NoMethodError-on-nil hidden by the generic message, making failures hard to diagnose in CI.","solutions":["Pass an explicit scope that provides every method the block references: `generate(:user, :info, scope: user)`.","If the sequence should be stateless, rewrite it to use only the counter: `sequence(:info) { |n| \"user-#{n}\" }`.","For lists, thread the same scope: `generate_list(:user, :info, 5, scope: user)`.","While debugging, call the sequence directly (e.g. `FactoryBot::Sequence.find(:user, :info).next(user)`) to see the original exception before factory_bot rewrites it."],"exampleFix":"# before\ngenerate(:user, :info) # ArgumentError: state-dependent sequence, no scope\n\n# after\nuser = build(:user, name: 'Jester', age: 21)\ngenerate(:user, :info, scope: user)","handlingStrategy":"validation","validationCode":"# wrapper that refuses scope-less calls to state-dependent sequences\ndef generate_scoped!(*uri, scope:)\n  raise ArgumentError, 'scope: is required for this sequence' if scope.nil?\n  generate(*uri, scope: scope)\nend","typeGuard":"->(scope) { !scope.nil? }","tryCatchPattern":"begin\n  generate(:user, :info)\nrescue ArgumentError => e\n  raise unless e.message.include?('failed to return a value')\n  # either pass scope: or rewrite the sequence to be self-contained\nend","preventionTips":["Treat generate without scope: as valid only for stateless sequences.","Write sequences using only |n| unless a scope is guaranteed at every call site.","Debug by calling sequence.next(scope) directly — the rescue hides the underlying exception."],"tags":["ruby","factory-bot","sequence","generate","scope"],"backgroundTag":"sequence-scope-missing","analyzedSha":"18ae8b581bf55de681c8adb5f74d32787fd2157f","analyzedAt":"2026-08-21T18:25:33.545Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}