Skip to content
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

Fix validate goal crashing if not configured #14104

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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