Skip to content

fix(ros2pkg): replace deprecated importlib.resources.path with files/as_file#1234

Closed
darshmenon wants to merge 2 commits into
ros2:rollingfrom
darshmenon:fix/ros2pkg-deprecated-importlib-resources-path
Closed

fix(ros2pkg): replace deprecated importlib.resources.path with files/as_file#1234
darshmenon wants to merge 2 commits into
ros2:rollingfrom
darshmenon:fix/ros2pkg-deprecated-importlib-resources-path

Conversation

@darshmenon
Copy link
Copy Markdown

Problem

importlib.resources.path() has been deprecated since Python 3.11 and emits DeprecationWarning on Python 3.12, which is the interpreter used by ROS 2 Jazzy and Rolling. This means every ros2 pkg create invocation currently prints a deprecation warning on current Ubuntu 24.04 systems.

There is also a latent correctness bug: the existing code exits the with resources.path(...) context manager before using template_path, then calls os.path.exists() and _expand_template() on a path that may already be invalid. For resources packaged inside a zip (e.g. installed wheels), the temporary extracted file is deleted when the context manager exits, making the subsequent os.path.exists() check and _expand_template() call operate on a stale path.

Fix

Replace resources.path() with resources.files().joinpath() + resources.as_file(), the API recommended since Python 3.9. Move all path-dependent calls inside the as_file() context manager so the extracted file remains valid for the duration of template expansion.

# before
with resources.path(full_package, template_file_name) as path:
    template_path = str(path)
if not os.path.exists(template_path):          # path may be stale here
    raise FileNotFoundError(...)
_expand_template(template_path, ...)           # and here

# after
ref = resources.files(full_package).joinpath(template_file_name)
with resources.as_file(ref) as path:
    template_path = str(path)
    if not os.path.exists(template_path):      # path is valid inside the block
        raise FileNotFoundError(...)
    _expand_template(template_path, ...)       # and here

Test Plan

  • ros2 pkg create --build-type ament_cmake my_pkg — no DeprecationWarning on Python 3.12
  • ros2 pkg create --build-type ament_python my_pkg — all template files generated correctly
  • ros2 pkg create --build-type ament_cmake --node-name my_node my_pkg — node stub created correctly
  • Verify no regression on Python 3.10 (Humble)

resources.path() is deprecated since Python 3.11 and emits
DeprecationWarning on 3.12 (used by ROS 2 Jazzy+). Replace with
resources.files().joinpath() + resources.as_file(), which is the
recommended API since Python 3.9.

Also fixes a latent bug: the old code used template_path after exiting
the with-block, so extraction of zip-packaged resources would have
produced a stale path. All path-dependent calls are now inside the
as_file() context manager.
Copy link
Copy Markdown
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

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

same here, AI usage needs to be disclosed, please use our PR template.
besides, this PR includes unrelated difference from #1233. please include only code and changes related to the PR. do not include unrelated changes.

and DCO is missing.

@christophebedard
Copy link
Copy Markdown
Member

christophebedard commented May 16, 2026

importlib.resources.path() has been deprecated since Python 3.11 and emits DeprecationWarning on Python 3.12, which is the interpreter used by ROS 2 Jazzy and Rolling. This means every ros2 pkg create invocation currently prints a deprecation warning on current Ubuntu 24.04 systems.

Verify no regression on Python 3.10 (Humble)

This doesn't make sense. There's Kilted in between Jazzy and Rolling, and Rolling isn't on Ubuntu 24.04 anymore, it's on 26.04. And this targets Rolling, so I don't see why you'd need to check for regressions on Humble.

@christophebedard
Copy link
Copy Markdown
Member

This means every ros2 pkg create invocation currently prints a deprecation warning on current Ubuntu 24.04 systems.

I sourced Jazzy and then ran ros2 pkg create mypkg and I don't see any DeprecationWarning. Can you provide a ros2 pkg create command that results in a warning?

@darshmenon darshmenon closed this May 16, 2026
@darshmenon darshmenon deleted the fix/ros2pkg-deprecated-importlib-resources-path branch May 16, 2026 03:23
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.

3 participants