Skip to content

Commit

Permalink
Fix validate goal crashing if not configured (#14104)
Browse files Browse the repository at this point in the history
Closes #12189.

This also will allow us in #14102 to know whether to run `validate` as part of the `lint` goal or not. If it isn't configured, we'll skip it.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano authored Jan 7, 2022
1 parent b2d3606 commit 57a4f69
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/python/pants/backend/project_info/source_file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import annotations

import logging
import re
import textwrap
from dataclasses import dataclass
Expand All @@ -19,6 +20,8 @@
from pants.util.frozendict import FrozenDict
from pants.util.memo import memoized_method

logger = logging.getLogger(__name__)


class DetailLevel(Enum):
"""How much detail about validation to emit to the console.
Expand Down Expand Up @@ -133,8 +136,12 @@ def register_options(cls, register):
register("--config", type=dict, fromfile=True, help=schema_help)

@memoized_method
def get_multi_matcher(self):
return MultiMatcher(ValidationConfig.from_dict(self.options.config))
def get_multi_matcher(self) -> MultiMatcher | None:
return (
MultiMatcher(ValidationConfig.from_dict(self.options.config))
if self.options.config
else None
)


@dataclass(frozen=True)
Expand Down Expand Up @@ -285,6 +292,13 @@ async def validate(
source_file_validation: SourceFileValidation,
) -> Validate:
multi_matcher = source_file_validation.get_multi_matcher()
if multi_matcher is None:
logger.error(
"You must set the option `[sourcefile-validation].config` for the "
"`validate` goal to work. Run `./pants help sourcefile-validation`."
)
return Validate(PANTS_FAILED_EXIT_CODE)

digest_contents = await Get(DigestContents, Digest, specs_snapshot.snapshot.digest)
regex_match_results = RegexMatchResults(
multi_matcher.check_source_file(file_content.path, file_content.content)
Expand Down

0 comments on commit 57a4f69

Please sign in to comment.