-
Notifications
You must be signed in to change notification settings - Fork 380
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: merge new Go naming convention to master #863
Conversation
Add directive 'go_naming_convention' to control the name of generated Go rules. go_default_library: Legacy behaviour. import: Name targets after their import path. import_alias: Same as import, but generate alias targets to maintain backwards compatibility with go_default_library. We also add a `build_naming_convention` attribute to `go_repository` rules, allowing per-external control over the naming convention. gazelle fix is augmented to migrate between the different styles, both forwards and backwards. For #5
* For external dependency resolution, if we don't know what naming convention the external repository uses (for example, because there's no known repository), we'll now use goDefaultLibraryNamingConvention. This avoids assuming that repositories with build files fetched with http_archive have been updated. * In migrateNamingConvention, print a warning and avoid renaming if there's already a target with the new name. * Library, test, and alias actuals are now generated based on import path instead of package name. * Added unknownNamingConvention, a new zero value. * Added detectNamingConvention. It reads the root build file and build files in subdirectories one level deep to infer the naming convention used if one is not specified in the root build file. Defaults to importNamingConvention. * Delete empty go_library rules in directories with go_binary. * Fixed tests. For #5
* Set naming convention in main build file. * Add gazelle:repository declarations in WORKSPACE for repos in gazelle_dependencies. * Run 'gazelle fix'. * Manual fixes for manually written targets. For #5
Checking |
I ran this on our depot and ran into the issue where I don't suppose there could be yet another directive to set the default if there is no info? |
Allows the user to set the default Go naming convention used when resolving imports of packages in external repositories with unknown naming conventions. For #5
My expectation is that we should be able to pull in this change without having to update our naming convention from After adding these flags I am still running into some issues:
The problem here appears to be that Gazelle and our repo share a dependency on Gazelle is also adding an unwanted
Finally, Gazelle is giving a warning for the following case with multiple
Warning: |
Thanks @blico, that feedback is really helpful. I'm curious why it was necessary to set I'll put together a PR soon with the following changes:
|
I was able to confirm we do not need to set |
* Rename goNamingConventionExtern to goNamingConventionExternal, together with the flag and directive. No need to abbrev. * Only migrate go_library if it matches the expected import path for the directory. This avoids migrating the wrong target when there are multiple go_libraries in the same directory. * Only migrate go_test if it embeds the library. * When fixing rules, don't add an embed attribute to go_test. For #5
Ok, the issues above are fixed, so I think this is basically ready to go. Let me know if you see any other problems. If not, I'll merge this tomorrow. |
Congrats, @jayconrod and @tomlu! This is a huge step foward for rules_go |
* Update Bazel build to work with 4.0.0 This includes several updates to the BUILD and WORKSPACE files, so that they now work with Bazel 4.0.0, Gazelle 0.23 and rules_go 0.27. By default, [Gazelle now uses](bazel-contrib/bazel-gazelle#863) the last component of the Bazel package, rather than `go_default_library` as the `go_library name`. This causes problems when the naming conventions do not match. bazel-contrib/bazel-gazelle#890 (comment) Adding support for the new naming convention should not break anything that depends on using the old naming convention, because the Gazelle option `import_alias` makes aliases to the libraries and this allows the old naming convention to still work.
* Update Bazel build to work with 4.0.0 This includes several updates to the BUILD and WORKSPACE files, so that they now work with Bazel 4.0.0, Gazelle 0.23 and rules_go 0.27. By default, [Gazelle now uses](bazel-contrib/bazel-gazelle#863) the last component of the Bazel package, rather than `go_default_library` as the `go_library name`. This causes problems when the naming conventions do not match. bazel-contrib/bazel-gazelle#890 (comment) Adding support for the new naming convention should not break anything that depends on using the old naming convention, because the Gazelle option `import_alias` makes aliases to the libraries and this allows the old naming convention to still work.
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?
//language/go
extension.import
mode, libraries and tests will be named based on the last component of their import paths.go_default_library
mode, libraries and tests will be namedgo_default_library
andgo_default_test
.import_alias
mode, targets are generated with theimport
mode, but aliases are generated with thego_default_library
mode. This should be used in repositories that other projects may depend on.go_repository
uses this mode by default.import
.fix
andupdate
.import_alias
naming convention.Credit to @tomlu for implementing most of this.
Which issues(s) does this PR fix?
Fixes #5
Other notes for review
cc @linzhp @blico: Please take a look and see if this will cause any issues for you. My expectation is that it should auto-detect the
go_default_library
mode, and then very little should change until you explicitly specify another mode.cc @tomlu: I think you've been using this branch for a while, but please confirm this final version works for you.