← Back to issue list

fix: `charmcraft pack --project-dir` with metadata.yaml

View original Github issue

Metadata

Project
charmcraft
Number
#2498
Type
pull request
State
closed
Author
Copilot
Labels
Created
Updated
Closed

Current evaluation

Merged fix for charmcraft pack --project-dir failing with legacy metadata.yaml. Resolved by changing the working directory to the project directory before loading and preprocessing configuration files. Resolves issue #2493.

Suggested action:

No scores available.

Issue body

Fixes #2493 where `charmcraft pack --project-dir` fails with legacy charm projects that have separate `metadata.yaml`, `config.yaml`, and `actions.yaml` files. ## The Problem When using `--project-dir`, charmcraft couldn't find auxiliary files because: 1. craft-application looked for `charmcraft.yaml` relative to the current working directory, not the project directory 2. The preprocessing code uses `pathlib.Path.cwd()` which didn't match the project directory when using `--project-dir` ## The Solution Added `os.chdir(self.project_dir)` in `Charmcraft._run_inner()` after resolving the project directory. When `--project-dir` is specified, the application now changes the working directory to the project directory before craft-application attempts to load the project file or perform preprocessing. This approach: - Works within craft-application's existing API constraints (no staticmethod changes needed) - Ensures both finding `charmcraft.yaml` and preprocessing work correctly - Doesn't require type ignore comments or workarounds ## Changes - Modified `charmcraft/application/main.py` to change working directory when `--project-dir` is specified - Added spread test in `tests/spread/smoketests/project-dir-with-metadata/` with pre-created charm directory structure that validates end-to-end functionality of `charmcraft pack --project-dir` with metadata.yaml, config.yaml, and actions.yaml ## Testing - ✅ All 136 unit and integration tests pass - ✅ Spread test validates --project-dir with metadata.yaml, config.yaml, and actions.yaml - ✅ Python syntax validation passes --- - [x] I've followed the [contribution guidelines](https://github.com/canonical/charmcraft/blob/main/CONTRIBUTING.md). - [x] I've signed the [CLA](http://www.ubuntu.com/legal/contributors/). - [x] I've successfully run `make lint && make test`. - [ ] I've added or updated any relevant documentation. - [ ] I've updated the relevant release notes. <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> ---- *This section details on the original issue you should resolve* <issue_title>charmcraft pack fails with --project-dir and metadata.yaml</issue_title> <issue_description>### Check existing issues - [x] I've verified that this bug isn't described by any existing issues. ### Bug description <!-- Describe the bug, including any relevant context or screenshots. --> I don't see an exact match for this bug, but it certainly seems like it might be related to https://github.com/canonical/charmcraft/issues/1809. A charm defined using the old system with a separate `metadata.yaml` file can build without error when built from within the directory containing the `charmcraft.yaml` and `metadata.yaml` file but will error when built outside that directory using the `--project-dir` flag. It results in validation errors such as the following: ``` $ charmcraft pack --project-dir ./charm charmcraft internal error: 3 validation errors for PlatformCharm name Field required [type=missing, input_value={'type': 'charm', 'base':...snaps': ['astral-uv']}}}, input_type=dict] For further information visit https://errors.pydantic.dev/2.11/v/missing summary Field required [type=missing, input_value={'type': 'charm', 'base':...snaps': ['astral-uv']}}}, input_type=dict] For further information visit https://errors.pydantic.dev/2.11/v/missing description Field required [type=missing, input_value={'type': 'charm', 'base':...snaps': ['astral-uv']}}}, input_type=dict] For further information visit https://errors.pydantic.dev/2.11/v/missing ``` ### Steps to reproduce <!-- Provide step-by-step instructions to reproduce the bug. --> I can confirm this happens with a K8s charm. It may also happen with machine charm, which I have not yet tried. 1. Create a new charm with `charmcraft init --profile kubernetes` inside a directory called `charm` 2. Move the following fields from `charmcraft.yaml` to a new `metadata.yaml` file * `name` * `title` * `summary` * `containers` * `resources` 3. It may not matter, but I also deleted some fields from the `charmcraft.yaml`, such that it was left with only the following * `type` * `base` * `platforms` * `parts` 4. (Optional) From within the charm directory, run `charmcraft pack` - this should succeed. Delete the charm afterwards. 5. Move up a directory with `cd ..` 6. Now try `charmcraft pack --project-dir ./charm`. This should fail ### Environment <!-- We need to understand the context in which Charmcraft failed. Please let us know if you are running Charmcraft in destructive mode, whether you are using LXD or Multipass, and which operating system you are running Charmcraft on. --> I am using Ubuntu 24.04. Charmcraft is using LXD to build. ### charmcraft.yaml ```yaml <!-- If the issue is specific to a project, share its `charmcraft.yaml`. This field automatically renders as YAML, so you don't need to add special formatting. --> ``` ### Log output ```shell 2025-11-27 15:14:29.415 Starting charmcraft, version 4.0.1 2025-11-27 15:14:29.415 Log verbosity level set to BRIEF 2025-11-27 15:14:29.417 Preparing application... 2025-11-27 15:14:29.417 Setting up ConfigService 2025-11-27 15:14:29.434 Configuring application... 2025-11-27 15:14:29.434 Getting runtime directory. 2025-11-27 15:14:29.434 Using '/run/user/1738809480/3700465' for the state directory. 2025-11-27 15:14:29.434 Setting up StateService 2025-11-27 15:14:29.434 Setting 'charmcraft.started_at' to '2025-11-27T15:14:29.434239'. 2025-11-27 15:14:29.434 Loading state file '/run/user/1738809480/3700465/charmcraft.yaml'. 2025-11-27 15:14:29.434 State file doesn't exist. 2025-11-27 15:14:29.434 Writing state to '/run/user/1738809480/3700465/charmcraft.yaml'. 2025-11-27 15:14:29.434 Set 'charmcraft.started_at' to '2025-11-27T15:14:29.434239'. 2025-11-27 15:14:29.435 Configuring application... 2025-11-27 15:14:29.435 Setting 'charmcraft.started_at' to '2025-11-27T15:14:29.435183'. 2025-11-27 15:14:29.435 Loading state file '/run/user/1738809480/3700465/charmcraft.yaml'. 2025-11-27 15:14:29.435 Overwriting existing value. 2025-11-27 15:14:29.435 Writing state to '/run/user/1738809480/3700465/charmcraft.yaml'. 2025-11-27 15:14:29.435 Set 'charmcraft.started_at' to '2025-11-27T15:14:29.435183'. 2025-11-27 15:14:29.465 Build plan: platform=None, build_for=None 2025-11-27 15:14:29.466 Setting up ProjectService 2025-11-27 15:14:29.466 Loading project file '/home/william.taylor@canonical.com/charm/charmcraft.yaml 2025-11-27 15:14:29.467 Running charmcraft pack on host 2025-11-27 15:14:29.467 Using parallel build count of 16 from CPU count 2025-11-27 15:14:29.467 Checking if 'version' exists. 2025-11-27 15:14:29.467 Getting value for 'version'. 2025-11-27 15:14:29.467 Got None (updated=False) for 'version'. 2025-11-27 15:14:29.467 'version' exists. 2025-11-27 15:14:29.467 Getting value for 'version'. 2025-11-27 15:14:29.467 Got None (updated=False) for 'version'. 2025-11-27 15:14:29.467... </details> - Fixes canonical/charmcraft#2493 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

Evaluation history

Date Model Scores Action Summary
qwen/qwen3.6-35b-a3b Merged fix for charmcraft pack --project-dir failing with legacy metadata.yaml. Resolved by changing the working directory to the project directory before loading and preprocessing configuration files. Resolves issue #2493.
qwen3.6-35b-a3b-mtp-q6 Merged fix for #2493 resolving charmcraft pack --project-dir failures with legacy metadata.yaml. Added os.chdir(self.project_dir) in _run_inner() to align cwd with the project path, ensuring craft-application locates auxiliary files. All tests pass.
qwen3.6-35b-a3b-mtp-q6 Fixed charmcraft pack --project-dir failures for legacy charms with separate metadata files. Resolved by changing the working directory to the project path before loading configuration. Merged after all tests passed.

Update history

No update history recorded yet.

Related issues

No related issues found above the similarity threshold.