Skip to content
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

exception from ArgsRule.matchtasks (playbook): attempted relative import with no known parent package #4270

Open
CAAHS opened this issue Jul 24, 2024 · 3 comments
Assignees
Labels

Comments

@CAAHS
Copy link

CAAHS commented Jul 24, 2024

Summary

Running ansible-lint(1) emits a diagnostic message:

WARNING Ignored exception from ArgsRule.matchtasks while processing plays/fact-cache.yml (playbook): attempted relative import with no known parent package

Issue Type
  • Bug Report
OS / ENVIRONMENT
$ ansible-lint --version
ansible-lint 24.7.0 using ansible-core:2.17.2 ansible-compat:24.7.0 ruamel-yaml:0.18.6 ruamel-yaml-clib:0.2.8
  • ansible installation method: pip
  • ansible-lint installation method: pip (same)
STEPS TO REPRODUCE

We use the ansible-lint version as documented and the following plays/fact-cache.yml (playbook):

#!/usr/bin/env ansible-playbook
---

- name: 'Refresh Fact Cache'
  hosts: all
  tasks:
    - name: Refresh all machine facts from the cache
      setup:
        gather_subset: all
        gather_timeout: 10
Desired Behavior

The above documented diagnostics are not emitted.

Actual Behavior

The above documented diagnostics are emitted.

Excerpt from extra verbosity:

$ ansible-lint fixtures plays roles -vvvv
...
DEBUG    Running rule args
WARNING  Ignored exception from ArgsRule.matchtasks while processing plays/fact-cache.yml (playbook): attempted relative import with no known parent package
DEBUG    Ignored exception details
Traceback (most recent call last):
  File "/.../python-3.10/lib/python3.10/site-packages/ansiblelint/_internal/rules.py", line 94, in getmatches
    matches.extend(method(file))
  File "/.../python-3.10/lib/python3.10/site-packages/ansiblelint/rules/__init__.py", line 178, in matchtasks
    result = self.matchtask(task, file=file)
  File "/.../python-3.10/lib/python3.10/site-packages/ansiblelint/rules/args.py", line 161, in matchtask
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/.../python-3.10/lib/python3.10/site-packages/ansible/modules/setup.py", line 174, in <module>
    from ..module_utils.basic import AnsibleModule
ImportError: attempted relative import with no known parent package
...

As we're getting a stack trace, also the output of the Ansible playbook syntax check command:

$ ansible-playbook --syntax-check plays/fact-cache.yml

playbook: plays/fact-cache.yml

As we've searched your forge, a reference we best guessed for context: https://github.com/ansible/ansible-lint/pull/4216/files/1b424b5122b3158df808fcde4bdd40c40474dc49#diff-ade5ce2a08eeaf42cfeb0027865a0b7cd2c987da9af081d6604ce9d937d13bd0L146-L148

@corubba
Copy link
Contributor

corubba commented Jul 27, 2024

I had a quick look at this, and noticed the following: The error only occurs when the non-full-qualified task name setup is used, it works fine when using the full-qualified ansible.builtin.setup. In both cases the value of (pre #4216 used) resolved_fqcn is ansible.builtin.setup, but the value of (post #4216 used) plugin_resolved_name differs: it's setup in the non-fq case, and ansible_collections.ansible.builtin.plugins.modules.setup in the fq case.

EDIT: A very hacky/bad solution that seems to work

--- a/src/ansiblelint/rules/args.py
+++ b/src/ansiblelint/rules/args.py
@@ -146,4 +146,4 @@ class ArgsRule(AnsibleLintRule):
             spec = importlib.util.spec_from_file_location(
-                name=loaded_module.plugin_resolved_name,
+                name=loaded_module.plugin_resolved_name if '.' in loaded_module.plugin_resolved_name else loaded_module.resolved_fqcn,
                 location=loaded_module.plugin_resolved_path,
             )

@CAAHS
Copy link
Author

CAAHS commented Jul 29, 2024

Thanks for taking a look @corubba

We can confirm the observation that the module name (task name) is triggering it, and that using a fully qualified name is w/o the diagnostics.

This also already strengthened our first impression towards that this might be a regression when introducing plugin_resolved_name.

We can further confirm that your provided patch is w/o the diagnostic message as well for the (not fully-qualified) name.

  • file patched: .direnv/python-3.10/lib64/python3.10/site-packages/ansiblelint/rules/args.py

Putting your own judgement of the patch aside for a brief moment, what do you think about adding this reports reproducer as a test case that tests for the flaw in support/context so it is already captured? This could also help to prepare a better patch in a more controlled manner. Awaiting your thoughts. @corubba

@corubba
Copy link
Contributor

corubba commented Aug 2, 2024

To be perfectly honest, I don't know. Someone smarter than me needs to have a look at this.

@Qalthos Qalthos removed the new Triage required label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Roadmap
Development

No branches or pull requests

3 participants