diff --git a/examples/go/with_proto/MODULE.bazel b/examples/go/with_proto/MODULE.bazel new file mode 100644 index 00000000000..00bb18361f7 --- /dev/null +++ b/examples/go/with_proto/MODULE.bazel @@ -0,0 +1,6 @@ +############################################################################### +# Bazel now uses Bzlmod by default to manage external dependencies. +# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. +# +# For more details, please check https://github.com/bazelbuild/bazel/issues/18958 +############################################################################### diff --git a/examples/go/with_proto/go/external/BUILD.bazel b/examples/go/with_proto/go/external/BUILD.bazel new file mode 100644 index 00000000000..fffc6833c5a --- /dev/null +++ b/examples/go/with_proto/go/external/BUILD.bazel @@ -0,0 +1,8 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "external", + srcs = ["external.go"], + importpath = "github.com/bazelbuild/intellij/examples/go/with_proto/go/external", + visibility = ["//visibility:public"], +) diff --git a/examples/go/with_proto/go/external/README.md b/examples/go/with_proto/go/external/README.md new file mode 100644 index 00000000000..c9fe36b730a --- /dev/null +++ b/examples/go/with_proto/go/external/README.md @@ -0,0 +1,4 @@ +# Packages named `external` + +This directory exists to make sure that packages named `external` are imported correctly, as per https://github.com/bazelbuild/intellij/issues/6324. +Targets depending on this package (such as `//lib:lib.go`) should resolve without red squigglies. \ No newline at end of file diff --git a/examples/go/with_proto/go/external/external.go b/examples/go/with_proto/go/external/external.go new file mode 100644 index 00000000000..5f56b5d96ac --- /dev/null +++ b/examples/go/with_proto/go/external/external.go @@ -0,0 +1,5 @@ +package external + +func ExternalCats() []string { + return []string{"Ember", "Cinder"} +} diff --git a/examples/go/with_proto/go/main.go b/examples/go/with_proto/go/main.go index 9b14e051a3f..38801536920 100644 --- a/examples/go/with_proto/go/main.go +++ b/examples/go/with_proto/go/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/bazelbuild/intellij/examples/go/with_proto/go/external" "github.com/bazelbuild/intellij/examples/go/with_proto/go/lib" "github.com/bazelbuild/intellij/examples/go/with_proto/proto" "google.golang.org/grpc" @@ -12,7 +13,7 @@ func main() { fmt.Printf("AddToTwo(%d) = %d", num, lib.AddToTwo(num)) } -// This function exists to check that: +// serv exists to check that: // - Third party symbols (`grpc.Server`) resolve. // - Code generated from protobuf resolves (symbols in the `proto` package). // - We should be able to "go to definition" in `proto.FooServiceServer`. @@ -22,3 +23,10 @@ func serv(grpcSrv *grpc.Server, protoSrv *proto.FooServiceServer) { req := proto.HelloRequest{Name: &name} req.GetName() } + +// CountExternalCats function exists to check that packages that are named `external` resolve: +// +// We should be able to "go to definition" in `external.ExternalCats()`. +func CountExternalCats() int { + return len(external.ExternalCats()) +} diff --git a/examples/go/with_proto/go/main_test.go b/examples/go/with_proto/go/main_test.go index bcd5225425a..c2de1288c82 100644 --- a/examples/go/with_proto/go/main_test.go +++ b/examples/go/with_proto/go/main_test.go @@ -54,3 +54,24 @@ func TestEnvVars(t *testing.T) { } } } + +func TestCountExternalCats(t *testing.T) { + testCases := []struct { + name string + expectedCount int + }{ + { + name: "Cats", + expectedCount: 2, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + count := CountExternalCats() + if count != tc.expectedCount { + t.Errorf("Expected to have %d cats, got %d cats", tc.expectedCount, count) + } + }) + } + +} diff --git a/golang/src/com/google/idea/blaze/golang/resolve/BlazeGoPackage.java b/golang/src/com/google/idea/blaze/golang/resolve/BlazeGoPackage.java index 5cb1bb09c19..2388ae4c7ef 100644 --- a/golang/src/com/google/idea/blaze/golang/resolve/BlazeGoPackage.java +++ b/golang/src/com/google/idea/blaze/golang/resolve/BlazeGoPackage.java @@ -185,7 +185,7 @@ private static File toRealFile(@Nullable File maybeExternal) { if (externalString.contains("/external/") && !externalString.contains("/bazel-out/") && !externalString.contains("/blaze-out/")) { - return new File(externalString.replaceAll("/execroot.*/external/", "/external/")); + return new File(externalString.replaceAll("/execroot.*?/external/", "/external/")); } return maybeExternal; }