Skip to content

Conversation

@philwitty
Copy link

  • as the glob special characters are not valid package names this should not cause compile errors for any matches of existing modules

@philwitty philwitty force-pushed the allow-globs-for-pkgs branch from bd6660c to 1af7692 Compare October 5, 2023 11:22
* as the glob special characters are not valid package names this should not cause compile errors for any matches of existing modules
@philwitty philwitty force-pushed the allow-globs-for-pkgs branch from 1af7692 to 6199f61 Compare October 5, 2023 11:30
Copy link
Member

@dixonwille dixonwille left a comment

Choose a reason for hiding this comment

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

I am contemplating if this matters. But there was a performance benefit to only allowing prefixing instead of globs. Globs require to go through the array from the start to the end (the sorting that was done is no longer needed and maybe not expected).

Doing string comparison in comparison is cheaper and I can do a binary search (sort.Search) requiring potentially even fewer comparisons against what could be pretty lengthy slice (think mono repository with many packages).

There are significantly fewer files than imports so was willing to take the hit of globs there. Plus, globing against a well structured package (one controlled by the maintainers of the linter configuration) makes logical sense. But not every go project is built the same. So globing at package level seemed a bit like false security (don't include any package with the name test?). Prefixing made the most sense as I may not trust X organization and I can just exclude it with a prefix...

t.Run(c.name, func(t *testing.T) {
set, err := c.configurator.parse(c.inputFile)
if err != nil {
t.Fatalf("file is not a valid json file: %s", err)
Copy link
Member

Choose a reason for hiding this comment

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

File may not be json.

if err != nil {
errs = append(errs, err)
}
sort.Strings(l.Allow)
Copy link
Member

Choose a reason for hiding this comment

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

This modifies the input slice which can be unexpected behavior to the consumer (if there are any). That is why I had it copied to an internal slice before sort.

@philwitty
Copy link
Author

I am contemplating if this matters. But there was a performance benefit to only allowing prefixing instead of globs. Globs require to go through the array from the start to the end (the sorting that was done is no longer needed and maybe not expected).

Doing string comparison in comparison is cheaper and I can do a binary search (sort.Search) requiring potentially even fewer comparisons against what could be pretty lengthy slice (think mono repository with many packages).

There are significantly fewer files than imports so was willing to take the hit of globs there. Plus, globing against a well structured package (one controlled by the maintainers of the linter configuration) makes logical sense. But not every go project is built the same. So globing at package level seemed a bit like false security (don't include any package with the name test?). Prefixing made the most sense as I may not trust X organization and I can just exclude it with a prefix...

Really good points, we certainly could optimise the globbing if performance is an issue (i.e. using a custom globber which given the well defined package names that sounds reasonable), as there isn't any cleverness in the current matching this shouldn't be a noticeable performance decrease I think.

Not sure I understand your second point about the use, I should have made my use case clear in the PR, we wanted to have a shared linter config across many repos but ensure none of them were using any our-organisation/{repo}/test/ packages in non test code

@ezk84
Copy link

ezk84 commented Nov 12, 2025

This makes sense for a use case I have, where I want to enforce dependency rules in a layered application implementing Clean Architecture:

contexts/
   context-a/
     internal/
       adapters/
       app/
       domain/
   context-b/
     internal/
       adapters/
       app/
       domain/

One such rule might say:

  • inside each context, app must not depend on adapters or any subpackage

prefix-only matching would force us to update the config for any new context we add to the application, whereas with package globs this would be simple:

prevent_app_deps_adapters:
  files: 
    - <my-repo>/contexts/**/internal/app/**/*.go
  deny:
    - pkg: <my-repo>/contexts/**/internal/adapters
      desc: Application layer can't depend on adapters

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