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

Allow some way to prioritise one file over another #237

Closed
Naatan opened this issue Jun 21, 2018 · 3 comments · Fixed by #1700
Closed

Allow some way to prioritise one file over another #237

Naatan opened this issue Jun 21, 2018 · 3 comments · Fixed by #1700
Labels

Comments

@Naatan
Copy link

Naatan commented Jun 21, 2018

The # keep option is great, but unfortunately it does not have any effect on the order of the item. It seems gazelle will reorder srcs values each time it is called. Problem is execution order sometimes matters in Go. I need a way to say "this file should be the first entry". At the moment the only option I see is to exclude the relevant directory from gazelle and manually define my rule.

Example:

    srcs = [
        "locale-packr.go",  # keep
        "locale.go",
    ],

After I run gazelle the result is:

    srcs = [
        "locale.go",
        "locale-packr.go", # keep
    ],

I need locale-packr.go to be first.

@jayconrod
Copy link
Contributor

I think this adds complexity without adding much value. I'd rather not implement it if the problem can be solved another way.

Are you able to build this with go build or go get? I think the Go toolchain compiles files in sorted order unless you are compiling a package with all source files specified on the command line.

What do you think about renaming files so the sorted order is the correct order? Something like aa-locale-packr.go?

If all else fails, you can mark the whole srcs attribute with a # keep comment above. Gazelle won't add, remove, or sort srcs, but it will still update deps and other attributes.

@Naatan
Copy link
Author

Naatan commented Jun 21, 2018

This does indeed work with go build, I ran into it when converting a build to bazel. Note you can easily reproduce it just by using packr. The filename is decided by packr and is in the format of <package>-packr.go.

I think perhaps this isnt so much an enhancement as much as it is a bug in that gazelle does not appear to apply the same load order to files that go build does.

@jayconrod jayconrod added bug and removed enhancement labels Jun 22, 2018
@jayconrod
Copy link
Contributor

You're right, this is actually a bug.

Gazelle uses buildifier internally to format build files before writing them out. One of the transformations buildifier applies is sorting string lists. The sorting algorithm is intended to work with labels as well as source files, so it's not strictly lexicographic.

Buildifier has some functionality that lets you control this. Groups of strings in a list with an intervening blank line are sorted separately. And you can write "# do not sort" above a group to prevent it from being sorted. Not sure if that's documented anywhere, but the code is in rewrite.go.

Unfortunately, when Gazelle uses buildifier through its library, we don't get that behavior. I'm not exactly sure why yet, but it is a bug.

t-8ch pushed a commit to t-8ch/bazel-gazelle that referenced this issue Dec 27, 2023
This allows user to attach comments like "# do not sort" to the
assignment expressions maintained by rule.go.

rule.Format() calls into buildtools/build.Format() which in turn calls
Rewrite(). Rewrite() by default sorts certain label arguments like
"deps" and "hdrs". To preventing this sorting it is possible to provide
the comment "# do not sort". However this comment needs to be attached
to the assignment expression.
In rule.go the assignment expressions are not exposed to the user,
making it impossible to attach the comment to it.

See bazelbuild#237 (comment)
t-8ch added a commit to t-8ch/bazel-gazelle that referenced this issue Dec 27, 2023
This allows user to attach comments like "# do not sort" to the
assignment expressions maintained by rule.go.

rule.Format() calls into buildtools/build.Format() which in turn calls
Rewrite(). Rewrite() by default sorts certain label arguments like
"deps" and "hdrs". To preventing this sorting it is possible to provide
the comment "# do not sort". However this comment needs to be attached
to the assignment expression.
In rule.go the assignment expressions are not exposed to the user,
making it impossible to attach the comment to it.

See bazelbuild#237 (comment)
fmeum pushed a commit to t-8ch/bazel-gazelle that referenced this issue Dec 27, 2023
This allows user to attach comments like "# do not sort" to the
assignment expressions maintained by rule.go.

rule.Format() calls into buildtools/build.Format() which in turn calls
Rewrite(). Rewrite() by default sorts certain label arguments like
"deps" and "hdrs". To preventing this sorting it is possible to provide
the comment "# do not sort". However this comment needs to be attached
to the assignment expression.
In rule.go the assignment expressions are not exposed to the user,
making it impossible to attach the comment to it.

See bazelbuild#237 (comment)
t-8ch added a commit to t-8ch/bazel-gazelle that referenced this issue Dec 28, 2023
This allows user to attach comments like "# do not sort" to the
assignment expressions maintained by rule.go.

rule.Format() calls into buildtools/build.Format() which in turn calls
Rewrite(). Rewrite() by default sorts certain label arguments like
"deps" and "hdrs". To preventing this sorting it is possible to provide
the comment "# do not sort". However this comment needs to be attached
to the assignment expression.
In rule.go the assignment expressions are not exposed to the user,
making it impossible to attach the comment to it.

See bazelbuild#237 (comment)
fmeum pushed a commit that referenced this issue Dec 28, 2023
This allows user to attach comments like "# do not sort" to the
assignment expressions maintained by rule.go.

rule.Format() calls into buildtools/build.Format() which in turn calls
Rewrite(). Rewrite() by default sorts certain label arguments like
"deps" and "hdrs". To preventing this sorting it is possible to provide
the comment "# do not sort". However this comment needs to be attached
to the assignment expression.
In rule.go the assignment expressions are not exposed to the user,
making it impossible to attach the comment to it.

See #237 (comment)
jeromep-stripe pushed a commit to jeromep-stripe/bazel-gazelle that referenced this issue Mar 22, 2024
This allows user to attach comments like "# do not sort" to the
assignment expressions maintained by rule.go.

rule.Format() calls into buildtools/build.Format() which in turn calls
Rewrite(). Rewrite() by default sorts certain label arguments like
"deps" and "hdrs". To preventing this sorting it is possible to provide
the comment "# do not sort". However this comment needs to be attached
to the assignment expression.
In rule.go the assignment expressions are not exposed to the user,
making it impossible to attach the comment to it.

See bazelbuild#237 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants