Skip to content

utils: fix broken symlink copy exception #3785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Karneades
Copy link

@Karneades Karneades commented Jul 29, 2024

First of all: Thanks for your project and awesome work behind MkDocs!

Issue

The pull request fixes an exception with broken symlinks. Broken symlinks were mentioned multiple times in the past in various places. I run into the same with a broken symlink in a recent project with the most recent release.

Expected behavior

Site is built/served without the broken symlink but a warning is provided to the user (also useful for CI/CD checks etc.).

Solution

Based on #668 I added the required check in the utils -> copy_file function to report an warning if a symlink is broken and its file was not found, but continue with the copy and serving/building of the site instead of an exception.

After the fix, it looks like this

% mkdocs serve                                                             
INFO    -  Building documentation...
INFO    -  Cleaning site directory
WARNING -  Symlink broken, not copy file: /awesome-mkdocs/docs/mylink
INFO    -  Documentation built in 0.05 seconds
INFO    -  [08:44:05] Watching paths for changes: 'docs', 'mkdocs.yml'
INFO    -  [08:44:05] Serving on http://127.0.0.1:8000

How to reproduce

  • simple mkdocs config with just site_name in the config.
  • Add symlink to non-existing file into docs dir.
    ln -s ../not-existing mylink

Exception

INFO    -  Cleaning site directory
INFO    -  Building documentation to directory: ...
[...]
Traceback (most recent call last):
  File "/usr/local/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/usr/lib/python3/dist-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/mkdocs/__main__.py", line 284, in build_command
    build.build(cfg, dirty=not clean)
  File "/usr/local/lib/python3.10/dist-packages/mkdocs/commands/build.py", line 325, in build
    files.copy_static_files(dirty=dirty, inclusion=inclusion)
  File "/usr/local/lib/python3.10/dist-packages/mkdocs/structure/files.py", line 122, in copy_static_files
    file.copy_file(dirty)
  File "/usr/local/lib/python3.10/dist-packages/mkdocs/structure/files.py", line 485, in copy_file
    utils.copy_file(self.abs_src_path, output_path)
  File "/usr/local/lib/python3.10/dist-packages/mkdocs/utils/__init__.py", line 123, in copy_file
    shutil.copyfile(source_path, output_path)
  File "/usr/lib/python3.10/shutil.py", line 254, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '...'

Refs: #1753 #639


I read https://www.mkdocs.org/about/contributing/ but was unsure if I should open the PR against master or another branch. We could make that clear in the contributing guidelines.

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