Skip to content

Commit

Permalink
Add example to embed a go_source rule in a go_library and a go_test rule
Browse files Browse the repository at this point in the history
  • Loading branch information
lbcjbb committed Aug 21, 2023
1 parent e880884 commit 4ffefc9
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ awaiting-review:

'product: GoLand':
- base/**/*
- examples/go/with_proto/**/*
- examples/go/**/*
- gazelle/**/*
- golang/**/*
4 changes: 4 additions & 0 deletions examples/go/with_go_source/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix github.com/bazelbuild/intellij/examples/go/with_go_source
gazelle(name = "gazelle")
36 changes: 36 additions & 0 deletions examples/go/with_go_source/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
workspace(name = "bazel-playground")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
sha256 = "278b7ff5a826f3dc10f04feaf0b70d48b68748ccd512d7f98bf442077f043fe3",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
],
)

http_archive(
name = "bazel_gazelle",
sha256 = "29218f8e0cebe583643cbf93cae6f971be8a2484cdcfa1e45057658df8d54002",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.32.0/bazel-gazelle-v0.32.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.32.0/bazel-gazelle-v0.32.0.tar.gz",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk", "go_register_toolchains", "go_rules_dependencies")

go_download_sdk(
name = "go_sdk",
version = "1.21.0",
)

go_rules_dependencies()

go_register_toolchains()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()
9 changes: 9 additions & 0 deletions examples/go/with_go_source/otherlib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

package(default_visibility = ["//:__subpackages__"])

go_library(
name = "otherlib",
srcs = ["otherlib.go"],
importpath = "github.com/bazelbuild/intellij/examples/go/with_go_source/otherlib",
)
3 changes: 3 additions & 0 deletions examples/go/with_go_source/otherlib/otherlib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package otherlib

func Foo() {}
42 changes: 42 additions & 0 deletions examples/go/with_go_source/testa/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_source")

package(default_visibility = ["//:__subpackages__"])

genrule(
name = "gen",
outs = ["gen.go"],
cmd = """
cat <<EOF > "$@"
package testa
import (
"github.com/bazelbuild/intellij/examples/go/with_go_source/otherlib"
)
func FromGeneratedFile() {
otherlib.Foo()
}
EOF
""",
)

go_source(
name = "srcs",
srcs = [
"src.go", # A Go source file for package testa
":gen", # A generated Go file for package testa
],
deps = ["//otherlib"], # A simple Go library dependency
)

#
# Test with a Go library embedding our `go_source` rule target.
#

go_library(
name = "testa",
srcs = ["testa.go"], # keep
embed = [":srcs"], # keep
importpath = "github.com/bazelbuild/intellij/examples/go/with_go_source/testa",
deps = [], # keep
)
9 changes: 9 additions & 0 deletions examples/go/with_go_source/testa/src.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testa

import (
"github.com/bazelbuild/intellij/examples/go/with_go_source/otherlib"
)

func FromSourceFile() {
otherlib.Foo()
}
6 changes: 6 additions & 0 deletions examples/go/with_go_source/testa/testa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package testa

func MyFunction() {
FromSourceFile()
FromGeneratedFile()
}
51 changes: 51 additions & 0 deletions examples/go/with_go_source/testb/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_source", "go_test")

package(default_visibility = ["//:__subpackages__"])

genrule(
name = "gen",
outs = ["gen.go"],
cmd = """
cat <<EOF > "$@"
package testb
import (
"github.com/bazelbuild/intellij/examples/go/with_go_source/otherlib"
)
func FromGeneratedFile() {
otherlib.Foo()
}
EOF
""",
)

go_source(
name = "srcs",
srcs = [
"src.go", # A Go source file for package testb
":gen", # A generated Go file for package testb
],
deps = ["//otherlib"], # A simple Go library dependency
)

#
# Test with a Go test embedding our `go_source` rule target +
# the default library of the test to infer the importpath.
#

go_library(
name = "testb",
srcs = ["testb.go"], # keep
importpath = "github.com/bazelbuild/intellij/examples/go/with_go_source/testb",
deps = [], # keep
)

go_test(
name = "testb_test",
srcs = ["testb_test.go"],
embed = [
":testb",
":srcs", # keep
],
)
9 changes: 9 additions & 0 deletions examples/go/with_go_source/testb/src.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testb

import (
"github.com/bazelbuild/intellij/examples/go/with_go_source/otherlib"
)

func FromSourceFile() {
otherlib.Foo()
}
3 changes: 3 additions & 0 deletions examples/go/with_go_source/testb/testb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package testb

func OtherFunction() {}
12 changes: 12 additions & 0 deletions examples/go/with_go_source/testb/testb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package testb

import (
"testing"
)

func TestB(t *testing.T) {
OtherFunction()

FromSourceFile()
FromGeneratedFile()
}
15 changes: 15 additions & 0 deletions examples/go/with_go_source/with_go_source.bazelproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
directories:
.

derive_targets_from_directories: true

additional_languages:
go

gazelle_target: //:gazelle

import_run_configurations:
# Test that validates that macro expansion works, as per issue:
# https://github.com/bazelbuild/intellij/issues/4112#event-7958662669
# This configuration should be executed on both 'run' (the green arrow) and 'debug' (the bug) modes.
run_configurations/test_macro_expansion.xml

0 comments on commit 4ffefc9

Please sign in to comment.