ITADN

ValueError: path is on mount 'C:', start on mount 'D:' in 在optimize plugin on Windows with cross-drive project paths

#8558Closeddjqts 创建于 2026-01-18
bugnot feasible
D
djqtscommented
### Context When the optimize plugin (enabled by default in Material for MkDocs) is active and the project resides on a non-system drive (e.g., D:), running mkdocs serve or mkdocs build fails during image optimization—particularly for PNG files—with the following error: ``` ValueError: path is on mount 'C:', start on mount 'D:' ``` The full traceback points to: ``` File ".../material/plugins/optimize/plugin.py", line 318, in _optimize_image_png path = os.path.relpath(file.abs_src_path, docs) File "<frozen ntpath>", line 807, in relpath ``` ### Bug description 🔍 Root Cause Analysis This error occurs because Python’s os.path.relpath() cannot compute relative paths across different Windows drives (e.g., from C: to D:), and raises a ValueError. In the optimize plugin: Image processing libraries (e.g., Pillow) are used to optimize images. During optimization (e.g., format conversion or compression), temporary files are created in the system temp directory—typically on C:. The plugin then attempts to call os.path.relpath(temp_file_path, docs_dir) to resolve a relative path for internal tracking. Since temp_file_path is on C: while docs_dir is on D:, relpath() fails → build crashes. !!!Note: This happens even if all source files (including images) are located on D:. The issue stems from the plugin’s internal use of temporary files on a different drive. ✅ Verified Workaround The issue can be completely avoided by disabling PNG optimization in mkdocs.yml: ```yaml plugins: - search - optimize: optimize_png: false # ← Critical: disables problematic PNG processing ``` Further testing shows: The error only occurs when optimize_png: true (the default), which triggers the _optimize_image_png function containing the faulty relpath call. Setting optimize_png: false skips PNG optimization entirely, avoiding temporary file handling and preventing the crash. # The repair suggestions were generated by AI and I'm not quite sure about their accuracy. I hope it can be of some reference. 🛠 Suggested Fixes To resolve this robustly and improve cross-platform compatibility, consider: Avoid os.path.relpath in cross-drive scenarios 1. Use pathlib.Path with .resolve() for safer path resolution, or catch ValueError and fall back to absolute paths. 2. Add drive-letter compatibility check Before calling relpath, verify that both paths share the same drive using: ```python if os.path.splitdrive(path)[0] != os.path.splitdrive(start)[0]: # handle cross-drive case (e.g., skip relpath or log warning) ``` 💬 Additional Notes This issue does not occur on Linux or macOS (which lack drive letters), but is highly reproducible on Windows in common multi-drive development setups. Fixing it would significantly improve the developer experience for Windows users. Thank you for creating such an excellent theme! We’d greatly appreciate your attention to this issue and hope to see a fix in a future release. 🙏 ### Related links - [Reporting a bug](https://squidfunk.github.io/mkdocs-material/contributing/reporting-a-bug/) - ### Reproduction Sorry, I didn't do it. ``` ValueError: path is on mount 'C:', start on mount 'D:' File ".../material/plugins/optimize/plugin.py", line 318, in _optimize_image_png path = os.path.relpath(file.abs_src_path, docs) File "<frozen ntpath>", line 807, in relpath ``` ### Steps to reproduce Sorry, I didn't do it. ### Browser _No response_ ### Before submitting - [x] I have read and followed the [bug reporting guidelines](https://squidfunk.github.io/mkdocs-material/contributing/reporting-a-bug/). - [x] I have attached links to [the documentation](https://squidfunk.github.io/mkdocs-material/), and possibly related [issues](https://github.com/squidfunk/mkdocs-material/issues) and [discussions](https://github.com/squidfunk/mkdocs-material/discussions). - [x] I assure that I have [removed all customizations](https://squidfunk.github.io/mkdocs-material/contributing/reporting-a-bug/#remove-customizations) before submitting this bug report. - [x] I have attached a __.zip file__ with a [minimal reproduction](https://squidfunk.github.io/mkdocs-material/guides/creating-a-reproduction/) using the [built-in info plugin](https://squidfunk.github.io/mkdocs-material/plugins/info/).
关闭于 2026-01-18 1 条评论