Skip to content
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

feat: dynamically determine protobuf version in build.gradle #1753

Merged
merged 15 commits into from
Jun 8, 2023

Conversation

JoeWang1127
Copy link
Collaborator

@JoeWang1127 JoeWang1127 commented Jun 6, 2023

Fixes #1757.

Steps to verify this PR:

  1. find a self-service client, e.g., google-maps-fleetengine.
  2. checkout this branch, assume the path of repo root is /path/sdkplatform-java.
  3. in googleapis repository, run bazelisk build to build the client library with gapic-generator-java and gax-java replaced with local repository.
    For example, bazelisk build //google/maps/fleetengine/v1:google-maps-fleetengine-v1-java --override_repository=gapic_generator_java=/path/sdk-platform-java/ --override_repository=com_google_api_gax_java=/path/sdk-platform-java/gax-java/
  4. in googleapis repository, unzip the generated tar file to googleapis root directory.
    For example, tar -xf bazel-bin/google/maps/fleetengine/v1/google-maps-fleetengine-v1-java.tar.gz
  5. find protobuf dependency, com.google.protobuf:protobuf-java in proto-*/build.gradle and make sure the version is the same as protobuf version defined in WORKSPACE in gooleapis.
  6. as an alternative, go to the client library directory (e.g., google-maps-fleetengine-v1-java/), and run ./gradlew build.

@product-auto-label product-auto-label bot added the size: xs Pull request size is extra small. label Jun 6, 2023
@suztomo
Copy link
Member

suztomo commented Jun 6, 2023

Can you add how you tested this in the pull request description?

@JoeWang1127 JoeWang1127 linked an issue Jun 6, 2023 that may be closed by this pull request
@JoeWang1127 JoeWang1127 marked this pull request as ready for review June 6, 2023 21:30
@JoeWang1127 JoeWang1127 requested a review from a team as a code owner June 6, 2023 21:30
@JoeWang1127 JoeWang1127 requested review from suztomo and blakeli0 June 6, 2023 21:30
@JoeWang1127 JoeWang1127 added the owlbot:run Add this label to trigger the Owlbot post processor. label Jun 6, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jun 6, 2023
@product-auto-label product-auto-label bot added size: s Pull request size is small. and removed size: xs Pull request size is extra small. labels Jun 7, 2023
gax-java/dependencies.properties Outdated Show resolved Hide resolved
rules_java_gapic/java_gapic_pkg.bzl Show resolved Hide resolved
Copy link
Member

@suztomo suztomo left a comment

Choose a reason for hiding this comment

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

Would you check whether the protobuf version line canbe removed?

rules_java_gapic/java_gapic_pkg.bzl Outdated Show resolved Hide resolved
@@ -23,7 +23,7 @@ version.gax_httpjson=0.114.1-SNAPSHOT
# Versions for dependencies which actual artifacts differ between Bazel and Gradle.
# Gradle build depends on prebuilt maven artifacts, while Bazel build depends on Bazel workspaces
# with the sources.
version.com_google_protobuf=3.23.2
version.com_google_protobuf=3.21.12
Copy link
Member

Choose a reason for hiding this comment

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

Where is this used now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This line can't be removed because

_protobuf_version = PROPERTIES["version.com_google_protobuf"]
depends on the key to be existed.

Copy link
Member

Choose a reason for hiding this comment

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

Of course the next question is the purpose of that line. Who calls com_google_api_gax_java_repositories()?

Copy link
Collaborator Author

@JoeWang1127 JoeWang1127 Jun 7, 2023

Choose a reason for hiding this comment

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

com_google_api_gax_java_repositories() is called from googleapis/WORKSPACE and gax-java/WORKSPACE.

The protobuf version can be passed from googleapis when building client libraries. However, when running showcase tests, it will create a cycle in WORKSPACE if this line (version.com_google_protobuf=3.21.12) is removed.

Please see #1753 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Add source code comment. It's used for showcase testing. Not for the generated Gradle file in googleapis project any more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

# the version of protobuf defined in googleapis is higher than protobuf
# defined in gax-java/dependencies.properties, use this replacement to
# sync the two versions.
props_as_map["version.com_google_protobuf"] = PROTOBUF_JAVA_VERSION
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Inject a key-value pair here, rather than in rules_java_gapic/java_gapic_pkg.bzl.

By doing this, we can remove protobuf version in gax-java/dependencies.properties

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This creates a cycle in gax-java/WORKSPACE:

(18:15:51) ERROR: Failed to load Starlark extension '@com_google_protobuf//:protobuf_version.bzl'.
Cycle in the workspace file detected. This indicates that a repository is used prior to being defined.
The following chain of repository dependencies lead to the missing definition.
 - @com_google_protobuf
