Skip to content

fix(env): trim trailing empty elements and fix unbound printf array e…#2513

Merged
CLHatch merged 1 commit into
mainfrom
Updates
Jun 1, 2026
Merged

fix(env): trim trailing empty elements and fix unbound printf array e…#2513
CLHatch merged 1 commit into
mainfrom
Updates

Conversation

@CLHatch

@CLHatch CLHatch commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

…xpansion

Pull request

Purpose
Describe the problem or feature in addition to a link to the issues.

Approach
How does this change address the problem?

Open Questions and Pre-Merge TODOs
Check all boxes as they are completed

  • Use github checklists. When solved, check the box and explain the answer.

Learning
Describe the research stage
Links to blog posts, patterns, libraries or addons used to solve this problem

Requirements
Check all boxes as they are completed

Summary by Sourcery

Adjust environment line formatting to avoid extraneous newlines and handle empty arrays safely.

Bug Fixes:

  • Trim all trailing empty environment lines before printing to prevent extra newlines in output.
  • Guard printf array expansion to avoid issues when the formatted environment line array is empty.

@CLHatch CLHatch requested a review from a team as a code owner June 1, 2026 05:41
@sourcery-ai

sourcery-ai Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts env_format_lines to strip all trailing empty entries before printing and changes printf array expansion to avoid unbound parameter issues when the array is empty.

File-Level Changes

Change Details Files
Strip all trailing empty formatted env lines rather than only the last one.
  • Replace single check/unset of the last array element with a while loop that repeatedly removes trailing empty entries.
  • Use array slicing to rebuild FormattedEnvLines without its last element while the last element is empty.
  • Retain existing behavior of matching Go strings.Join semantics regarding trailing delimiters.
scripts/env_format_lines.sh
Make printf robust when FormattedEnvLines is unset or empty.
  • Change printf expansion from a defaulted array expansion to a form that only expands the array when it is set, preventing unbound parameter errors.
  • Ensure printf still prints each remaining formatted env line on its own line using %s\n.
scripts/env_format_lines.sh

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions Bot added the core Automatic label label Jun 1, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new trailing-empty-element removal loop is more complex than necessary and uses ${FormattedEnvLines[-1]}, which may be less portable and harder to read; consider a clearer pattern like while ((${#FormattedEnvLines[@]} && ! ${FormattedEnvLines[-1]})); do ... or iterating from the end with an index variable.
  • The condition [[ ${#FormattedEnvLines[@]} -gt 0 && -z ${FormattedEnvLines[-1]-} ]] should quote the array element ("${FormattedEnvLines[-1]-}") to avoid unwanted word splitting and glob expansion when values contain spaces or special characters.
  • The printf call printf "%s\n" "${FormattedEnvLines[@]+"${FormattedEnvLines[@]}"}" is unusual and likely collapses the array into a single argument; if the goal is to avoid unbound expansion, a clearer pattern is to guard the call with if ((${#FormattedEnvLines[@]})); then printf '%s\n' "${FormattedEnvLines[@]}"; fi.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new trailing-empty-element removal loop is more complex than necessary and uses `${FormattedEnvLines[-1]}`, which may be less portable and harder to read; consider a clearer pattern like `while ((${#FormattedEnvLines[@]} && ! ${FormattedEnvLines[-1]})); do ...` or iterating from the end with an index variable.
- The condition `[[ ${#FormattedEnvLines[@]} -gt 0 && -z ${FormattedEnvLines[-1]-} ]]` should quote the array element (`"${FormattedEnvLines[-1]-}"`) to avoid unwanted word splitting and glob expansion when values contain spaces or special characters.
- The `printf` call `printf "%s\n" "${FormattedEnvLines[@]+"${FormattedEnvLines[@]}"}"` is unusual and likely collapses the array into a single argument; if the goal is to avoid unbound expansion, a clearer pattern is to guard the call with `if ((${#FormattedEnvLines[@]})); then printf '%s\n' "${FormattedEnvLines[@]}"; fi`.

## Individual Comments

### Comment 1
<location path="scripts/env_format_lines.sh" line_range="123" />
<code_context>
+			break
+		fi
+	done
+	printf "%s\n" "${FormattedEnvLines[@]+"${FormattedEnvLines[@]}"}"
 }

</code_context>
<issue_to_address>
**issue (bug_risk):** The current parameter expansion likely collapses all array elements into a single printf argument.

`${FormattedEnvLines[@]+
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

break
fi
done
printf "%s\n" "${FormattedEnvLines[@]+"${FormattedEnvLines[@]}"}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The current parameter expansion likely collapses all array elements into a single printf argument.

`${FormattedEnvLines[@]+

@CLHatch CLHatch merged commit 00d9179 into main Jun 1, 2026
16 checks passed
@CLHatch CLHatch deleted the Updates branch June 1, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Automatic label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant