Repository: mkdocs/mkdocs

Issue:
Configuration strict option not checking broken links
I was testing mkdocs earlier this year and was using "strict: true" in my mkdocs.yml. If I recall correctly, it was working then and building would break if there was a broken link in the .md.

However I am now using v0.14.0 and this config option seems to have no effect on the build - I get no warnings or errors when building regardless of the value of strict.



Discussion:
Sorry, this one got lost somewhere along the way. This feature is working for me. We would need debug help - have you tried the most recent version? do you get any errors? what python version and OS are you running?

I'll close this now as it is so old and seems to be working, but happy for a new issue to be opened or I'll reopen this one.

I think I'm seeing a similar issue. I'm getting a warning, but the build is not halted as the doco says it should.

I'm using MkDocs 0.15.3, Python 3.5.2. My YML file is:

``` yaml
site_name: Documentation
theme_dir: 'fb_theme'
copyright: 'Copyright © 2016'
strict: true
extra:
# REDACTED
```

When I run `mkdocs serve`:

```
INFO    -  Building documentation... 
DEBUG   -  Loading configuration file: REDACTED/mkdocs.yml 
DEBUG   -  Config value: 'site_author' = None 
DEBUG   -  Config value: 'site_description' = None 
DEBUG   -  Config value: 'include_nav' = True 
DEBUG   -  Config value: 'site_url' = None 
DEBUG   -  Config value: 'google_analytics' = None 
DEBUG   -  Config value: 'include_next_prev' = True 
DEBUG   -  Config value: 'mdx_configs' = {} 
DEBUG   -  Config value: 'site_dir' = 'REDACTED/site' 
DEBUG   -  Config value: 'mkdocs_templates' = '/usr/lib/python3.5/site-packages/mkdocs/templates' 
DEBUG   -  Config value: 'theme_dir' = ['REDACTED/fb_theme', '/usr/lib/python3.5/site-packages/mkdocs/assets/search'] 
DEBUG   -  Config value: 'use_directory_urls' = True 
DEBUG   -  Config value: 'repo_url' = None 
DEBUG   -  Config value: 'strict' = False 
DEBUG   -  Config value: 'pages' = ['index.md', {

[...] (too many pages)

DEBUG   -  Config value: 'markdown_extensions' = ['meta', 'toc', 'tables', 'fenced_code'] 
DEBUG   -  Config value: 'extra_templates' = [] 
DEBUG   -  Config value: 'config_file_path' = 'REDACTED/mkdocs.yml' 
DEBUG   -  Config value: 'theme' = 'mkdocs' 
DEBUG   -  Config value: 'dev_addr' = '127.0.0.1:8000' 
DEBUG   -  Config value: 'repo_name' = None 
DEBUG   -  Config value: 'site_favicon' = None 
DEBUG   -  Config value: 'extra_css' = [] 
DEBUG   -  Config value: 'site_name' = 'Documentation' 
DEBUG   -  Config value: 'docs_dir' = 'REDACTED/docs' 
DEBUG   -  Config value: 'extra_javascript' = [] 
DEBUG   -  Config value: 'remote_branch' = 'gh-pages' 
DEBUG   -  Config value: 'copyright' = 'Copyright © 2016' 
DEBUG   -  Config value: 'remote_name' = 'origin' 
DEBUG   -  Config value: 'extra' = redacted
INFO    -  Cleaning site directory 
DEBUG   -  Copying static assets from theme: /usr/lib/python3.5/site-packages/mkdocs/assets/search 
DEBUG   -  Copying static assets from theme: REDACTED/fb_theme 
DEBUG   -  Copying static assets from the docs dir. 
DEBUG   -  Building markdown pages. 
DEBUG   -  Building template: 404.html 
DEBUG   -  Building template: search.html 
DEBUG   -  Search is enabled but the theme doesn't contain a search.html file. Assuming the theme implements search within a modal. 
DEBUG   -  Building template: sitemap.xml 
DEBUG   -  Building extra_templates page 
DEBUG   -  Building page index.md 
WARNING -  The page "index.md" contained a hyperlink to "Something.md" which is not listed in the "pages" configuration. 
DEBUG   -  Building page administer/administer-introduction.md 
DEBUG   -  Building page administer/diagnostics/download-support-package.md 
DEBUG   -  Building page administer/diagnostics/system-diagnostic.md 
...
```

I get the warning, but the build continues.

Hm, it works with `mkdocs build` though, so I guess this is expected for `serve` to not halt the build? If that's the case please disregard my comment.

Erm... ok so, actually, it doesn't work :wink: 
- Setting `strict: true` in `mkdocs.yml` does NOT halt the build on broken links
- Using `mkdocs build -s` does

> - Setting `strict: true` in `mkdocs.yml` does NOT halt the build on broken links
> - Using `mkdocs build -s` does

@nguillaumin thanks for the clear report. I can confirm that I get the same behavior and am reopening this issue. 

