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

Golang source sets (go_source) are not supported: source files are unsynced #1265

Closed
wingsofovnia opened this issue Oct 15, 2019 · 10 comments
Closed
Assignees
Labels
awaiting-maintainer Awaiting review from Bazel team on issues lang: go Go rules integration topic: sync Issues related to the sync operation type: feature request

Comments

@wingsofovnia
Copy link

Bazel golang rule supports source sets and their further embedding into binaries/tests (https://github.com/bazelbuild/rules_go/blob/master/go/core.rst#embedding).

We are using this extensively and unfortunately, this is not supported by Bazel Intellij plugin: all sources defined via go_source & embed attr are "unsynced".

Setup

.bazelproject

directories:
  .
derive_targets_from_directories: false
targets:
  //myapp:all
additional_languages:
  go

Bazel build file

go_source(
    name = "src",
    srcs = glob(
        include = ["*.go"],
        exclude = ["*_test.go"],
    ),
    deps = [],
)
go_binary(
    name = "app",
    embed = [":src"],
)

Expected behavior

All source files matched by go_source#srcs are synced.

Actual behavior

go_binary#embed is ignored and all sources are unsynced.

Walkaround

It seems that Bazel plugin doesn't understand go_source so all sources must be specified explicitly in go_binary#srcs. This syncs up correctly:

Bazel build file

go_binary(
    name = "app",
    srcs = glob(
        include = ["*.go"],
        exclude = ["*_test.go"],
    ),
)
@chaoren
Copy link
Collaborator

chaoren commented Oct 15, 2019

Is a go_source just a go_library without the import path?

@wingsofovnia
Copy link
Author

@chaoren I am not sure I understand the question. We use go_source for grouping sources so that they can be reused in go_binary and go_test. It might be go_library instead of go_binary in my example.

@wingsofovnia
Copy link
Author

wingsofovnia commented Oct 16, 2019

@chaoren I've just discovered that go_test doesn't experience the same problem! Giving that Bazel build file looks the following way:

go_source(
    name = "src",
    srcs = glob(
        include = ["*.go"],
        exclude = ["*_test.go"],
    ),
    deps = [],
)
go_binary(
    name = "app",
    embed = [":src"],
)
go_test(
    name = "test",
    srcs = glob(
        include = ["*_test.go"],
    ),
    embed = [":src"],
    deps = [
        "@com_github_stretchr_testify//assert:go_default_library",
    ],
)

.bazelproject with targets: //myapp:all will leave all *.go unsynced, however, *_test.go from go_test will be synced!

@chaoren
Copy link
Collaborator

chaoren commented Oct 17, 2019

Yeah, only embeds with tests are handled as that's the only kind of embedding allowed internally. The various kinds of embedding in the bazel rule complicates the situation a bit, as it now allows multiple distinct rules to occupy the same go package.

@wingsofovnia
Copy link
Author

@chaoren what would be the best way to write similar #1265 (comment) Bazel build file avoiding src definition duplication? Should I just define go_binary explicitly and embed it to the test?

@chaoren
Copy link
Collaborator

chaoren commented Oct 18, 2019

Should I just define go_binary explicitly and embed it to the test?

That would work yes.

@wingsofovnia
Copy link
Author

@chaoren Thank you. Do you plan to support embeds with binaries/libraries in the future tho?

@wingsofovnia
Copy link
Author

@chaoren embedding go_binary to go_test is deprecated and it is not possible to embed it into genrule. In a real project, source and dependency lists are quite long and duplicating it for every rule makes it easier to forget to change something and adds a lot of boilerplate code, therefore, I believe it is not a feasible solution. In other words, for big projects with long BUILD files, this plugin becomes unusable :(

@mai93 mai93 added lang: go Go rules integration type: feature request topic: sync Issues related to the sync operation labels Feb 11, 2021
@sgowroji sgowroji assigned HaimingTian and unassigned chaoren Nov 17, 2022
@sgowroji sgowroji added the awaiting-maintainer Awaiting review from Bazel team on issues label Nov 17, 2022
@sgowroji
Copy link
Member

Hi there! We're doing a clean up of old issues and will be closing this one. Please reopen if you’d like to discuss anything further. We’ll respond as soon as we have the bandwidth/resources to do so.

@lbcjbb
Copy link
Contributor

lbcjbb commented Aug 6, 2023

Hi,

I hope this issue can be reopened. In my project I use a lot of go_source rules for all generated mock with mockey.

A few months ago I created a simple Bazel project to test all possibiliies to use a go_source in a go_library and go_test rule: https://github.com/leboncoin/bazel-playground/tree/intellij-go-source.

I've just submitted the PR #5205 to support the use of an go_source rule in a go_test or go_library rule, and these submitted changes fix my plugin in Intellij UE. All my generated mock files are synced to the project.

lbcjbb added a commit to leboncoin/bazel-intellij that referenced this issue Aug 17, 2023
lbcjbb added a commit to leboncoin/bazel-intellij that referenced this issue Aug 21, 2023
blorente pushed a commit that referenced this issue Aug 22, 2023
…rules (#5205)

* Support embedded go_source rule type in go_test and go_library rules

Issue: #1265

* Add example to embed a go_source rule in a go_library and a go_test rule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-maintainer Awaiting review from Bazel team on issues lang: go Go rules integration topic: sync Issues related to the sync operation type: feature request
Projects
None yet
Development

No branches or pull requests

6 participants