Skip to content

Commit

Permalink
Isolate strict warning counter to just the ongoing build (#2607)
Browse files Browse the repository at this point in the history
Refactor to eliminate global state.
Fixes the issue with the webserver's warnings being able to abort the build.
  • Loading branch information
oprypin committed Oct 11, 2021
1 parent 5754091 commit 7a27572
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
5 changes: 0 additions & 5 deletions mkdocs/__main__.py
Expand Up @@ -72,11 +72,6 @@ def __init__(self, log_name='mkdocs', level=logging.INFO):
self.stream.name = 'MkDocsStreamHandler'
self.logger.addHandler(self.stream)

# Add CountHandler for strict mode
self.counter = utils.log_counter
self.counter.setLevel(logging.WARNING)
self.logger.addHandler(self.counter)


pass_state = click.make_pass_decorator(State, ensure=True)

Expand Down
17 changes: 14 additions & 3 deletions mkdocs/commands/build.py
Expand Up @@ -240,8 +240,16 @@ def _build_page(page, config, doc_files, nav, env, dirty=False):

def build(config, live_server=False, dirty=False):
""" Perform a full site build. """
try:

logger = logging.getLogger('mkdocs')

# Add CountHandler for strict mode
warning_counter = utils.CountHandler()
warning_counter.setLevel(logging.WARNING)
if config['strict']:
logging.getLogger('mkdocs').addHandler(warning_counter)

try:
from time import time
start = time()

Expand Down Expand Up @@ -308,8 +316,8 @@ def build(config, live_server=False, dirty=False):
# Run `post_build` plugin events.
config['plugins'].run_event('post_build', config=config)

counts = utils.log_counter.get_counts()
if config['strict'] and len(counts):
counts = warning_counter.get_counts()
if counts:
msg = ', '.join([f'{v} {k.lower()}s' for k, v in counts])
raise Abort(f'\nAborted with {msg} in strict mode!')

Expand All @@ -323,6 +331,9 @@ def build(config, live_server=False, dirty=False):
raise Abort('\nAborted with a BuildError!')
raise

finally:
logger.removeHandler(warning_counter)


def site_directory_contains_stale_files(site_directory):
""" Check if the site directory contains stale files from a previous build. """
Expand Down
3 changes: 0 additions & 3 deletions mkdocs/utils/__init__.py
Expand Up @@ -442,9 +442,6 @@ def get_counts(self):
return [(logging.getLevelName(k), v) for k, v in sorted(self.counts.items(), reverse=True)]


# A global instance to use throughout package
log_counter = CountHandler()

# For backward compatibility as some plugins import it.
# It is no longer necessary as all messages on the
# `mkdocs` logger get counted automatically.
Expand Down

0 comments on commit 7a27572

Please sign in to comment.