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

Support polyglot BUILD file #170

Merged
merged 6 commits into from Dec 11, 2023
Merged

Support polyglot BUILD file #170

merged 6 commits into from Dec 11, 2023

Conversation

eed3si9n
Copy link
Contributor

@eed3si9n eed3si9n commented Dec 8, 2023

Fixes #169

Problem
Currently it's not possible to mix languages within a package.

Solution

  1. This implements --append flag to allow appending contents to BUILD.bazel.
  2. This also adds include_file_extension configuration to allow py_test target to keep the name of the file as the target name.


fn add_non_empty(
opt: &'static Opt,
key: &str,
labels: &Vec<String>,
extra_kv_pairs: &mut HashMap<String, Vec<String>>,
include_file_extension: bool,
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we avoid boolean blindness with a small Enum to represent the semantics:

enum TargetNameStrategy { Directory, DirectoryFileType }

or something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in d5f87f4

crates/driver/src/print_build.rs Outdated Show resolved Hide resolved
crates/driver/src/print_build.rs Outdated Show resolved Hide resolved
crates/python_extractor/src/lib.rs Outdated Show resolved Hide resolved
**Problem**
Currently it's not possible to mix languages within a package.

**Solution**
1. This implements `--append` flag to allow appending contents
   to BUILD.bazel.
2. This also adds `include_file_extension` configuration to
   allow `py_test` target to keep the name of the file
   as the target name.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's an example output of a polyglot BUILD.

@@ -4,7 +4,7 @@ package com.example.foo;

option java_multiple_files = true;

import "src/main/protos/com/example/aa.proto";
import "com/example/aa.proto";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No src/main/protos/ here.

@eed3si9n eed3si9n requested a review from a team December 8, 2023 18:03
Ok(file_name.replace(".", "_"))
} else {
match Path::new(&file_name).file_stem() {
Some(s) => Ok(s.to_str().unwrap().to_string()),
Copy link
Contributor

Choose a reason for hiding this comment

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

This wont panic because file_stem is always going to return a valid OsStr, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At least I'm hoping that's the case.

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 might just use to_string_lossy() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in d5f87f4

Copy link
Collaborator

Choose a reason for hiding this comment

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

we are already returning a Result. Why not pass on the original error or rebox it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

s.to_str() returns an Option, and my guess is that it's highly unlikely that it would fail.

Copy link

@non non left a comment

Choose a reason for hiding this comment

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

The changes themselves seemed OK but I had a question about the expected behavior/usage of the tool.

@@ -5,3 +5,6 @@ proto_library(name='aa_proto', srcs=['aa.proto'], visibility=['//visibility:publ
java_proto_library(name='aa_proto_java', deps=[':aa_proto'], visibility=['//visibility:public'])
py_proto_library(name='aa_proto_py', deps=[':aa_proto'], visibility=['//visibility:public'])
# ---- END BZL_GEN_BUILD_GENERATED_CODE ---- no_hash
# ---- BEGIN BZL_GEN_BUILD_GENERATED_CODE ---- no_hash
py_library(name='hello', srcs=['hello.py'], deps=['@@rules_python~0.24.0~pip~pip_39_pandas//:pkg'], visibility=['//visibility:public'])
# ---- END BZL_GEN_BUILD_GENERATED_CODE ---- no_hash
Copy link

Choose a reason for hiding this comment

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

Is it expected to have two different generated sections with the same name?

I had thought we'd only have one of these, or else we'd use two different names for the different parts. (That way each generated section could be safely updated independently without affecting any others.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No the target name must be unique within a package. This package demonstrates that bzl-gen-build can be configured to produce "hello" package even though it's inside of the "example" package. Normally we make a target based on the package (directory) name not the source file name.

Copy link

@non non Dec 11, 2023

Choose a reason for hiding this comment

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

Sorry i wasn't more clear. I was actually referring to the generated comment section names (i.e. "BZL_GEN_BUILD_GENERATED_CODE"). If we have two generators "sharing" a file and want them each to be able to update their own section safely they'd need to be able to distinguish those sections somehow.

It's not really a big deal, and it would be easy to change I think, I just wanted to point it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At least in the way it's done right now, the polyglot support is implemented by simply appending output after another, so yea, we end up with multiple codegen regions. I guess it's something we can follow up.

Copy link

@non non left a comment

Choose a reason for hiding this comment

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

Looks OK to me (once main is merged). I'd like it if the polyglot appenders used unique names but that's easy to do later.

@eed3si9n eed3si9n enabled auto-merge (squash) December 11, 2023 23:05
@eed3si9n eed3si9n merged commit 9b6e1f0 into bazeltools:main Dec 11, 2023
6 checks passed
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.

Support polyglot BUILD.bazel ("Google3" layout)
5 participants