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

chore: [hermetic-build] simplify usage of synthtool #2772

Merged
merged 7 commits into from
May 21, 2024

Conversation

diegomarquezp
Copy link
Contributor

@diegomarquezp diegomarquezp commented May 15, 2024

This PR eliminates the need for:

  • library_generation/owlbot/src/apply_repo_templates.py
  • Passing the config yaml path to the postprocessing scripts

How is it achieved?

We set the environment variable SYNTHTOOL_TEMPLATES to point to a
temp dir where we copy the contents of owlbot/templates plus a
modification of the template using sed (instead of
synthtool.common_templates). In this way, we avoid the usage of
apply_repo_templates.py.
generation_config.template_excludes.

Why don't we use common_templates to pass libraries_bom_version?

Because owlbot.py is a legacy script that was written when
java.common_templates didn't take libraries_bom_version as a
parameter. If we don't like this approach of having two template
replacement workflows we could

  1. Modify all owlbot.py files
  2. Pass libraries_bom_version as an environment variable (not bad)

What about template_excludes in the config yaml?

We still need it to generate new libraries. This is done by rendering a
new owlbot.py via templates.

This PR eliminates the need for:
 * `library_generation/owlbot/src/apply_repo_templates.py`
 * Passing the path to the config yaml to the postprocessing scripts

 ### How is it achieved?
 We set the environment variable `SYNTHTOOL_TEMPLATES` to point to a
 temp dir where we copy the contents of `owlbot/templates` plus a
 modification of the template using `sed` (instead of
 `synthtool.common_templates`). In this way, we avoid the usage of
 `apply_repo_templates.py`.
 generation_config.template_excludes.

 #### Why don't we use common_templates to pass libraries_bom_version?
 Because `owlbot.py` is a legacy script that was written when
 `java.common_templates` didn't take `libraries_bom_version` as a
 parameter. If we don't like this approach of having two template
 replacement workflows we could

 1. Modify all owlbot.py files
 2. Pass libraries_bom_version as an environment variable (not bad)

 #### What about template_excludes in the config yaml?
 We still need it to generate new libraries. This is done by rendering a
 new `owlbot.py` via templates.
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label May 15, 2024
@diegomarquezp diegomarquezp added hermetic-build owlbot:run Add this label to trigger the Owlbot post processor. labels May 15, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label May 15, 2024
@diegomarquezp diegomarquezp marked this pull request as ready for review May 15, 2024 14:42
@diegomarquezp diegomarquezp requested a review from a team as a code owner May 15, 2024 14:42
export SYNTHTOOL_TEMPLATES=$(mktemp -d)
cp -r ${scripts_root}/owlbot/templates/* "${SYNTHTOOL_TEMPLATES}"
sed -i 's/\$\$__libraries_bom_version__\$\$/'"${libraries_bom_version}"'/g' "${SYNTHTOOL_TEMPLATES}/java_library/README.md"
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if we migrate java.py to sdk-platform-java? Do you think of another solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be ideal. A couple considerations:

  • owlbot.py imports synthtool, meaning that we would need to not only transfer java.py but other scripts such as common.py (or we could merge all the logic in a single script)
  • If we migrate synthtool/java.py, we would still need a way to pass libraries_bom_version to owlbot.py. We can either modify owlbot.py or we can rely on an environment variable consumed in java.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another more hacky approach would be to pass the version in repo-metadata

Copy link
Collaborator

Choose a reason for hiding this comment

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

The logic here seems not easy to maintain, as it is tightly coupled with a magic string __libraries_bom_version__.
I'm leaning towards the environment variable approach you mentioned. If we go with that approach, I guess we should not need any changes in this file or the template, we just need to load the libraries bom version from environment variable and add it to the kwargs in synthtool?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added googleapis/synthtool#1967 to infer the bom version from the environment. Once that's merged, we can enable the env var approach.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What was this file used for? I think it was not only for libraries_bom_version? Or maybe it was a mistake that we rendered the templates twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It allowed us to run common_templates with the list of template_excludes listed in the config yaml

export SYNTHTOOL_TEMPLATES=$(mktemp -d)
cp -r ${scripts_root}/owlbot/templates/* "${SYNTHTOOL_TEMPLATES}"
sed -i 's/\$\$__libraries_bom_version__\$\$/'"${libraries_bom_version}"'/g' "${SYNTHTOOL_TEMPLATES}/java_library/README.md"
Copy link
Collaborator

Choose a reason for hiding this comment

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

The logic here seems not easy to maintain, as it is tightly coupled with a magic string __libraries_bom_version__.
I'm leaning towards the environment variable approach you mentioned. If we go with that approach, I guess we should not need any changes in this file or the template, we just need to load the libraries bom version from environment variable and add it to the kwargs in synthtool?

Copy link

Quality Gate Passed Quality Gate passed for 'gapic-generator-java-root'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link

Quality Gate Passed Quality Gate passed for 'java_showcase_integration_tests'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@diegomarquezp diegomarquezp merged commit 38b90e2 into main May 21, 2024
48 checks passed
@diegomarquezp diegomarquezp deleted the simplify-synthtool-templates branch May 21, 2024 21:00
@JoeWang1127
Copy link
Collaborator

cloud build failed after merging this PR:

fatal: reference is not a tree: a2c9b4a5da2d7f583c8a1869fd2843c206145834
The command '/bin/sh -c git checkout "${SYNTHTOOL_COMMITTISH}"' returned a non-zero code: 128

lqiu96 pushed a commit that referenced this pull request May 22, 2024
This PR eliminates the need for:
 * `library_generation/owlbot/src/apply_repo_templates.py`
 * Passing the config yaml path to the postprocessing scripts

 ### How is it achieved?
 We set the environment variable `SYNTHTOOL_TEMPLATES` to point to a
 temp dir where we copy the contents of `owlbot/templates` plus a
 modification of the template using `sed` (instead of
 `synthtool.common_templates`). In this way, we avoid the usage of
 `apply_repo_templates.py`.
 generation_config.template_excludes.

 #### Why don't we use common_templates to pass libraries_bom_version?
 Because `owlbot.py` is a legacy script that was written when
 `java.common_templates` didn't take `libraries_bom_version` as a
 parameter. If we don't like this approach of having two template
 replacement workflows we could

 1. Modify all owlbot.py files
 2. Pass libraries_bom_version as an environment variable (not bad)

 #### What about template_excludes in the config yaml?
 We still need it to generate new libraries. This is done by rendering a
 new `owlbot.py` via templates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hermetic-build size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants