Skip to content

feat: Fix import error (issue #6924) - #6926

Merged
blackboxsw merged 1 commit into
canonical:mainfrom
kajiya3141:fix-importlib
Jul 10, 2026
Merged

feat: Fix import error (issue #6924)#6926
blackboxsw merged 1 commit into
canonical:mainfrom
kajiya3141:fix-importlib

Conversation

@kajiya3141

@kajiya3141 kajiya3141 commented Jul 8, 2026

Copy link
Copy Markdown
Member

Proposed Commit Message

feat: Fix import error (issue #6924)

importer.py calls importlib.util.find_spec() but the file only actually imports importlib
by itself.

Up until now we've gotten away with it as something else has always imported importlib.util for us "by accident", but if an end consumer imports cloud-init as a library (i.e. ubuntu-pro-client) there's no guarantee that will happen. It happened in #6924 so let's fix it now.

Test Steps

Tested in a LP build with the same functional change in our bootstraps:

IMPORTER_PATH="mountpoint_gce/usr/lib/python3/dist-packages/cloudinit/importer.py"

if grep -qx 'import importlib' "$IMPORTER_PATH"; then
  sed -i 's/^import importlib$/import importlib.util/' "$IMPORTER_PATH"
fi

# recompile it so the shipped .pyc matches the patched source
chroot mountpoint_gce py3compile /usr/lib/python3/dist-packages/cloudinit/importer.py

# Now actually try the path that crashed so the build will fail loudly if the workaround
# is missing and/or broken
chroot mountpoint_gce python3 -c "from cloudinit import mergers; mergers.construct(mergers.default_mergers())"

it builds fine and auto-attach is now fixed 🥳

`importer.py` calls `importlib.util.find_spec()`
but the file only actually imports `importlib`
by itself.

Up until now we've gotten away with it as something
else has always imported `importlib.util` for us
"by accident", but if an end consumer imports
`cloud-init` as a library (i.e. `ubuntu-pro-client`)
there's no guarantee that will happen. It happened
in canonical#6924 so let's fix it now.
@kajiya3141
kajiya3141 marked this pull request as ready for review July 8, 2026 22:11

@blackboxsw blackboxsw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @kajiya314~1 This is the correct and least impact fix to make.

I verified failure path using the following procedure

lxc launch ubuntu-minimal:bionic b-min
lxc exec b-min -- apt install ubuntu-pro-client
lxc exec b-min -- python3 /usr/lib/ubuntu-advantage/auto_attach.py
...
  File "/usr/lib/python3/dist-packages/cloudinit/importer.py", line 52, in find_module
    if not importlib.util.find_spec(full_path):
AttributeError: module 'importlib' has no attribute 'ut=il'

With your patch applied no traceback:

$ python3 /usr/lib/ubuntu-advantage/auto_attach.py 
["2026-07-10T16:47:33.571", "ERROR", "ubuntupro.lib.auto_attach", "main", 102, "The LXD host does not allow guest auto attach", {}]
["2026-07-10T16:47:33.572", "INFO", "ubuntupro.lib.auto_attach", "main", 104, "creating flag file to trigger retries", {}]

Given that cherry picking this will target bionic images I think keeping this as simple as possible will make that backport easier as well.

@blackboxsw
blackboxsw merged commit a97e746 into canonical:main Jul 10, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants