Fix unreachable JSON validation code in validate_input_path#4782
Open
codewithfourtix wants to merge 3 commits intoaboutcode-org:developfrom
Open
Fix unreachable JSON validation code in validate_input_path#4782codewithfourtix wants to merge 3 commits intoaboutcode-org:developfrom
codewithfourtix wants to merge 3 commits intoaboutcode-org:developfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes the JSON-specific validation path in validate_input_path() so that JSON extension/content checks can run (instead of being placed after an unconditional raise).
Changes:
- Splits the
--from-jsonvalidation into “not a file” vs “is a file” branches. - Restores execution of
.jsonextension and “looks like JSON” ({prefix) checks for file inputs.
Comments suppressed due to low confidence (1)
src/scancode/cli.py:202
open(inp)uses the platform default text encoding and theread()can raiseUnicodeDecodeErrorfor binary/non-UTF-8 inputs, which would surface as an unhandled exception during CLI argument validation. Handle decode errors explicitly (e.g., specify an encoding and catchUnicodeDecodeError) and convert them to aclick.BadParameterso users get a consistent, friendly error.
with open(inp) as js:
start = js.read(100).strip()
if not start.startswith("{"):
raise click.BadParameter(f"JSON input: {inp!r} is not a well formed JSON file")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The .json extension check and JSON well-formedness check after the 'raise click.BadParameter' were unreachable dead code. Move these validations into a separate conditional block that runs when from_json is True and the input is a valid file. Signed-off-by: codewithfourtix <codewithfourtix@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: codewithfourtix <codewithfourtix@gmail.com>
79803f6 to
ba29630
Compare
The JSON extension and well-formedness checks were unreachable dead code because they appeared after an unconditional raise statement. Restructure into if/elif blocks so both checks are actually executed. Also add explicit UTF-8 encoding and handle UnicodeDecodeError when reading the JSON file header to give users a clear error message. Signed-off-by: codewithfourtix codewithfourtix@gmail.com Signed-off-by: codewithfourtix <codewithfourtix@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In
src/scancode/cli.py, thevalidate_input_pathfunction contained a prematureraisestatement that made the JSON validation checks below it dead/unreachable code. As a result, two important validations were silently never executed:.jsonextension{)Root Cause
The
raise click.BadParameter(...)for "not a file" was placed before the extension and content checks, causing Python to exit the block immediately and skip the remaining validations entirely.Before (Broken)
After (Fixed)
Impact
Without this fix, users could pass a non-
.jsonfile or a malformed JSON file as--from-jsoninput and ScanCode would proceed without raising an appropriate error, likely causing a confusing failure downstream.Tasks