annotaterb icon indicating copy to clipboard operation
annotaterb copied to clipboard

Model annotation chokes on an empty file

Open barrywoolgar opened this issue 11 months ago • 1 comments

Hello, thanks for taking on the abandoned annotate gem and carrying it forward.

In my project I have a model test file which (scandalously) contains only one empty line. When forcing the model annotations to be generated it will fail because line_number_after is nil:

root@43a5d2fdec98:/app# annotaterb models --force
Annotating models
/usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/annotated_file/generator.rb:70:in 'AnnotateRb::ModelAnnotator::AnnotatedFile::Generator#content_annotated_after': undefined method '+' for nil (NoMethodError)

          content_with_annotations_written_after << content_without_annotations.lines[(line_number_after + 1)..]
                                                                                                         ^
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/annotated_file/generator.rb:39:in 'AnnotateRb::ModelAnnotator::AnnotatedFile::Generator#generate'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/single_file_annotator.rb:46:in 'AnnotateRb::ModelAnnotator::SingleFileAnnotator.call'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/single_file_annotator.rb:8:in 'AnnotateRb::ModelAnnotator::SingleFileAnnotator.call_with_instructions'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/project_annotator.rb:22:in 'block in AnnotateRb::ModelAnnotator::ProjectAnnotator#annotate'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/project_annotator.rb:21:in 'Array#map'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/project_annotator.rb:21:in 'AnnotateRb::ModelAnnotator::ProjectAnnotator#annotate'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/annotator.rb:21:in 'AnnotateRb::ModelAnnotator::Annotator#do_annotations'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/model_annotator/annotator.rb:8:in 'AnnotateRb::ModelAnnotator::Annotator.do_annotations'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/commands/annotate_models.rb:17:in 'AnnotateRb::Commands::AnnotateModels#call'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/runner.rb:24:in 'AnnotateRb::Runner#run'
	from /usr/local/bundle/gems/annotaterb-4.13.0/lib/annotate_rb/runner.rb:7:in 'AnnotateRb::Runner.run'
	from /usr/local/bundle/gems/annotaterb-4.13.0/exe/annotaterb:18:in '<top (required)>'
	from /usr/local/bundle/bin/annotaterb:25:in 'Kernel#load'
	from /usr/local/bundle/bin/annotaterb:25:in '<main>'

Obviously the file shouldn't have been empty, so I've just deleted it for now, but presumably somewhere along the line there needs to be something that forces line_number_after (and maybe line_number_before?) to be an integer (and zero in this case).

Happy to do a PR but wanted to check you were happy with the suggestion first. Cheers

barrywoolgar avatar Jan 28 '25 14:01 barrywoolgar

Thanks for reporting @barrywoolgar. That does seem like an edge case that's not currently supported in the annotation generator. If you want to submit a PR for this that would be helpful.

drwl avatar Feb 17 '25 02:02 drwl

I created a pull request because many members on my team were getting stuck on this issue without understanding the cause. I would appreciate it if you could review it. @drwl @barrywoolgar

tanukiti1987 avatar Jun 23 '25 09:06 tanukiti1987