Skip to content

Fix runtime cache folder error when building with pyinstaller #805

Merged
lucemia merged 1 commit intolivingbio:mainfrom
rudcode:main
Feb 12, 2026
Merged

Fix runtime cache folder error when building with pyinstaller #805
lucemia merged 1 commit intolivingbio:mainfrom
rudcode:main

Conversation

@rudcode
Copy link
Contributor

@rudcode rudcode commented Feb 5, 2026

When building executable binary through pyinstaller, the resulting binary will fail because the cache directory could not be found. Use system temp directory instead to fix this using python builtin tempfile library.

@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 79.98%. Comparing base (27cc312) to head (170f9ba).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/ffmpeg/common/cache.py 88.88% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #805      +/-   ##
==========================================
+ Coverage   79.97%   79.98%   +0.01%     
==========================================
  Files          51       51              
  Lines        3425     3432       +7     
==========================================
+ Hits         2739     2745       +6     
- Misses        686      687       +1     
Flag Coverage Δ
3.10 79.98% <88.88%> (+0.01%) ⬆️
3.11 79.95% <88.88%> (+0.01%) ⬆️
3.12 79.95% <88.88%> (+0.01%) ⬆️
backend 79.98% <88.88%> (+0.01%) ⬆️
python 79.98% <88.88%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/ffmpeg/common/cache.py 93.33% <88.88%> (-2.32%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR changes how typed-ffmpeg locates and populates its bundled cache so frozen/PyInstaller builds can run without failing due to missing on-disk cache directories.

Changes:

  • Move the runtime cache_path from a package-relative directory to a system temp directory.
  • Add import-time initialization that copies bundled cache files into the temp cache directory via importlib.resources.

Comment on lines 13 to 15
cache_path = Path(tempfile.gettempdir()) / "ffmpeg_cache"
cache_path.mkdir(exist_ok=True)

Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

cache_path is a fixed, predictable directory under the system temp dir. On multi-user systems (and especially if the process ever runs with elevated privileges), using a shared temp path can be vulnerable to symlink/hardlink tricks and can also cause cross-user/process cache corruption. Consider using a per-user (and ideally per-version) subdirectory and validating that cache_path is a real directory (not a symlink) before writing/copying into it.

Copilot uses AI. Check for mistakes.
Comment on lines 13 to 14
cache_path = Path(tempfile.gettempdir()) / "ffmpeg_cache"
cache_path.mkdir(exist_ok=True)
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

Changing the cache location to the system temp dir affects non-PyInstaller/dev workflows too: the code-gen scripts in src/scripts/ use ffmpeg.common.cache.save() to (re)generate the checked-in cache data, but with this change they will write to the temp dir instead of the repo/package cache directory. Consider making the cache base dir configurable (e.g., env var) and/or only falling back to temp when the packaged cache/ directory isn’t available (such as in a frozen/PyInstaller build).

Copilot uses AI. Check for mistakes.
Comment on lines 17 to 31
def _initialize_builtin_cache() -> None:
"""Copy built-in cache files to temp directory if they don't exist."""
with importlib.resources.as_file(
importlib.resources.files("ffmpeg.common").joinpath("cache")
) as builtin_cache_path:
if builtin_cache_path.exists():
for item in builtin_cache_path.rglob("*"):
if item.is_file():
dest_path = cache_path / item.relative_to(builtin_cache_path)
dest_path.parent.mkdir(parents=True, exist_ok=True)
if not dest_path.exists():
_ = shutil.copy2(item, dest_path)


_initialize_builtin_cache()
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

_initialize_builtin_cache() runs at import time and performs an unconditional rglob over the bundled cache tree (which is large) on every import, even when everything is already copied. This adds avoidable startup overhead and import-time side effects. Consider making initialization lazy (run on first load/save) and/or using a marker file/version check so you can skip the directory traversal once initialization is complete.

Copilot uses AI. Check for mistakes.
_ = shutil.copy2(item, dest_path)


_initialize_builtin_cache()
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

No tests currently cover the new built-in cache initialization/copying path (the behavior added here and especially relevant for frozen/PyInstaller builds). Consider adding a unit test that imports the module in an isolated temp dir and asserts the expected bundled cache artifacts are available (e.g., load(list[FFMpegOption], "options") succeeds) without relying on preexisting state in the system temp directory.

Copilot generated this review using guidance from repository custom instructions.
@rudcode
Copy link
Contributor Author

rudcode commented Feb 12, 2026

Sorry for overcomplicating things, the solution is actually this simple

https://pyinstaller.org/en/stable/runtime-information.html

@rudcode rudcode changed the title Use system temp for cache directory Fix runtime cache folder error when building with pyinstaller Feb 12, 2026
@lucemia lucemia enabled auto-merge (squash) February 12, 2026 22:13
Copy link
Contributor

@lucemia lucemia left a comment

Choose a reason for hiding this comment

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

thanks, looks good to me

@lucemia lucemia merged commit 03b7c35 into livingbio:main Feb 12, 2026
4 of 5 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