yard icon indicating copy to clipboard operation
yard copied to clipboard

yard server issues with custom code objects

Open ajvondrak opened this issue 4 years ago • 0 comments

I have a custom code object and a handler that adds it to the YARD registry. I'm using .yardopts to pass the -e flag pointing at this custom code.

.yardopts

issue.rb
-e custom.rb

issue.rb

module Issue
  custom 'stuff'
end

custom.rb

class CustomObject < YARD::CodeObjects::Base
  def path
    '__CustomObject' + sep + super
  end
end

class CustomHandler < YARD::Handlers::Ruby::Base
  handles method_call(:custom)

  def process
    register CustomObject.new(namespace, statement[1].source)
  end
end

Running yard doc generates static output just fine. It also generates the .yardoc/ db containing a marshalled reference to my custom code object. If I keep this db around and run yard server, the server barfs.

$ yard doc
Files:           1
Modules:         1 (    1 undocumented)
Classes:         0 (    0 undocumented)
Constants:       0 (    0 undocumented)
Attributes:      0 (    0 undocumented)
Methods:         0 (    0 undocumented)
 0.00% documented
$ yard server
>> YARD 0.9.25 documentation server at http://localhost:8808
Puma starting in single mode...
* Version 3.12.2 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:8808
Use Ctrl-C to stop
$ curl localhost:8808
undefined class/module CustomObject
yard-0.9.25/lib/yard/serializers/yardoc_serializer.rb:105:in `load'
yard-0.9.25/lib/yard/serializers/yardoc_serializer.rb:105:in `deserialize'
yard-0.9.25/lib/yard/registry_store.rb:300:in `load_root'
yard-0.9.25/lib/yard/registry_store.rb:258:in `load_yardoc'
yard-0.9.25/lib/yard/registry_store.rb:132:in `load'
yard-0.9.25/lib/yard/registry.rb:132:in `load_yardoc'
yard-0.9.25/lib/yard/server/commands/library_command.rb:155:in `load_yardoc'
yard-0.9.25/lib/yard/server/commands/library_command.rb:125:in `setup_library'
yard-0.9.25/lib/yard/server/commands/library_command.rb:88:in `call_without_fork'
yard-0.9.25/lib/yard/server/commands/library_command.rb:74:in `call'
yard-0.9.25/lib/yard/server/router.rb:141:in `route_docs'
yard-0.9.25/lib/yard/server/router.rb:148:in `route_index'
yard-0.9.25/lib/yard/server/router.rb:107:in `route'
yard-0.9.25/lib/yard/server/router.rb:56:in `call'
yard-0.9.25/lib/yard/server/rack_adapter.rb:52:in `call'
puma-3.12.2/lib/puma/configuration.rb:227:in `call'
puma-3.12.2/lib/puma/server.rb:674:in `handle_request'
puma-3.12.2/lib/puma/server.rb:476:in `process_client'
puma-3.12.2/lib/puma/server.rb:334:in `block in run'
puma-3.12.2/lib/puma/thread_pool.rb:135:in `block in spawn_thread'

This appears to be because YARD is loading the registry before the .yardopts. https://github.com/lsegal/yard/blob/ca59056a5c706ed519d1065efbf893c4d191d7f7/lib/yard/server/commands/library_command.rb#L123-L128

Swapping those two lines so that the .yardopts are setup first (thereby loading my custom code) fixes the issue.

diff --git a/lib/yard/server/commands/library_command.rb b/lib/yard/server/commands/library_command.rb
index 34b84d89..2ac244b7 100644
--- a/lib/yard/server/commands/library_command.rb
+++ b/lib/yard/server/commands/library_command.rb
@@ -122,8 +122,8 @@ module YARD

         def setup_library
           library.prepare! if request.xhr? && request.query['process']
-          load_yardoc
           setup_yardopts
+          load_yardoc
           true
         end

Any reason these steps shouldn't go in this order? Or am I missing something about how I'm supposed to load custom code?

FWIW, without the above patch, I can still force the custom code to load first by explicitly saying yard server -e custom.rb. Then loading the registry works fine and the server doesn't crash. I would of course like the yardopts to take care of it, though.

ajvondrak avatar Jul 14 '20 22:07 ajvondrak