Fix upload crash when run outside project directory
Metadata
Current evaluation
No evaluation has been recorded for this issue yet.
Issue body
Fixed a bug where `charmcraft upload` crashes with `RuntimeError("Project not configured yet.")` when run outside a directory containing `charmcraft.yaml`.
## Root Cause
The `UploadCommand.needs_project()` method checks if `charmcraft.yaml` exists, but this doesn't mean the project service will be configured. After a successful upload, the code tried to check for unpublished charm libraries without first checking if the project was configured.
## Solution
Check `project_service.is_configured` before calling `project_service.get()` to ensure the project is configured before accessing it.
## Changes Made
1. Modified `UploadCommand.run()` in `charmcraft/application/commands/store.py` to check if project is configured before accessing it
2. Added unit test `test_upload_without_configured_project` in `tests/unit/commands/test_store.py` to verify the fix
3. Fixed type annotation for `Uploaded.status` field from `int` to `str` in `charmcraft/store/models.py` to resolve mypy lint error
4. Added spread test in `tests/spread/store/upload-outside-project/` for end-to-end integration testing:
- Minimal `charmcraft.yaml` file with staging store configuration and `plugin: nil` for fast builds
- `task.yaml` that uses `charmcraft init` to create a charm, copies the minimal config, packs the charm, moves it outside the project directory, and uploads it
## Testing
- **Unit test**: Verifies the fix at the code level by mocking the upload scenario without a configured project
- **Spread test**: End-to-end integration test that creates a charm with a minimal `charmcraft.yaml` using the nil plugin for fast builds, packs it in the project directory, moves the charm file to a directory without `charmcraft.yaml`, and uploads from outside the project directory to verify no crash occurs
- All 203 command tests pass
- No security vulnerabilities introduced
## Notes
The bug is in **charmcraft**, not craft-application. The craft-application library is working as designed by raising a RuntimeError when trying to get an unconfigured project.
---
- [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 crashes with internal error when uploading outside of project directory</issue_title>
> <issue_description>### Check existing issues
>
> - [x] I've verified that this bug isn't described by any existing issues.
>
> ### Bug description
>
> When performing an upload of a charm with `charmcraft upload` outside of a directory with a `charmcraft.yaml`, then the upload succeeds but charmcraft crashes at the end with `RuntimeError("Project not configured yet.")`. This was not present with earlier versions (< 4.x)
>
> ### Steps to reproduce
>
> Take a `.charm` and run `charmcraft upload ./my.charm` from outside the directory where the `charmcraft.yaml` is located
>
> ### Environment
>
> Running on Ubuntu 25.10
>
> ### charmcraft.yaml
>
> ```yaml
> n/a
> ```
>
> ### Log output
>
> ```shell
> simon@ersa ~/W/anbox-cloud-charms (main)> charmcraft upload --release 1.28/edge --name aar ./aar_ubuntu@22.04-amd64.charm
> charmcraft internal error: RuntimeError('Project not configured yet.')
> Full execution log: '/home/simon/.local/state/charmcraft/log/charmcraft-20251126-161538.963709.log'
>
> simon@ersa ~/W/anbox-cloud-charms (main) [70]> cat '/home/simon/.local/state/charmcraft/log/charmcraft-20251126-161538.963709.log'
> 2025-11-26 16:15:38.964 Starting charmcraft, version 4.0.1
> ....
> 2025-11-26 16:15:45.461 Setting up ProjectService
> 2025-11-26 16:15:45.461 charmcraft internal error: RuntimeError('Project not configured yet.')
> 2025-11-26 16:15:45.465 Traceback (most recent call last):
> 2025-11-26 16:15:45.465 File "/snap/charmcraft/7517/lib/python3.12/site-packages/craft_application/application.py", line 663, in run
> 2025-11-26 16:15:45.465 return_code = self._run_inner()
> 2025-11-26 16:15:45.465 ^^^^^^^^^^^^^^^^^
> 2025-11-26 16:15:45.465 File "/snap/charmcraft/7517/lib/python3.12/site-packages/charmcraft/application/main.py", line 143, in _run_inner
> 2025-11-26 16:15:45.465 return super()._run_inner()
> 2025-11-26 16:15:45.465 ^^^^^^^^^^^^^^^^^^^^
> 2025-11-26 16:15:45.465 File "/snap/charmcraft/7517/lib/python3.12/site-packages/craft_application/application.py", line 640, in _run_inner
> 2025-11-26 16:15:45.465 return_code = dispatcher.run() or os.EX_OK
> 2025-11-26 16:15:45.465 ^^^^^^^^^^^^^^^^
> 2025-11-26 16:15:45.465 File "/snap/charmcraft/7517/lib/python3.12/site-packages/craft_cli/dispatcher.py", line 564, in run
> 2025-11-26 16:15:45.465 return self._loaded_command.run(self._parsed_command_args)
> 2025-11-26 16:15:45.465 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2025-11-26 16:15:45.465 File "/snap/charmcraft/7517/lib/python3.12/site-packages/charmcraft/application/commands/store.py", line 586, in run
> 2025-11-26 16:15:45.465 if project := self._services.get("project").get():
> 2025-11-26 16:15:45.465 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2025-11-26 16:15:45.465 File "/snap/charmcraft/7517/lib/python3.12/site-packages/craft_application/services/project.py", line 605, in get
> 2025-11-26 16:15:45.465 raise RuntimeError("Project not configured yet.")
> 2025-11-26 16:15:45.466 RuntimeError: Project not configured yet.
> 2025-11-26 16:15:45.466 Full execution log: '/home/simon/.local/state/charmcraft/log/charmcraft-20251126-161538.963709.log'
> ```</issue_description>
>
> <agent_instructions>1. Write a test that checks whether this bug is still relevant with the latest main branch.
> 2. Determine whether the bug is in charmcraft or in craft-application.
> 3. If the bug is in charmcraft, not craft-application, write a fix for it.</agent_instructions>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
- Fixes canonical/charmcraft#2492
<!-- 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
No evaluation history available.