BTW, I also noticed that this only applies to `*.md` internal links. For example, when testing this, I noticed that the `index.md` file in MkDocs documentation contains links to both `user-guide/deploying-your-docs/` and `user-guide/deploying-your-docs.md` (which in itself, is a separate issue in need to fixing -- see mivok/markdownlint#137). The former does not raise a warning and the later does when the links are altered to be inaccurate (when testing I removed the final dash: `user-guide/deploying-yourdocs/` and `user-guide/deploying-yourdocs.md`).

Ahh, when the `--strict` flag is not included, then is evaluates to `False` and is passed in by the CLI overriding the value in the config file. Of course, when if the flag is included, it evaluated to `True` and no problem occurs. So we need to not pass the `script` value to the config from the CLI if the flag is not `True`. Not sure if Click has a way to do that or if we need to special case it in the code


Resolving patch:
diff --git a/mkdocs/__main__.py b/mkdocs/__main__.py
--- a/mkdocs/__main__.py
+++ b/mkdocs/__main__.py
@@ -9,7 +9,7 @@
 from mkdocs import __version__
 from mkdocs import utils
 from mkdocs import exceptions
-from mkdocs.config import load_config
+from mkdocs import config
 from mkdocs.commands import build, gh_deploy, new, serve
 
 log = logging.getLogger(__name__)
@@ -112,6 +112,10 @@ def serve_command(dev_addr, config_file, strict, theme, theme_dir, livereload):
 
     logging.getLogger('tornado').setLevel(logging.WARNING)
 
+    # Don't override config value if user did not specify --strict flag
+    # Conveniently, load_config drops None values
+    strict = strict or None
+
     try:
         serve.serve(
             config_file=config_file,
@@ -136,8 +140,13 @@ def serve_command(dev_addr, config_file, strict, theme, theme_dir, livereload):
 @common_options
 def build_command(clean, config_file, strict, theme, theme_dir, site_dir):
     """Build the MkDocs documentation"""
+
+    # Don't override config value if user did not specify --strict flag
+    # Conveniently, load_config drops None values
+    strict = strict or None
+
     try:
-        build.build(load_config(
+        build.build(config.load_config(
             config_file=config_file,
             strict=strict,
             theme=theme,
@@ -168,8 +177,12 @@ def json_command(clean, config_file, strict, site_dir):
                 "future MkDocs release. For details on updating: "
                 "http://www.mkdocs.org/about/release-notes/")
 
+    # Don't override config value if user did not specify --strict flag
+    # Conveniently, load_config drops None values
+    strict = strict or None
+
     try:
-        build.build(load_config(
+        build.build(config.load_config(
             config_file=config_file,
             strict=strict,
             site_dir=site_dir
@@ -189,13 +202,13 @@ def json_command(clean, config_file, strict, site_dir):
 def gh_deploy_command(config_file, clean, message, remote_branch, remote_name):
     """Deploy your documentation to GitHub Pages"""
     try:
-        config = load_config(
+        cfg = config.load_config(
             config_file=config_file,
             remote_branch=remote_branch,
             remote_name=remote_name
         )
-        build.build(config, dirty=not clean)
-        gh_deploy.gh_deploy(config, message=message)
+        build.build(cfg, dirty=not clean)
+        gh_deploy.gh_deploy(cfg, message=message)
     except exceptions.ConfigurationError as e:
         # Avoid ugly, unhelpful traceback
         raise SystemExit('\n' + str(e))


Test patch:
diff --git a/mkdocs/tests/cli_tests.py b/mkdocs/tests/cli_tests.py
--- a/mkdocs/tests/cli_tests.py
+++ b/mkdocs/tests/cli_tests.py
@@ -22,16 +22,31 @@ def test_serve(self, mock_serve):
             cli.cli, ["serve", ], catch_exceptions=False)
 
         self.assertEqual(result.exit_code, 0)
-        self.assertEqual(mock_serve.call_count, 1)
-
+        mock_serve.assert_called_once_with(
+            config_file=None,
+            dev_addr=None,
+            strict=None,
+            theme=None,
+            theme_dir=None,
+            livereload=None
+        )
+
+    @mock.patch('mkdocs.config.load_config', autospec=True)
     @mock.patch('mkdocs.commands.build.build', autospec=True)
-    def test_build(self, mock_build):
+    def test_build(self, mock_build, mock_load_config):
 
         result = self.runner.invoke(
             cli.cli, ["build", ], catch_exceptions=False)
 
         self.assertEqual(result.exit_code, 0)
         self.assertEqual(mock_build.call_count, 1)
+        mock_load_config.assert_called_once_with(
+            config_file=None,
+            strict=None,
+            theme=None,
+            theme_dir=None,
+            site_dir=None
+        )
 
     @mock.patch('mkdocs.commands.build.build', autospec=True)
     def test_build_verbose(self, mock_build):