Skip to content

Commit 0b87262

Browse files
committed
fix: improve Vale style checking robustness
- Use xargs to properly handle filenames with spaces - Add more robust error handling for JSON parsing - Handle empty output files explicitly - Add truncation for large issue lists - Improve jq handling with try/catch pattern
1 parent 0401aa6 commit 0b87262

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

.github/docs/actions/docs-core/action.yaml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -779,25 +779,32 @@ runs:
779779
echo "Running Vale with config .github/docs/vale/.vale.ini"
780780
if [ -n "$FILES_TO_CHECK" ]; then
781781
echo "Using space-separated file list for Vale"
782-
vale --no-exit --output=JSON --config=.github/docs/vale/.vale.ini $FILES_TO_CHECK > "$TEMP_DIR/vale_results.json" 2>/dev/null || true
782+
# Use xargs to properly handle each file as a separate argument
783+
echo "$FILES_TO_CHECK" | xargs vale --no-exit --output=JSON --config=.github/docs/vale/.vale.ini > "$TEMP_DIR/vale_results.json" 2>/dev/null || true
783784
elif [ -n "$MD_FILES_CSV" ]; then
784785
# Convert CSV to space-separated for command line
785786
echo "Using converted CSV file list for Vale"
786-
FILE_LIST=$(echo "$MD_FILES_CSV" | tr ',' ' ')
787-
vale --no-exit --output=JSON --config=.github/docs/vale/.vale.ini $FILE_LIST > "$TEMP_DIR/vale_results.json" 2>/dev/null || true
787+
# Use xargs to properly handle each file as a separate argument
788+
echo "$MD_FILES_CSV" | tr ',' '\n' | xargs vale --no-exit --output=JSON --config=.github/docs/vale/.vale.ini > "$TEMP_DIR/vale_results.json" 2>/dev/null || true
788789
else
789790
echo "No files to check with Vale"
790791
echo "[]" > "$TEMP_DIR/vale_results.json"
791792
fi
792793
793794
# Process results from JSON output
794795
if [ -f "$TEMP_DIR/vale_results.json" ]; then
796+
# Check if the file is empty (which is valid but not valid JSON)
797+
if [ ! -s "$TEMP_DIR/vale_results.json" ]; then
798+
echo "Empty results file, treating as success"
799+
echo "[]" > "$TEMP_DIR/vale_results.json"
800+
fi
801+
795802
# Make sure the JSON output is valid
796803
if jq empty "$TEMP_DIR/vale_results.json" 2>/dev/null; then
797804
VALE_OUTPUT=$(cat "$TEMP_DIR/vale_results.json")
798805

799-
# Check if there are any issues
800-
if [ "$VALE_OUTPUT" = "[]" ] || [ "$(echo "$VALE_OUTPUT" | jq 'length')" -eq 0 ]; then
806+
# Handle empty array case explicitly
807+
if [ "$VALE_OUTPUT" = "" ] || [ "$VALE_OUTPUT" = "[]" ] || [ "$(echo "$VALE_OUTPUT" | jq 'length')" -eq 0 ]; then
801808
echo "Vale check passed: No style issues found"
802809
echo "status=success" >> $GITHUB_OUTPUT
803810
echo "message=No style issues found in $FILE_COUNT files" >> $GITHUB_OUTPUT
@@ -806,16 +813,18 @@ runs:
806813
ISSUE_COUNT=$(echo "$VALE_OUTPUT" | jq 'length')
807814
echo "Vale found $ISSUE_COUNT style issues in $FILE_COUNT files"
808815

809-
# Group issues by file for better readability
810-
echo "$VALE_OUTPUT" | jq -r 'group_by(.Path) | .[] | .[0].Path + ":" + (. | length | tostring) + " issues"'
816+
# Group issues by file for better readability (with safety check)
817+
echo "$VALE_OUTPUT" | jq -r 'try (group_by(.Path) | .[] | .[0].Path + ":" + (. | length | tostring) + " issues") // "Could not group issues"'
811818

812819
# Show details of first 10 issues
813820
echo "First 10 issues (detail):"
814-
echo "$VALE_OUTPUT" | jq -r '.[] | "\(.Path):\(.Line):\(.Column) - \(.Message) (\(.Check))"' | head -10
821+
echo "$VALE_OUTPUT" | jq -r 'try (.[] | "\(.Path):\(.Line):\(.Column) - \(.Message) (\(.Check))") // "Error displaying issues"' | head -10
815822

816823
echo "status=warning" >> $GITHUB_OUTPUT
817824
echo "message=Found $ISSUE_COUNT style issues in $FILE_COUNT files" >> $GITHUB_OUTPUT
818-
echo "issues=$VALE_OUTPUT" >> $GITHUB_OUTPUT
825+
# Limit the size of the issues output to avoid any GitHub Actions limits
826+
ISSUES_TRUNCATED=$(echo "$VALE_OUTPUT" | jq 'if length > 50 then .[0:50] else . end')
827+
echo "issues=$ISSUES_TRUNCATED" >> $GITHUB_OUTPUT
819828
fi
820829
else
821830
echo "Warning: Vale produced invalid JSON output, treating as success"

0 commit comments

Comments
 (0)