providers: refactor `launched_environment()`
Metadata
Current evaluation
Merged refactoring of launched_environment() to remove snapcraft-specific logic and align with rockcraft. Implemented standardized base translation for craft-providers. Approved and merged following successful CI checks.
Suggested action: —
No scores available.
Issue body
snapcraft-specific code in `launched_environment()` is removed from the craft-providers interface.
- [X] Have you followed the [guidelines for contributing](https://github.com/snapcore/snapcraft/blob/master/CONTRIBUTING.md)?
- [X] Have you signed the [CLA](http://www.ubuntu.com/legal/contributors/)?
- [X] Have you successfully run `make lint`?
- [X] Have you successfully run `pytest tests/unit`?
-----
### Overview
Removes snapcraft-specific logic from `launched_environment()`. This function is now matches the implementation in rockcraft ([lxd](https://github.com/canonical/rockcraft/blob/97306bc3d93c8a41f18432b1dadf4569c7f28854/rockcraft/providers/_lxd.py#L99) and [multipass](https://github.com/canonical/rockcraft/blob/97306bc3d93c8a41f18432b1dadf4569c7f28854/rockcraft/providers/_multipass.py#L89))
This is the most complex PR of the series, due to the translation of bases. The base translation was added in [this PR](https://github.com/canonical/rockcraft/pull/85) to rockcraft.
### Base Translation
#### Background
*craft applications have a concept of a "base". This base is translated a `craft-providers` base and is passed to `craft-providers` so it can choose which base image (OS and release) to use for the instance.
#### Problem
Currently, each application translates its own base inside `_lxd.py` and `_multipass.py`. This application-specific code needs to be removed from these files.
#### Solution
To support this, I created a list of what bases craft-providers can accept. Right now, `18.04`, `20.04`, and `22.04` are supported. Other OSs and releases can be added in the future without breaking backwards compatibility.
The image below shows how an application can translates its base to a `craft-providers` base. It also shows `craft-providers` translating it to the specific name needed for LXD and Multipass to download the base image.

### Note about the changeset
This PR shows ` 10 files changed, 768 insertions(+), 274 deletions(-)`, but it's much smaller. It's really `8 files changed, 164 insertions(+), 122 deletions(-)`.
This is because I copied over rockcraft's files `tests/unit/providers/test_{lxd,multipass}.py`. These files have already been reviewed and are going to be migrated to `craft-providers` later this week. Thus, these unit test files probably don't need intense scrutiny.
(CRAFT-1359)
Evaluation history
| Date | Model | Scores | Action | Summary |
|---|---|---|---|---|
| qwen/qwen3.6-35b-a3b | — | — | Merged refactoring of launched_environment() to remove snapcraft-specific logic and align with rockcraft. Implemented standardized base translation for craft-providers. Approved and merged following successful CI checks. | |
| qwen3.6-35b-a3b-mtp-q6 | — | — | Merged refactoring of launched_environment() in craft-providers to remove snapcraft-specific logic and align with rockcraft. Added centralized base translation mapping and incorporated rockcraft unit tests. | |
| qwen3.6-35b-a3b-mtp-q6 | — | — | Merged a refactor of launched_environment that removes snapcraft-specific logic and aligns it with rockcraft. Introduces centralized base translation for LXD and Multipass, streamlining the craft-providers interface. |
Update history
No update history recorded yet.
Related issues
| Issue | Project | State | Summary | Similarity |
|---|---|---|---|---|
| #3929 providers: refactor clean_project_environments | snapcraft | merged | Merged a refactor to the craft-providers interface, removing get_instance_name() from clean_project_environments() and renaming is_provider_available() to is_provider_installed(). Aligns with rockcraft, passed CI, and increased coverage by 0.13%. | |
| #890 (internal) Refactor launched_environment (CRAFT-1358). | charmcraft | merged | Refactored the launched_environment module (CRAFT-1358). Approved by one reviewer and passing CI, the changes were successfully merged. The update involved 248 additions and 243 deletions across 10 files. | |
| #110 providers: move lxd project name out of craft-providers interface | rockcraft | merged | Merged refactoring that moves rockcraft's project name from lxd.py to providers.py in the craft-providers interface. Approved by one reviewer, passed CI, and successfully integrated. | |
| #409 providers: introduce launch_environment() (CRAFT-312) | charmcraft | merged | Merged. Introduces launch_environment() to launch and configure an LXC container for charmcraft, including buildd remote setup. Approved by two reviewers, adding 163 lines across two files. |