Fix ansible-doc -l/-F/--metadata-dump for relative imports in filter/test plugins (#85801)

* Add test for sidecar documentation for filter plugin in a subdirectory

Fix ansible-doc --list/--list_files/--metadata-dump for relative imports in nested filter/test plugin files
This commit is contained in:
Sloane Hertel 2025-09-15 18:15:55 -04:00 committed by GitHub
parent ced362a50d
commit 5e8815b823
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 26 additions and 4 deletions

View File

@ -0,0 +1,2 @@
bugfixes:
- ansible-doc --list/--list_files/--metadata-dump - fixed relative imports in nested filter/test plugin files (https://github.com/ansible/ansible/issues/85753).

View File

@ -105,18 +105,25 @@ def _list_plugins_from_paths(ptype, dirs, collection, depth=0, docs=False):
]):
continue
resource_dir = to_native(os.path.dirname(full_path))
resource_name = get_composite_name(collection, plugin, resource_dir, depth)
if ptype in ('test', 'filter'):
# NOTE: pass the composite resource to ensure any relative
# imports it contains are interpreted in the correct context
if collection:
resource_name = '.'.join(resource_name.split('.')[2:])
try:
file_plugins = _list_j2_plugins_from_file(collection, full_path, ptype, plugin)
file_plugins = _list_j2_plugins_from_file(collection, full_path, ptype, resource_name)
except KeyError as e:
display.warning('Skipping file %s: %s' % (full_path, to_native(e)))
continue
for plugin in file_plugins:
plugin_name = get_composite_name(collection, plugin.ansible_name, os.path.dirname(to_native(full_path)), depth)
plugin_name = get_composite_name(collection, plugin.ansible_name, resource_dir, depth)
plugins[plugin_name] = full_path
else:
plugin_name = get_composite_name(collection, plugin, os.path.dirname(to_native(full_path)), depth)
plugin_name = resource_name
plugins[plugin_name] = full_path
else:
display.debug("Skip listing plugins in '{0}' as it is not a directory".format(path))

View File

@ -2,7 +2,9 @@
from __future__ import annotations
from ansible.utils.display import Display
from ansible_collections.testns.testcol.plugins.module_utils import Display
# Test for https://github.com/ansible/ansible/issues/85754
from ...module_utils import Display
display = Display()

View File

@ -0,0 +1,7 @@
DOCUMENTATION:
description: filter plugin in a subdirectory
author: ansible-core
options:
_input:
description: input data, which does nothing
type: raw

View File

@ -0,0 +1,3 @@
from __future__ import annotations
from ansible.utils.display import Display

View File

@ -243,6 +243,7 @@ echo "testing sidecar docs for jinja plugins"
[ "$(ansible-doc -t test --playbook-dir ./ testns.testcol.yolo| wc -l)" -gt "0" ]
[ "$(ansible-doc -t filter --playbook-dir ./ donothing| wc -l)" -gt "0" ]
[ "$(ansible-doc -t filter --playbook-dir ./ ansible.legacy.donothing| wc -l)" -gt "0" ]
[ "$(ansible-doc -t filter --playbook-dir ./ testns.testcol.filter_subdir.nested| wc -l)" -gt "0" ]
echo "testing no docs and no sidecar"
ansible-doc -t filter --playbook-dir ./ nodocs 2>&1| grep "${GREP_OPTS[@]}" -c 'missing documentation' || true