This could either mean you have to add the '@com_google_protobuf' repository with a statement like `http_archive` in your WORKSPACE file (note that transitive dependencies are not added automatically), or move an existing definition earlier in your WORKSPACE file.

Full log: https://github.com/googleapis/sdk-platform-java/actions/runs/5203236910/jobs/9385816289?pr=1753

Copy link
Member

Choose a reason for hiding this comment

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

Interesting.


def _wrapPropertyNamesInBraces(properties):
wrappedProperties = {}
for k, v in properties.items():
wrappedProperties["{{%s}}" % k] = v
return wrappedProperties

_PROPERTIES = _wrapPropertyNamesInBraces(PROPERTIES)
_PROPERTIES = _wrapPropertyNamesInBraces(PROPERTIES | _syncVersionWithGoogleapis())
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_PROPERTIES = _wrapPropertyNamesInBraces(PROPERTIES | _syncVersionWithGoogleapis())
# Before this replacement, there is a problem (e.g., b/284292352) when ...
PROPERTIES["version.com_google_protobuf"] = PROTOBUF_JAVA_VERSION
_PROPERTIES = _wrapPropertyNamesInBraces(PROPERTIES)

Copy link
Collaborator Author

@JoeWang1127 JoeWang1127 Jun 7, 2023

Choose a reason for hiding this comment

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

This actually won't work as it will violate mutability rule.

ERROR: Traceback (most recent call last):
        File "/private/var/tmp/_bazel_joewa/b6b64da05090260654456c80e641ce7c/external/gapic_generator_java/rules_java_gapic/java_gapic_pkg.bzl", line 28, column 43, in <toplevel>
                PROPERTIES["version.com_google_protobuf"] = PROTOBUF_JAVA_VERSION
Error: trying to mutate a frozen dict value

Copy link
Member

@suztomo suztomo Jun 7, 2023

Choose a reason for hiding this comment

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

Nice learning. Then create a copy or operate on _PROPERTIES. The latter needs to tweak the key with braces.

# the version of protobuf defined in googleapis is higher than protobuf
# defined in gax-java/dependencies.properties, use this replacement to
# sync the two versions.
SYNCED_PROPERTIES = PROPERTIES | {"version.com_google_protobuf": PROTOBUF_JAVA_VERSION}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@suztomo is this the idea of "making a copy"?

Copy link
Member

@suztomo suztomo Jun 8, 2023

Choose a reason for hiding this comment

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

This is better than what I was thinking with "dict.copy()".


def _wrapPropertyNamesInBraces(properties):
wrappedProperties = {}
for k, v in properties.items():
wrappedProperties["{{%s}}" % k] = v
return wrappedProperties

_PROPERTIES = _wrapPropertyNamesInBraces(PROPERTIES)
# Before this replacement, there is a problem (e.g., b/284292352) when
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Before this replacement, there is a problem (e.g., b/284292352) when
# Before this replacement, there was a problem (e.g., b/284292352) when

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@@ -23,6 +23,8 @@ version.gax_httpjson=0.114.1-SNAPSHOT
# Versions for dependencies which actual artifacts differ between Bazel and Gradle.
# Gradle build depends on prebuilt maven artifacts, while Bazel build depends on Bazel workspaces
# with the sources.
# The protobuf version is only used by showcase tests, not used by gradle files
# in generated, self-service clients (from googleapis project).
version.com_google_protobuf=3.23.2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this used by showcase? I think the protobuf runtime version in Showcase module extends from gapic-generator-java-parent-pom

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct me if I'm wrong.

Looking at the ci error log, showcase golden tests eventually invoke bazelisk run //showcase:verify_gapic.

bazelisk run //showcase:verify_gapic will fail if version.com_google_protobuf=3.23.2 is removed from dependencies.properties.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, I didn't notice that we are making sure showcase modules are compilable right after generation, they are still verified with the generated gradle files though.

Comment on lines 26 to 27
# The protobuf version is only used by showcase tests, not used by gradle files
# in generated, self-service clients (from googleapis project).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# The protobuf version is only used by showcase tests, not used by gradle files
# in generated, self-service clients (from googleapis project).
# The protobuf version is only used for generating gradle files for showcase module,
# not for self-service clients (from googleapis project).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

@JoeWang1127 JoeWang1127 requested a review from blakeli0 June 8, 2023 17:11
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jun 8, 2023

[gapic-generator-java-root] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jun 8, 2023

[java_showcase_integration_tests] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jun 8, 2023

[java_showcase_unit_tests] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@JoeWang1127 JoeWang1127 merged commit 6de5110 into main Jun 8, 2023
@JoeWang1127 JoeWang1127 deleted the feat/dynamic-version branch June 8, 2023 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: s Pull request size is small.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamically determine protobuf version in build.gradle.
3 participants