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

fix(build_gen): support proto java_package, make gapic.yaml dep optional, add tests [rules_gapic] #26

Merged
merged 4 commits into from
Feb 19, 2021

Conversation

miraleung
Copy link
Contributor

build_gen previously relied on language_settings.java in gapic.yaml to find and generate test classes in BUILD.bazel. However, modern GAPIC clients shouldn't use gapic.yaml at all, so we should derive the Java package from the options specified in the protobuf files.

The existing regex to parse language package options was actually grabbing the first <common_prefix><more_text> proto option, so I fixed that as well.

Since Java needs to continue supporting the gapic.yaml language overrides for the foreseeable future (for popular legacy clients like PubSub), I added a legacy test as well.

@miraleung miraleung requested a review from a team as a code owner February 19, 2021 09:17
@product-auto-label product-auto-label bot added the api: cloudbuild Issues related to the Cloud Build API. label Feb 19, 2021
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Feb 19, 2021
Copy link
Contributor

@alexander-fenster alexander-fenster left a comment

Choose a reason for hiding this comment

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

LGTM but please wait for @vam-google's review since he knows this code better than I do.

Copy link
Contributor

@vam-google vam-google left a comment

Choose a reason for hiding this comment

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

Just a few minor concerns

Pattern.compile(
"(?m)^option\\s+(?<optName>\\w+)\\s+=\\s+\"(?<optValue>[\\w./;\\\\\\-]+)\"\\s*;\\s*$");
"(?m)^option\\s+(?<optName>(java|go|csharp|ruby|php|javascript)_(namespace|package))\\s+=\\s+\"(?<optValue>[\\w./;\\\\\\-]+)\"\\s*;\\s*$");
Copy link
Contributor

Choose a reason for hiding this comment

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

Was the less specific regexp failing on practical cases (if yes, can you please give an example?), or is this change mostly to avoid something like that in the future?

Copy link
Contributor Author

@miraleung miraleung Feb 19, 2021

Choose a reason for hiding this comment

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

Yes, it was failing. For Java, it grabbed the first available option, resulting in the wrong value. For example, since java_outer_classname comes first in the asset proto, we ended up with the map entry "java" : "AssetProto" instead of the expected "java" : "com.google.cloud.asset.v1". I think this worked for other languages because they generally have only one config option.

@@ -361,9 +366,11 @@ void parseBazelBuildFile(Path file) {
// Duplicated rule of the same kind will break our logic for preserving rule name.
System.err.println("There are more than one rule of kind " + kind + ".");
System.err.println(
"Bazel build file generator does not support regenerating BUILD.bazel in this case.");
"Bazel build file generator does not support regenerating BUILD.bazel in this"
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are already touching this code and split it into concatenated strings, can you please move all that into single System.err.println() (instead of using subseuent calls to print essentially a single multi-line error message)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

bp.getLangGapicNameOverrides()
.get("java")
.getOrDefault(bp.getProtoPackage() + "." + service, service);
bp.getLangGapicNameOverrides().containsKey("java")
Copy link
Contributor

Choose a reason for hiding this comment

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

I've seen recently the test targets for java test rules generated with empty test class list. Looks like this has potential of fixing that, right? If yes, then it is awesome!

@@ -80,7 +80,7 @@ java_gapic_library(
java_gapic_test(
name = "library_java_gapic_test_suite",
test_classes = [
"com.google.cloud.example.library.v1.LibraryServiceClientTest",
"com.google.example.library.v1.LibraryServiceClientTest",
Copy link
Contributor

Choose a reason for hiding this comment

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

The actual clients (regardless if they are micro or macro generated) are still distributed under "cloud" namespace. Where does the "cloud" portion in the namespace should come from now, if not from gapic yaml?

I'm afraid that we may still be generating client libraries under cloud namespace, but the tests specified in the build file may be loosing the cloud portion of the package, making it a broken test class name essentially.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you're referring to this Library client specifically, any other suggested value for java_package in the protos are welcome. This test means to exercise the "detect package from proto" case with a package distinct from the gapic.yaml one, so any value that achieves that is fine.

For other APIs, many have .cloud in their proto-specified java_package (e.g. Functions).

@@ -0,0 +1,336 @@
# This file was automatically generated by BuildFileGenerator
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really want to keep the legacy tests? It seems like a lot of text with sophisticated folder structure is added only to test the very small, legacy and rare case of reading from gapic yaml. I think it is ok just to not test that obscure execution path in the tests.

Copy link
Contributor Author

@miraleung miraleung Feb 19, 2021

Choose a reason for hiding this comment

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

Yes - we need to ensure we don't break the 10+ clients who still use this. We also need to ensure the delta between the main and legacy test cases are clear, otherwise an untested edge case could easily slip past.

We could remove sections of the library proto that don't affect imports (for both cases), and I'd be happy to do that in a separate PR.

Copy link
Contributor

@vam-google vam-google left a comment

Choose a reason for hiding this comment

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

LGTM

@miraleung miraleung merged commit dc4dd84 into main Feb 19, 2021
@miraleung miraleung deleted the fix/build_gen branch February 19, 2021 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: cloudbuild Issues related to the Cloud Build API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants