-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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 vendor scripts shellcheck failures #79316
Conversation
hack/update-vendor.sh
Outdated
@@ -47,6 +47,15 @@ if [ -z "${BASH_XTRACEFD:-}" ]; then | |||
set -x | |||
fi | |||
|
|||
# list_staging_repos outputs a sorted list of repos in staging/src/k8s.io | |||
# each entry will just be the $repo portion of staging/src/k8s.io/$repo/... | |||
function list_staging_repos() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correctly listing these portably and sorted in one place had a nice side effect of simplifying the script a bit.
I implemented a couple variants of this, but this is probably the most robust version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to kube::util as I found the other vendor scripts need to do the same thing a lot..
@@ -67,17 +76,17 @@ function ensure_require_replace_directives_for_all_dependencies() { | |||
go mod edit -json | jq -r ".Replace // [] | sort | .[] | select(${replace_filter})" > "${replace_json}" | |||
|
|||
# 1a. Ensure replace directives have an explicit require directive | |||
cat "${replace_json}" | jq -r '"-require \(.Old.Path)@\(.New.Version)"' | xargs -L 100 go mod edit -fmt | |||
jq -r '"-require \(.Old.Path)@\(.New.Version)"' < "${replace_json}" | xargs -L 100 go mod edit -fmt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary cat usage. we can just pipe the file to input. all of the changes on .*cat.*
lines are this.
echo "replace ("; | ||
cat "${go_mod_replace}"; | ||
echo ")"; | ||
} > go.mod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we've used a command group to redirect once, which is simpler (and preferred by shellcheck).
It has a super minor performance benefit of only needing to open the file once since bash know understands the intent to redirect all of these to the same output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between { foo }
and ( foo )
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ foo }
is a command group but does not involve a subshell. ( foo )
is a command group in a subshell. using { foo; }
or:
{
foo
}
gets the benefits of grouping without involving a subprocess / shell.
technically there are also some slightly different rules for parsing to identify them that don't apply here.
@@ -201,9 +214,15 @@ done | |||
kube::log::status "go.mod: sorting staging modules" | |||
# tidy staging repos in reverse dependency order. | |||
# the content of dependencies' go.mod files affects what `go mod tidy` chooses to record in a go.mod file. | |||
ls staging/src/k8s.io | sort | xargs -I {} echo "k8s.io/{}" > "${TMP_DIR}/tidy_unordered.txt" | |||
tidy_unordered="${TMP_DIR}/tidy_unordered.txt" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not strictly necessary, but we were computing this in at least 3 places, so >WET ==> DRY
# We do read from ${tidy_unordered} in the pipeline and mention it within the | ||
# pipeline (but only ready it again) so we disable the lint to assure shellcheck | ||
# that :this-is-fine: | ||
# shellcheck disable=SC2094 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dislike needing these disable statements, but in this case it's a warning about potentially reading and writing to the same path in a loop (which wouldn't be safe). Since shellcheck has to just guess based on where the path is being referenced that it might be hitting this, we have to confirm that it is not.
/sig release |
@@ -531,6 +531,16 @@ EOF | |||
EOF | |||
} | |||
|
|||
# list_staging_repos outputs a sorted list of repos in staging/src/k8s.io | |||
# each entry will just be the $repo portion of staging/src/k8s.io/$repo/... | |||
function kube::util::list_staging_repos() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
originally I didn't add this to util, but all 3 fixed scripts have a need for this and doing it correctly / portably is tricky
6976ec1
to
95876cb
Compare
/retest |
( | ||
cd "${KUBE_ROOT}/staging/src/k8s.io" && \ | ||
find . -type d -mindepth 1 -maxdepth 1 | cut -c 3- | sort | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explanation:
- ls output is not portable
- find is more portable but gives relative paths
-> cd to the search directory in a subshell, then trim off the leading ./
from results.
search only for directories, set min/max depth so we only get results immediately below the search dir.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
/approve
echo "replace ("; | ||
cat "${go_mod_replace}"; | ||
echo ")"; | ||
} > go.mod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between { foo }
and ( foo )
?
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: BenTheElder, fejta The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/retest |
/retest Review the full test history for this PR. Silence the bot with an |
#78913 grrr |
What type of PR is this?
/kind cleanup
What this PR does / why we need it: fixes shellcheck failures in
hack/update-vendor.sh
,hack/pin-dependency.sh
, andhack/verify-no-vendor-cycles.sh
.Which issue(s) this PR fixes:
Part of #72956
Special notes for your reviewer:
Does this PR introduce a user-facing change?: