Fix prevent dangling symlink from crashing the build#43
Merged
Conversation
shenxianpeng
commented
Apr 26, 2026
9921afd to
8e00488
Compare
A dangling symlink in the docs directory would crash `mkdocs build` with an unhandled FileNotFoundError during file copy. `os.walk` lists the symlink as a regular file, but when `shutil.copyfile` (or `os.path.getmtime`) follows the symlink to a missing target, the uncaught OSError aborts the entire build. Fix: - `is_modified()`: catch OSError when calling getmtime on the source — return False so the file is skipped silently. - `copy_file()`: catch OSError around copy and log a warning, then skip the file gracefully instead of crashing. Closes: mkdocs/mkdocs#4048
Co-authored-by: Xianpeng Shen <xianpeng.shen@gmail.com>
8e00488 to
f786f49
Compare
PR #3785 added early broken-symlink detection in utils.copy_file() that logged a warning and returned early. PR #4048 added an except OSError handler in files.py expecting the exception to propagate. Because the early return short-circuited before shutil.copyfile, the OSError handler was never reached, and the mkdocs.utils warning leaked to lastResort. Fix: raise FileNotFoundError in utils.copy_file() instead of logging, allowing the existing except OSError handler in files.py to catch it and log with the mkdocs.structure.files logger as expected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A dangling symlink in the docs directory would crash
mkdocs buildwith an unhandled FileNotFoundError during file copy.os.walklists the symlink as a regular file, but whenshutil.copyfile(oros.path.getmtime) follows the symlink to a missing target, the uncaught OSError aborts the entire build.Fix:
is_modified(): catch OSError when calling getmtime on the source — return False so the file is skipped silently.copy_file(): catch OSError around copy and log a warning, then skip the file gracefully instead of crashing.Closes: mkdocs/mkdocs#4048
Type of Change
Checklist
docs/about/release-notes.mdupdated (if applicable)