Skip to content

fix(gopackagesdriver): Improve wildcard package query matching #4288

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Aaronkala
Copy link

What type of PR is this?

Bug fix

What does this PR do? Why is it needed?

Improve wildcard package loading (./something/...) when using a custom bazelQueryScope e.g. //.... Currently, the queryFromRequests transformation results in a query like:

bazel query "kind(\"^(go_library) rule$\", attr(importpath, \"^./something(/.+)?$\", deps(//...)))"

which fails to return any results due to the ^ start of string matching. This PR also aligns the behaviour more closely to the "non wild card" behaviour (see) by omitting the ^.

When using go outside bazel, the following query is valid syntax and correctly loads all the packages under ./something/:

package.Load(cfg, "./something/...")

Which issues(s) does this PR fix?

No issue

Other notes for review

@@ -116,7 +116,7 @@ func (b *BazelJSONBuilder) adjustToRelativePathIfPossible(request string) string

func (b *BazelJSONBuilder) packageQuery(importPath string) string {
if strings.HasSuffix(importPath, "/...") {
importPath = fmt.Sprintf(`^%s(/.+)?$`, strings.TrimSuffix(importPath, "/..."))
importPath = fmt.Sprintf(`%s(/.+)?$`, strings.TrimSuffix(importPath, "/..."))
Copy link
Author

Choose a reason for hiding this comment

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

The pattern supplied being treated as a regular expression can result in different behaviour if the input happens to contain regexp special characters. Leaving it as is here

Copy link
Member

Choose a reason for hiding this comment

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

If we change this, let's fix that as well. Could you wrap the import path into regexp.QuoteMeta?

Copy link
Author

Choose a reason for hiding this comment

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

I pushed a commit that escapes both the wild card case and the default case: 23be4f0

But this essentially means that the ./something query won't work at least against the path that we are seeing //something as \./something won't match //something. I'm unsure if this is correct, and/or if the //something paths are only specific to our implementation.

Copy link
Author

@Aaronkala Aaronkala Mar 7, 2025

Choose a reason for hiding this comment

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

Okay the importpaths it's actually matching against are in the form of github.com/orgname/reponame/something/foo/bar as defined in go_library() importpath

I'll test if importpath_aliases aliases fixes this for us with something like ./something/foo/bar as the alias

Copy link
Author

Choose a reason for hiding this comment

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

Hmm but the query is targetting only importpath 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for getting back to this so late, where are we at? Should I take another look?

@fmeum
Copy link
Member

fmeum commented Mar 6, 2025

What does the actual kind look like in that case that it doesn't match the start of string?

@Aaronkala
Copy link
Author

Aaronkala commented Mar 6, 2025

@fmeum all the returned results without "^" start with // for example:

//something/foo/bar/foobar:baz
//something/foo:bar
//something/bar/foo:baz

I'm not sure if this is what you mean by "kind"

@fmeum
Copy link
Member

fmeum commented Mar 6, 2025

Thanks, I thought this also affected the usage of ^ in the kind part of the query, but it doesn't.

@Aaronkala Aaronkala requested a review from fmeum March 6, 2025 19:26
@fmeum fmeum enabled auto-merge (squash) March 6, 2025 22:37
@fmeum fmeum disabled auto-merge March 6, 2025 22:37
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.

2 participants