Skip to content

fix(generator): align proto_library_with_info deps with librarian.yaml additional_protos - #4691

Draft
jinseopkim0 wants to merge 1 commit into
mainfrom
fix/generator-respect-additional-protos
Draft

jinseopkim0 wants to merge 1 commit into
mainfrom
fix/generator-respect-additional-protos

Conversation

@jinseopkim0

Copy link
Copy Markdown
Contributor

Ensure spring-cloud-generator only includes iam_policy.proto and locations.proto in proto_library_with_info when they are configured under additional_protos in google-cloud-java/librarian.yaml, preventing compilation failures in generated starters such as google-cloud-binary-authorization-spring-starter.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Spring Cloud generator scripts to extract additional_protos from librarian.yaml and use them to align the dependencies of proto_library_with_info in BUILD.bazel files. Specifically, it updates generate-library-list.sh to parse and append additional_protos to the library list, and modifies generate-steps.sh and generate.sh to pass and process these protos, conditionally removing unneeded dependencies like IAM policy and location protos. The review feedback suggests two key improvements: first, adding a fallback in the yq evaluation to safely handle cases where additional_protos is null or missing; second, simplifying the buildozer commands in generate-steps.sh by targeting %proto_library_with_info directly to avoid unnecessary subshells and handle multiple rules robustly.

fi

echo "$unique_module_name, $proto_paths_latest, $distribution_name, $monorepo_folder" >> $filename
additional_protos=$(yq eval '.libraries[] | select(.name == "'$name'") | .apis[] | select(.path == "'$proto_paths_latest'") | .java.additional_protos[].path' ./google-cloud-java/librarian.yaml | paste -sd ";" -)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If a library does not define additional_protos in librarian.yaml, yq will attempt to iterate over a null value (via .java.additional_protos[]), which prints Error: Cannot iterate over null to stderr. Since generate-library-list.sh runs with set -ex, this clutters the error logs with noisy error messages for almost every library.

Using the alternative operator // [] ensures that yq safely defaults to an empty array when additional_protos is missing or null.

Suggested change
additional_protos=$(yq eval '.libraries[] | select(.name == "'$name'") | .apis[] | select(.path == "'$proto_paths_latest'") | .java.additional_protos[].path' ./google-cloud-java/librarian.yaml | paste -sd ";" -)
additional_protos=$(yq eval '.libraries[] | select(.name == "'$name'") | .apis[] | select(.path == "'$proto_paths_latest'") | .java.additional_protos // [] | .[].path' ./google-cloud-java/librarian.yaml | paste -sd ";" -)

Comment on lines +149 to +157
PROTO_WITH_INFO_NAME="$(buildozer 'print name' ${path_to_build_file}:%proto_library_with_info 2>/dev/null || true)"
if [[ -n "$PROTO_WITH_INFO_NAME" ]]; then
if [[ "$additional_protos" != *"google/iam/v1/iam_policy.proto"* ]]; then
buildozer 'remove deps //google/iam/v1:iam_policy_proto' "${path_to_build_file}:${PROTO_WITH_INFO_NAME}" 2>/dev/null || true
fi
if [[ "$additional_protos" != *"google/cloud/location/locations.proto"* ]]; then
buildozer 'remove deps //google/cloud/location:location_proto' "${path_to_build_file}:${PROTO_WITH_INFO_NAME}" 2>/dev/null || true
fi
fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Instead of querying the name of the proto_library_with_info rule first and checking if it exists, you can target %proto_library_with_info directly in the buildozer command.

This is more concise, avoids an extra subshell invocation of buildozer, and is more robust if a BUILD.bazel file contains multiple proto_library_with_info rules (where PROTO_WITH_INFO_NAME would contain newlines and cause the subsequent buildozer commands to fail).

Suggested change
PROTO_WITH_INFO_NAME="$(buildozer 'print name' ${path_to_build_file}:%proto_library_with_info 2>/dev/null || true)"
if [[ -n "$PROTO_WITH_INFO_NAME" ]]; then
if [[ "$additional_protos" != *"google/iam/v1/iam_policy.proto"* ]]; then
buildozer 'remove deps //google/iam/v1:iam_policy_proto' "${path_to_build_file}:${PROTO_WITH_INFO_NAME}" 2>/dev/null || true
fi
if [[ "$additional_protos" != *"google/cloud/location/locations.proto"* ]]; then
buildozer 'remove deps //google/cloud/location:location_proto' "${path_to_build_file}:${PROTO_WITH_INFO_NAME}" 2>/dev/null || true
fi
fi
if [[ "$additional_protos" != *"google/iam/v1/iam_policy.proto"* ]]; then
buildozer 'remove deps //google/iam/v1:iam_policy_proto' "${path_to_build_file}:%proto_library_with_info" 2>/dev/null || true
fi
if [[ "$additional_protos" != *"google/cloud/location/locations.proto"* ]]; then
buildozer 'remove deps //google/cloud/location:location_proto' "${path_to_build_file}:%proto_library_with_info" 2>/dev/null || true
fi

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant