Make sure ansible-doc doesn't crash when scanning collections whose path contains ansible_collections twice (#85361) (#85931)

Ref: https://github.com/ansible/ansible/issues/84909#issuecomment-2767335761



(cherry picked from commit c6d8d206af)

Co-authored-by: s-hertel <19572925+s-hertel@users.noreply.github.com>
Co-authored-by: Brian Coca <bcoca@users.noreply.github.com>
pull/86092/head
Felix Fontein 1 month ago committed by GitHub
parent 03c0e28b91
commit e511663447
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
bugfixes:
- "ansible-doc - prevent crash when scanning collections in paths that have more than one ``ansible_collections`` in it
(https://github.com/ansible/ansible/issues/84909, https://github.com/ansible/ansible/pull/85361)."

@ -231,7 +231,9 @@ class RoleMixin(object):
b_colldirs = list_collection_dirs(coll_filter=collection_filter)
for b_path in b_colldirs:
path = to_text(b_path, errors='surrogate_or_strict')
collname = _get_collection_name_from_path(b_path)
if not (collname := _get_collection_name_from_path(b_path)):
display.debug(f'Skipping invalid path {b_path!r}')
continue
roles_dir = os.path.join(path, 'roles')
if os.path.exists(roles_dir):

@ -17,8 +17,10 @@ def list_collections(coll_filter=None, search_paths=None, dedupe=True, artifacts
collections = {}
for candidate in list_collection_dirs(search_paths=search_paths, coll_filter=coll_filter, artifacts_manager=artifacts_manager, dedupe=dedupe):
collection = _get_collection_name_from_path(candidate)
collections[collection] = candidate
if collection := _get_collection_name_from_path(candidate):
collections[collection] = candidate
else:
display.debug(f'Skipping invalid collection in path: {candidate!r}')
return collections

@ -208,6 +208,13 @@ ANSIBLE_LIBRARY='./nolibrary' ansible-doc --metadata-dump --no-fail-on-errors --
output=$(ANSIBLE_LIBRARY='./nolibrary' ansible-doc --metadata-dump --playbook-dir broken-docs testns.testcol 2>&1 | grep -c 'ERROR!' || true)
test "${output}" -eq 1
# ensure --metadata-dump does not crash if the ansible_collections is nested (https://github.com/ansible/ansible/issues/84909)
testdir="$(pwd)"
pbdir="collections/ansible_collections/testns/testcol/playbooks"
cd "$pbdir"
ANSIBLE_COLLECTIONS_PATH="$testdir/$pbdir/collections" ansible-doc -vvv --metadata-dump --no-fail-on-errors
cd "$testdir"
# ensure that role doc does not fail when --no-fail-on-errors is supplied
ANSIBLE_LIBRARY='./nolibrary' ansible-doc --no-fail-on-errors --playbook-dir broken-docs testns.testcol.testrole -t role 1>/dev/null 2>&1

Loading…
Cancel
Save