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

Add -to_macro option to update-repos #480

Merged
merged 3 commits into from
Mar 27, 2019

Conversation

blico
Copy link
Contributor

@blico blico commented Mar 13, 2019

Addresses #301

The to_macro option tells Gazelle to write repository rules into a .bzl macro function rather than the WORKSPACE file. The user can then load the macro into their WORKSPACE and call it from there. This helps with the maintainability of the WORKSPACE file, especially for projects with many go_repository rules.

The format of the option is -to_macro=macroFile%defName

Copy link
Contributor

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

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

Thanks for working on this. This is really cool.

I have a few requests. Primarily, I think we should be able to generate a macro file from scratch.

@@ -409,6 +413,10 @@ The following flags are accepted:
| |
| Gazelle will not process packages outside this directory. |
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
| :flag:`-to_macro macroFile%defName` | |
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
| Tells Gazelle to write repository rules into a .bzl macro function rather than the WORKSPACE file. |
Copy link
Contributor

Choose a reason for hiding this comment

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

It should probably be pointed out here that Gazelle won't see repositories declared in the macro when updating build files. That means that it won't use custom repository names, and it will go out to the network when trying to determine repository roots.

This is a solvable problem of course, but let's leave it for another time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, this was an oversight on my part. We are pretty dependent on the functionality you mentioned, so I would like to get this done sooner than later.

From a quick glance, it looks like one way to get this done would be to add a new flag named something like -go_repositories_macro=path. If the flag is set we call LoadMacroFile from newFixUpdateConfiguration. We then call FixWorkspace and FixLoads for both the macro and workspace files, and also pass both files to repo.ListRepositories.

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

#132 describes a # gazelle:repository directive that would declare this information in WORKSPACE without requiring command line arguments. That would be a useful escape hatch. I just opened #483, describing a more complete solution. In WORKSPACE, we could have a directive that tells Gazelle to read a macro in a separate .bzl file. Bonus points if we can update existing rules in that macro without needing the -to_macro flag.

Until those are implemented, we should mention this problem here, since the problem won't be obvious to people trying this out.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can simplify this problem a bit to start with. We can have a default bzl file like //go_repositories.bzl unless it's overridden by some the Gazelle directive proposed in #483. Then --to_macro becomes a boolean flag, and write go_repository rules to //go_repositories.bzl when the flag is set. update and fix will first check if there is a //go_repositories.bzl. If so, read those rules first. Then we should allow people to override rules in //go_repositories.bzl in WORKSPACE files. For example, if they want to apply a patch to some go_repository, they can redefine the rule with the same name in WORKSPACE. Then //go_repositories.bzl contains no manual edits.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can imagine large repositories needing multiple macros for different sets of dependencies. When language extensions eventually support update-repos functionality, it should be possible to update macros for non-Go dependencies, too. Basically, I want to make sure we stay flexible here.

rule/rule.go Outdated Show resolved Hide resolved
rule/rule.go Outdated Show resolved Hide resolved
rule/rule.go Outdated Show resolved Hide resolved
rule/rule.go Show resolved Hide resolved
cmd/gazelle/update-repos.go Outdated Show resolved Hide resolved
cmd/gazelle/update-repos.go Outdated Show resolved Hide resolved
load("@bazel_gazelle//:deps.bzl", "go_repository")

def go_repositories():
go_repository(name = "org_golang_x_net", build_file_generation = "off", commit = "66aacef3dd8a676686c7ae3716979581e8b03c47", importpath = "golang.org/x/net")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the output is pretty difficult to read with all of the attributes on one line. In Rule.sync, could you set the ForceMultiLine flag on rules with more than one attribute?

Copy link
Contributor Author

@blico blico Mar 15, 2019

Choose a reason for hiding this comment

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

It looks like it is not currently possible to print out the attributes on more than one line :/. I created a PR to fix this issue here: bazelbuild/buildtools#580

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for looking into this. I saw you set the ForceMultiline flag. So that's getting ignored? Hmm, that's unfortunate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change has been merged into buildtools. So we will need to update buildtools within Gazelle sometime soon.

@@ -409,6 +413,10 @@ The following flags are accepted:
| |
| Gazelle will not process packages outside this directory. |
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
| :flag:`-to_macro macroFile%defName` | |
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
| Tells Gazelle to write repository rules into a .bzl macro function rather than the WORKSPACE file. |
Copy link
Contributor

Choose a reason for hiding this comment

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

#132 describes a # gazelle:repository directive that would declare this information in WORKSPACE without requiring command line arguments. That would be a useful escape hatch. I just opened #483, describing a more complete solution. In WORKSPACE, we could have a directive that tells Gazelle to read a macro in a separate .bzl file. Bonus points if we can update existing rules in that macro without needing the -to_macro flag.

Until those are implemented, we should mention this problem here, since the problem won't be obvious to people trying this out.

rule/rule.go Show resolved Hide resolved
rule/rule.go Outdated Show resolved Hide resolved
rule/rule.go Outdated Show resolved Hide resolved
rule/rule.go Outdated Show resolved Hide resolved
load("@bazel_gazelle//:deps.bzl", "go_repository")

def go_repositories():
go_repository(name = "org_golang_x_net", build_file_generation = "off", commit = "66aacef3dd8a676686c7ae3716979581e8b03c47", importpath = "golang.org/x/net")
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for looking into this. I saw you set the ForceMultiline flag. So that's getting ignored? Hmm, that's unfortunate.

@blico
Copy link
Contributor Author

blico commented Mar 25, 2019

Sorry for the delay, I was out sick most of last week.

Copy link
Contributor

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

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

Nice! I tried this out, and it seems to work pretty well. Thanks again for working on this.

I'll merge this and update buildtools on master. It doesn't look like we'll need any other change to get multi-line printing.

@@ -409,6 +413,10 @@ The following flags are accepted:
| |
| Gazelle will not process packages outside this directory. |
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
| :flag:`-to_macro macroFile%defName` | |
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
| Tells Gazelle to write repository rules into a .bzl macro function rather than the WORKSPACE file. |
Copy link
Contributor

Choose a reason for hiding this comment

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

I can imagine large repositories needing multiple macros for different sets of dependencies. When language extensions eventually support update-repos functionality, it should be possible to update macros for non-Go dependencies, too. Basically, I want to make sure we stay flexible here.

@jayconrod jayconrod merged commit 44bce61 into bazel-contrib:master Mar 27, 2019
@jayconrod
Copy link
Contributor

It looks like there have been some breaking changes in buildtools since we last updated, and updating to the latest version will be non-trivial. I don't have time to do it right now, maybe this weekend. Filed #489 to track the update.

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.

3 participants