Skip to content

Commit

Permalink
src/ansible_compat/runtime.py: Fix collection loading (#223)
Browse files Browse the repository at this point in the history
* src/ansible_compat/runtime.py: Fix collection loading

- Ensure the cache dir is added to the collection loader
  path, in order to allow linting a collection role against
  a module this collection is providing

- Add missing default collection path to the loader for ansible
  version prior 2.15.0.dev0, as done by ansible's
  _configure_collection_loader(). The only remaining difference
  is the missing handling of COLLECTIONS_SCAN_SYS_PATH.

See ansible/ansible-lint#2969 for
reference.

Signed-off-by: Arnaud Patard <apatard@hupstream.com>

* Collection path is a list

---------

Signed-off-by: Arnaud Patard <apatard@hupstream.com>
Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
apatard and ssbarnea authored Feb 14, 2023
1 parent 0559d55 commit f51159b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ def _ensure_module_available(self) -> None:
# For ansible 2.15+ we need to initialize the plugin loader
# https://github.com/ansible/ansible-lint/issues/2945
if not Runtime.initialized: # noqa: F823
col_path = [f"{self.cache_dir}/collections"]
if self.version >= Version("2.15.0.dev0"):
# pylint: disable=import-outside-toplevel,no-name-in-module
from ansible.plugins.loader import init_plugin_loader

init_plugin_loader([])
init_plugin_loader(col_path)
else:
# noinspection PyProtectedMember
from ansible.utils.collection_loader._collection_finder import ( # pylint: disable=import-outside-toplevel
Expand All @@ -142,10 +143,12 @@ def _ensure_module_available(self) -> None:

# noinspection PyProtectedMember
# pylint: disable=protected-access
col_path += self.config.collections_paths
col_path += os.path.dirname(
os.environ.get(ansible_collections_path(), ".")
).split(":")
_AnsibleCollectionFinder(
paths=[
os.path.dirname(os.environ.get(ansible_collections_path(), "."))
]
paths=col_path
)._install() # pylint: disable=protected-access
Runtime.initialized = True

Expand Down

0 comments on commit f51159b

Please sign in to comment.