Skip to content

CLI: build_spec raises NoMethodError when a code object has no associated source file #10

Description

@jcouball

Summary

build_spec calls .join on the return value of Array#first without guarding against nil. If YARD ever returns a code object whose files array is empty, the call raises NoMethodError and the entire test-examples pipeline crashes.

Location

lib/yard/cli/test_examples.rbbuild_spec

spec.filepath = "#{Dir.pwd}/#{example.object.files.first.join(':')}"

When can files be empty?

YARD::CodeObject#files returns an empty array for objects that YARD could not associate with a source location — for example:

  • Objects defined entirely in C extensions loaded as .so/.bundle files.
  • Objects synthesised by YARD plugins or macros that do not set file information.
  • Objects documented in a yard file (.yardoc cache) whose original source has since been deleted.

All of these are unusual but can occur when users run yard test-examples against a project that mixes Ruby and native extensions.

Current behaviour

NoMethodError: undefined method 'join' for nil
  from lib/yard/cli/test_examples.rb:…:in 'build_spec'

The crash surfaces as an unhandled exception from generate_tests, aborting the entire run rather than just skipping the offending example.

Proposed solution

Guard against a missing first entry and fall back to a placeholder:

spec.filepath = if (file_info = example.object.files.first)
                  "#{Dir.pwd}/#{file_info.join(':')}"
                else
                  example.object.path
                end

Or more concisely:

spec.filepath = example.object.files.first&.then { |f| "#{Dir.pwd}/#{f.join(':')}" } ||
                example.object.path

The fallback of the YARD path ensures failure messages still identify the object even when no file location is available.

Acceptance criteria

  • build_spec does not raise when example.object.files is empty
  • A meaningful value is still assigned to spec.filepath
  • All existing cucumber scenarios continue to pass

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions