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

language/go: Add support for //go:build tags #1243

Merged
merged 9 commits into from
Jul 17, 2022

Conversation

thempatel
Copy link
Contributor

What type of PR is this?

Feature

What package or component does this PR mostly affect?

language/go

What does this PR do? Why is it needed?

Adds support for the new //go:build build tags syntax introduced in go1.17

Which issues(s) does this PR fix?

Fixes #1099

Other notes for review

@sluongng
Copy link
Contributor

My advice is to split this PR into smaller ones for easier reviewing:

  • 1 PR for refactoring test comparision and introduce com_github_google_go_cmp
  • 1 PR to add support for //go:build tags

@thempatel
Copy link
Contributor Author

@sluongng #1244

@thempatel thempatel force-pushed the milan/go-build-syntax branch 7 times, most recently from 6939f62 to 497dad6 Compare May 1, 2022 18:02
@thempatel thempatel force-pushed the milan/go-build-syntax branch 2 times, most recently from b5e86ab to b982fbc Compare May 8, 2022 14:31
@thempatel thempatel force-pushed the milan/go-build-syntax branch 3 times, most recently from 7e7bac8 to 69abeda Compare May 8, 2022 15:34
@thempatel
Copy link
Contributor Author

@sluongng PTAL

cc: @achew22 in case you are interested and want to review

@thempatel thempatel force-pushed the milan/go-build-syntax branch 3 times, most recently from 90b1ad2 to ea0b5ac Compare May 8, 2022 18:59
Copy link
Contributor

@sluongng sluongng left a comment

Choose a reason for hiding this comment

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

@thempatel thanks so much for working on this.

I have several nits as commented. Generally I would recommend moving some of the added, mirror functionality from go/build/constraint to a new file. fileinfo.go is approaching 1000 lines status and the diff hunks were quite hard to consume.

Very glad that you were able to refactor the unit tests using the 2 new structs. Could you please also provide 1 simple test cases (which only uses //go:build and not the old // +build) in testdata as integration test for this? The testdata are used in TestGenerateRules in generate_test.go FYI.

language/go/fileinfo.go Outdated Show resolved Hide resolved
language/go/fileinfo.go Outdated Show resolved Hide resolved
language/go/fileinfo.go Outdated Show resolved Hide resolved
language/go/fileinfo.go Show resolved Hide resolved
language/go/fileinfo.go Show resolved Hide resolved
language/go/fileinfo.go Outdated Show resolved Hide resolved
language/go/fileinfo.go Outdated Show resolved Hide resolved
language/go/fileinfo_test.go Outdated Show resolved Hide resolved
language/go/generate_test.go Show resolved Hide resolved
@thempatel
Copy link
Contributor Author

@sluongng PTAL

Copy link
Contributor

@sluongng sluongng left a comment

Choose a reason for hiding this comment

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

I only have small nits left.

I think this version is a lot cleaner and easier to digest. Breaking the diff into smaller files was definitely the right way to go.

fileinfo.go diff is still quite noisy to digest through. But the test passing is additional assurance.

There are small scope creeps here and there with the usage of cmp.Diff but IMO the end result is strictly better.

Pinging @achew22 @linzhp for additional reviews

language/go/fileinfo.go Outdated Show resolved Hide resolved
language/go/fileinfo.go Outdated Show resolved Hide resolved
language/go/fileinfo_test.go Outdated Show resolved Hide resolved
@thempatel
Copy link
Contributor Author

@sluongng PTAL

@thempatel
Copy link
Contributor Author

@sluongng @achew22 @linzhp ping, hoping to get this in -- we have work blocked on this particular body of code making it's way to mainline

Copy link
Contributor

@linzhp linzhp left a comment

Choose a reason for hiding this comment

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

When we have

-- collector_linux.go --
//go:build linux && amd64

package cpufreqcounters

import (
	"uber.com/inventory"
	"uber.com/file"
)

-- collector_other.go --
//go:build !(linux && amd64)

package cpufreqcounters

import (
	"uber.com/file"
)

This pull request would lead to huge list of deps in go_library like:

    deps = select({
        "@io_bazel_rules_go//go/platform:aix_ppc64": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:android_386": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:android_amd64": [
            "//uber.com/file",
            "//uber.com/inventory",
        ],
        "@io_bazel_rules_go//go/platform:android_arm": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:android_arm64": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:darwin_386": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:darwin_amd64": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:darwin_arm": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:darwin_arm64": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:dragonfly_amd64": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:freebsd_386": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:freebsd_amd64": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:freebsd_arm": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:freebsd_arm64": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:illumos_amd64": [
            "//uber.com/file",
        ],
        "@io_bazel_rules_go//go/platform:linux_amd64": [
            "//uber.com/file",
            "//uber.com/inventory",
        ],
        "//conditions:default": [],

Can you simplify it to something like:

    deps = [
            "//uber.com/file",
    ] + select({
        "@io_bazel_rules_go//go/platform:android": [
            "//uber.com/inventory",
        ],
        "@io_bazel_rules_go//go/platform:linux": [
            "//uber.com/inventory",
        ],
        "//conditions:default": [],
    }),

or

    deps = select({
        "@io_bazel_rules_go//go/platform:android": [
            "//uber.com/file",
            "//uber.com/inventory",
        ],
        "@io_bazel_rules_go//go/platform:linux": [
            "//uber.com/file",
            "//uber.com/inventory",
        ],
        "//conditions:default":  [
            "//uber.com/file",
        ],
    }),

// rest of the file by a blank line. Each string in the returned slice
// is the trimmed text of a line after a "+build" prefix.
// Based on go/build.Context.shouldBuild.
func readTags(path string) (*buildTags, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: According to Uber style guide:

Functions should be sorted in rough call order.

So it's better to put readTags at the top of this file.

@thempatel
Copy link
Contributor Author

@linzhp I'm somewhat confused by that, does that mean that the existing build tags logic is broken? How does it work today? Is the long list a problem, these are generated files so I would lean toward not introducing scope creep to make the file prettier unless the generated code is causing bugs/issues

@thempatel
Copy link
Contributor Author

I think the only concern left is a very uncommon edge case: users would have to be using cgo and is converting a code base from normal go build to rules_go for the first time to get affected.

Could you help me understand this edge case (for my own edification), my understanding is if a new repo becomes managed by gazelle and they are using cgo, then the BUILD files should be generated correctly.

Even when that happen, we provide a work around in gazelle by using # keep to fall back to manual BUILD file configuration.

this makes sense to me!

The question here is mainly: how much interops between rules_go and go build do we want to support? at what cost?

I am not sure, to me it seems like it would be situational, maybe some existing examples would help bring clarity

Imo some of the API in build_tags.go should be provided and customizable from Go SDK and Gazelle should only call into them instead of currrent approach of fork/vendor the implementation. But it might take more time to convince Go team to implement that API.

In an ideal world I agree, but I think it is unlikely we'll be able to convince the go team within any reasonable timeframe and we have many other things to do.

@linzhp PTAL

@linzhp
Copy link
Contributor

linzhp commented Jul 12, 2022

Sorry, I am getting swamped by the perf season. Would you mind rebasing on master so it's easier for me to test?

@thempatel
Copy link
Contributor Author

@linzhp rebased and pushed, PTAL

@linzhp
Copy link
Contributor

linzhp commented Jul 15, 2022

Some tests are failing

@thempatel
Copy link
Contributor Author

@linzhp pushed some old code, apologies. Things look green now

Copy link
Contributor

@linzhp linzhp left a comment

Choose a reason for hiding this comment

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

All tests in Uber passed. Thanks for the contribution.

@linzhp linzhp merged commit 35b00c2 into bazelbuild:master Jul 17, 2022
@thempatel thempatel deleted the milan/go-build-syntax branch July 17, 2022 22:02
@thempatel
Copy link
Contributor Author

thanks for the collaboration everyone!

@pcj
Copy link
Member

pcj commented Aug 22, 2022

The strings.Cut part causing upgrade pain.

@linzhp
Copy link
Contributor

linzhp commented Aug 22, 2022

Can you come up with a PR to fix it?

@thempatel
Copy link
Contributor Author

I'm sorry to hear that, I will point out that bazel-gazelle's minimum supported version is go1.18

and given that strings.Cut was added in go1.18, it felt like a safe change to make. If possible, please consider upgrading your version of Go.

@pcj
Copy link
Member

pcj commented Aug 23, 2022

Yeah, it happens. Just one of those many cases where you think upgrading something will be a small task and it morphs into a bigger one.

kreempuff added a commit to kreempuff/rules_unreal_engine that referenced this pull request Jan 31, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [bazel_gazelle](https://togithub.com/bazelbuild/bazel-gazelle) |
http_archive | minor | `v0.26.0` -> `v0.29.0` |

---

### Release Notes

<details>
<summary>bazelbuild/bazel-gazelle</summary>

###
[`v0.29.0`](https://togithub.com/bazelbuild/bazel-gazelle/releases/tag/v0.29.0)

[Compare
Source](https://togithub.com/bazelbuild/bazel-gazelle/compare/v0.28.0...v0.29.0)

#### What's Changed

- bzlmod: Update Publish to BCR app config by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1363
- Fix: Skip default_visibility extension logic if no BUILD.bazel file
present by [@&#8203;dnathe4th](https://togithub.com/dnathe4th) in
[bazelbuild/bazel-gazelle#1364
- fix updateStmt makeslice panic by
[@&#8203;pcj](https://togithub.com/pcj) in
[bazelbuild/bazel-gazelle#1371
- bzlmod: Add missing `strip_prefix` field to `source.template.json` by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1375
- feat: support common test args in `gazelle_generation_test` by
[@&#8203;cgrindel](https://togithub.com/cgrindel) in
[bazelbuild/bazel-gazelle#1377
- Make the new facts pacakge public by
[@&#8203;linzhp](https://togithub.com/linzhp) in
[bazelbuild/bazel-gazelle#1378
- fix: add timeout message for `gazelle_generation_test` by
[@&#8203;cgrindel](https://togithub.com/cgrindel) in
[bazelbuild/bazel-gazelle#1383
- bzlmod: Add missing repository metadata by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1387
- Replace `cfg = "host"` with `cfg = "exec"` by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1395
- upgrade rules_go to 0.37.0 by
[@&#8203;JamyDev](https://togithub.com/JamyDev) in
[bazelbuild/bazel-gazelle#1386
- Fix embed on windows by [@&#8203;st3veV](https://togithub.com/st3veV)
in
[bazelbuild/bazel-gazelle#1361
- Update bazel-skylib to 1.3.0. by
[@&#8203;benjaminp](https://togithub.com/benjaminp) in
[bazelbuild/bazel-gazelle#1367
- Fix Directives anchor by [@&#8203;jmthvt](https://togithub.com/jmthvt)
in
[bazelbuild/bazel-gazelle#1353
- Use `patch` from `@bazel_tools//tools/build_defs/repo` by
[@&#8203;tyler-french](https://togithub.com/tyler-french) in
[bazelbuild/bazel-gazelle#1381
- Add link to BenchSci's rules_nodejs_gazelle extension by
[@&#8203;ColinHeathman](https://togithub.com/ColinHeathman) in
[bazelbuild/bazel-gazelle#1369
- bzlmod: Skip Go modules available as Bazel modules by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1403
- repo: opportunistically populate RemoteCache from go.mod by
[@&#8203;jayconrod](https://togithub.com/jayconrod) in
[bazelbuild/bazel-gazelle#1396
- Fix Gazelle with `--incompatible_disallow_empty_glob` by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1405
- chore: remove experimental warning from bzlmod module by
[@&#8203;alexeagle](https://togithub.com/alexeagle) in
[bazelbuild/bazel-gazelle#1406
- chore: add Swift extension to language list by
[@&#8203;cgrindel](https://togithub.com/cgrindel) in
[bazelbuild/bazel-gazelle#1412
- Update everything for release prep, add releaser tool by
[@&#8203;dnathe4th](https://togithub.com/dnathe4th) in
[bazelbuild/bazel-gazelle#1373
- adding go version and std_package_list to releaser by
[@&#8203;linzhp](https://togithub.com/linzhp) in
[bazelbuild/bazel-gazelle#1415

#### New Contributors

- [@&#8203;damingerdai](https://togithub.com/damingerdai) made their
first contribution in
[bazelbuild/bazel-gazelle#1362
- [@&#8203;st3veV](https://togithub.com/st3veV) made their first
contribution in
[bazelbuild/bazel-gazelle#1361
- [@&#8203;benjaminp](https://togithub.com/benjaminp) made their first
contribution in
[bazelbuild/bazel-gazelle#1367
- [@&#8203;jmthvt](https://togithub.com/jmthvt) made their first
contribution in
[bazelbuild/bazel-gazelle#1353
- [@&#8203;ColinHeathman](https://togithub.com/ColinHeathman) made their
first contribution in
[bazelbuild/bazel-gazelle#1369

**Full Changelog**:
bazelbuild/bazel-gazelle@v0.28.0...v0.29.0

###
[`v0.28.0`](https://togithub.com/bazelbuild/bazel-gazelle/releases/tag/v0.28.0)

[Compare
Source](https://togithub.com/bazelbuild/bazel-gazelle/compare/v0.27.0...v0.28.0)

#### What's Changed

- language/proto: gen_known_imports creates structs instead of function
calls by [@&#8203;eric-skydio](https://togithub.com/eric-skydio) in
[bazelbuild/bazel-gazelle#1333
- Add DoneGeneratingRules language hook by
[@&#8203;illicitonion](https://togithub.com/illicitonion) in
[bazelbuild/bazel-gazelle#1325
- Allow configuring timeout of generation tests by
[@&#8203;illicitonion](https://togithub.com/illicitonion) in
[bazelbuild/bazel-gazelle#1324
- bug: Allow user-specified tags on gazelle rule by
[@&#8203;Helcaraxan](https://togithub.com/Helcaraxan) in
[bazelbuild/bazel-gazelle#1308
- Replace \_get_auth with Bazel's read_user_netrc by
[@&#8203;linzhp](https://togithub.com/linzhp) in
[bazelbuild/bazel-gazelle#1338
- language/go should consider default_visibility set by OtherGen
([#&#8203;783](https://togithub.com/bazelbuild/bazel-gazelle/issues/783))
by [@&#8203;dnathe4th](https://togithub.com/dnathe4th) in
[bazelbuild/bazel-gazelle#1341
- fix: pass `visibility` attribute for `gazelle` macro to resulting
`sh_binary` by [@&#8203;cgrindel](https://togithub.com/cgrindel) in
[bazelbuild/bazel-gazelle#1340
- Add additional bzlmod requirements to allow grpc protobufs to work by
[@&#8203;shs96c](https://togithub.com/shs96c) in
[bazelbuild/bazel-gazelle#1345
- bzlmod: Simplify go_grpc_library support by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1346
- bzlmod: Add support for custom `go_proto_library` compilers by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1348
- Add visibility extension to support recursive default_visibility
([#&#8203;783](https://togithub.com/bazelbuild/bazel-gazelle/issues/783))
by [@&#8203;dnathe4th](https://togithub.com/dnathe4th) in
[bazelbuild/bazel-gazelle#1343
- Make `gazelle_generation_test` respect out suffix when generating
golden files by [@&#8203;blorente](https://togithub.com/blorente) in
[bazelbuild/bazel-gazelle#1352
- Add size argument to `gazelle_generation_test` by
[@&#8203;charlesoconor](https://togithub.com/charlesoconor) in
[bazelbuild/bazel-gazelle#1351

#### New Contributors

- [@&#8203;eric-skydio](https://togithub.com/eric-skydio) made their
first contribution in
[bazelbuild/bazel-gazelle#1333
- [@&#8203;dnathe4th](https://togithub.com/dnathe4th) made their first
contribution in
[bazelbuild/bazel-gazelle#1341
- [@&#8203;cgrindel](https://togithub.com/cgrindel) made their first
contribution in
[bazelbuild/bazel-gazelle#1340
- [@&#8203;shs96c](https://togithub.com/shs96c) made their first
contribution in
[bazelbuild/bazel-gazelle#1345
- [@&#8203;blorente](https://togithub.com/blorente) made their first
contribution in
[bazelbuild/bazel-gazelle#1352
- [@&#8203;charlesoconor](https://togithub.com/charlesoconor) made their
first contribution in
[bazelbuild/bazel-gazelle#1351

**Full Changelog**:
bazelbuild/bazel-gazelle@v0.27.0...v0.28.0

###
[`v0.27.0`](https://togithub.com/bazelbuild/bazel-gazelle/releases/tag/v0.27.0)

[Compare
Source](https://togithub.com/bazelbuild/bazel-gazelle/compare/v0.26.0...v0.27.0)

#### What's Changed

- Use repo-relative labels everywhere by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1294
- Fix RST URL errors for rules_jvm by
[@&#8203;qaisjp](https://togithub.com/qaisjp) in
[bazelbuild/bazel-gazelle#1296
- bzlmod prototype by [@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1266
- bzlmod: Do not create a repository with an invalid name by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1304
- language/go: Add support for //go:build tags by
[@&#8203;thempatel](https://togithub.com/thempatel) in
[bazelbuild/bazel-gazelle#1243
- Unwrap `go list -m -json` errors correctly by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1301
- Make one more label repo-relative by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1297
- bzlmod: Add go_deps.from_file by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1300
- language: add BaseLang by
[@&#8203;sluongng](https://togithub.com/sluongng) in
[bazelbuild/bazel-gazelle#1303
- Allow adding arguments to Rules by
[@&#8203;illicitonion](https://togithub.com/illicitonion) in
[bazelbuild/bazel-gazelle#1310
- Register and parse flags before calling Kinds/Loads by
[@&#8203;illicitonion](https://togithub.com/illicitonion) in
[bazelbuild/bazel-gazelle#1318
- SortMacro() should also sort the Loads by
[@&#8203;tyler-french](https://togithub.com/tyler-french) in
[bazelbuild/bazel-gazelle#1321
- bzlmod: Fix canonical label literal after Bazel change by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1322
- update-repos: don't add repositories declared with gazelle:repository…
by [@&#8203;tyler-french](https://togithub.com/tyler-french) in
[bazelbuild/bazel-gazelle#1326
- Look in call args for loadable symbols by
[@&#8203;illicitonion](https://togithub.com/illicitonion) in
[bazelbuild/bazel-gazelle#1317
- SortMacro() should also sort rules by Kind() by
[@&#8203;tyler-french](https://togithub.com/tyler-french) in
[bazelbuild/bazel-gazelle#1327
- bzlmod: Fix missing .format in go_deps by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1330
- bzlmod: Depend on rules_proto by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[bazelbuild/bazel-gazelle#1331

#### New Contributors

- [@&#8203;qaisjp](https://togithub.com/qaisjp) made their first
contribution in
[bazelbuild/bazel-gazelle#1296

**Full Changelog**:
bazelbuild/bazel-gazelle@v0.26.0...v0.27.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/kreempuff/rules_unreal_engine).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMTcuMSIsInVwZGF0ZWRJblZlciI6IjM0LjExNy4xIn0=-->
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.

go:build syntax is not supported
4 participants