-
-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No warning when overriding namespace documentation #1173
Comments
I think this would create a breaking change in expected behavior for those using I believe this is something you could create a plugin or extension for without needing any modification of core. Specifically, you can use YARD::DocstringParser.after_parse to determine if you've duplicated a docstring and emit a warning; you would have to keep a manual tally, but it's doable. |
I would expect that those who use However, if you are worried about backwards compatibility, then perhaps it could be disabled by default but have a configuration that could turn on these warnings. That way the default could just be switched on the next release that makes breaking changes. The configuration could also be removed entirely on that release with breaking changes, unless you see a use case for not having these warnings other than backwards compatibility. Thanks for pointing me in the right direction working around this. I hadn't looked into that yet since I expected this to be desirable to be fixed as part of core. |
Documentation problems, yes-- but like this is less clear. There are legitimate usages for re-documenting the same module or class across multiple files, for instance, if you separate multiple packages as individual gems for release but generate separate documentation across all sub-packages at once. Users who use YARD in this way would start seeing failures where they previously had none. YARD historically hasn't been directly hands on about enforcing best practices or documentation style and instead left these types of linting tools up to downstream libraries like yardstick et al, which can be more focused on solving those specific problems. Warnings in YARD are typically only emitted in cases where YARD is hindered from doing its own job of parsing or generating documentation, with the arguable exception of warnings for parameter names. In this specific case, though, YARD isn't really hindered from generating docs, and it's not even a question of undocumented code, since it is in fact documented. You'll notice that YARD also doesn't warn about duplicate tags, and other dupe cases where it could, but YARD itself isn't the linter. For that reason I believe you would have a lot more control over checking this type of use case via plugin or extension. |
Ok, thanks for the added context. I'll close it sounds like it is considered out of scope for this project. |
I can confirm that In case someone else comes across the same problem, I'm currently loading the following script in yardoc using the # Prevent docstrings from being overridden
#
# This is particularly a concern for top-level namespaces,
# since a file defining a constant under the namespace could
# override the top-level namespaces documentation by putting
# that constant at the top of the file rather than preceding
# the nested constant defined by that file.
documented_objects = {}
YARD::DocstringParser.after_parse do |parser|
next if (parser.raw_text || '').empty?
object = parser.object
next unless object && parser.handler
previous_file = documented_objects[object]
current_file = parser.handler.parser.file
if previous_file
YARD::Logger.instance.warn(
"Documentation for #{object.type} #{object.path} from #{previous_file} " \
"is being replaced with one from one from #{current_file}"
)
end
documented_objects[object] = current_file
end |
Due to yard's override of docs when the docs are defined in several files for the same class ([see this issue](lsegal/yard#1173)), it's impossible to spread the docs to several files so that they are compiled into one document (e.g. a manual). Comparison using: RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge makes it impossible to put the docs on the included module level without filtering out those modules for which a badge can't be inferred, because `RuboCop::Cop::Badge.for` raises an error.
Due to yard's override of docs when the docs are defined in several files for the same class ([see this issue](lsegal/yard#1173)), it's impossible to spread the docs to several files so that they are compiled into one document (e.g. a manual). Comparison using: RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge makes it impossible to put the docs on the included module level without filtering out those modules for which a badge can't be inferred, because `RuboCop::Cop::Badge.for` raises an error.
This call to [register]() method seems to be redundant for this handler and it's causing warnings when used with this callback that ensures top namespace documentations are not overriden lsegal/yard#1173 (comment)
This call to [register](https://github.com/lsegal/yard/blob/7104257bef486424d77eddd2f1ffc6834d7ee090/lib/yard/handlers/base.rb#L407) method seems to be redundant for this handler and it's causing warnings when used with this callback that ensures top namespace documentations are not overridden lsegal/yard#1173 (comment)
* Remove redundant call to register This call to [register](https://github.com/lsegal/yard/blob/7104257bef486424d77eddd2f1ffc6834d7ee090/lib/yard/handlers/base.rb#L407) method seems to be redundant for this handler and it's causing warnings when used with this callback that ensures top namespace documentations are not overridden lsegal/yard#1173 (comment) * test to prevent regression * revert test * test refinement * settle on expect_any_instance_of
Problem
We have documented top-level namespaces like
which can easily be accidentally overridden by nested classes/modules when the documentation gets mistakenly put at the top-level instead of directly preceding the nested class. E.g.
The second documentation of the top-level module overrides the other one without any warning, such that we can't catch this in CI by using
yardoc
with the--fail-on-warning
option.Steps to reproduce
sales.rb
andsales/order_query.rb
yardoc --fail-on-warning sales.rb sales/order_query.rb
echo $?
outputs the success status0
open doc/Sales.html
and see that the overview has "description of class Sales::OrderQuery"I would expect a warning letting me know that the documentation is being overridden along with file and line information for the two places where the documentation is provided so one of them can be moved/removed or so they could be merged.
What is Happening in the Code
The docstring for the YARD::CodeObjects::ModuleObject is being replaced in YARD::Handlers::Base#register_docstring when the ModuleObject is registered for the second module in YARD::Handlers::Ruby::ModuleHandler
Proposed Solution
log.warn
could be placed beforeobject.docstring = parser.to_docstring
in YARD::Handlers::Base#register_docstring inside anif object.docstring && !object.docstring.empty?
condition. However, that trivial change would cause warnings when usingyardoc
with the--use-cache
option, since it just applies the old documentation to the cached code objects in the registry.I propose that this warning would only be logged either when
--use-cache
option isn't being usedobject
argument passed toregister_docstring
wasn't loaded from the cacheIt doesn't look like either of this information is properly exposed to the YARD::Handlers::Base object that
register_docstring
is called on. In the case of option 1, I think that could be exposed through a method on the registry. In the case of option 2, this state could be stored on the YARD::CodeObjects::Base objects either before serializing them to store them in the cache or after deserializing them from the cache. This seems like the least clear choice to make before moving forward on adding the warning.Long term, I think #1165 needs to be addressed so that this is easier to address when using the
--use-cache
option. At that point, it should be easier to distinguish between theobject
argument toregister_docstring
having an existing docstring because it was defined elsewhere (where we want a warning) or because it was loaded from the cache (where we don't want the warning).The text was updated successfully, but these errors were encountered: