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

Remove potential Go module versions from shortened names #571

Merged
merged 6 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion internal/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
// Removes package name and method arugments for Go function names.
// See tests for examples.
goRegExp = regexp.MustCompile(`^(?:[\w\-\.]+\/)+(.+)`)
// Checks for a package name that could be a module version.
goVerRegExp = regexp.MustCompile(`^v([2-9]|[1-9][0-9]+)\.`)
// Strips C++ namespace prefix from a C++ function / method name.
// NOTE: Make sure to keep the template parameters in the name. Normally,
// template parameters are stripped from the C++ names but when
Expand Down Expand Up @@ -442,12 +444,32 @@ func ShortenFunctionName(f string) string {
f = cppAnonymousPrefixRegExp.ReplaceAllString(f, "")
for _, re := range []*regexp.Regexp{goRegExp, javaRegExp, cppRegExp} {
if matches := re.FindStringSubmatch(f); len(matches) >= 2 {
return strings.Join(matches[1:], "")
name := strings.Join(matches[1:], "")
if re == goRegExp {
return shortenGoFunc(f, name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel it might be simpler to first remove the version substring from the name, and then handle it just like before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if you saw the previous review comments, but if preferred this all can be removed and replaced with a single regex change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel it might be simpler to first remove the version substring from the name, and then handle it just like before.

I'm not sure how this is possible; the name here is extracted from the regex directly. If we remove the version suffix, you get the empty string.

Copy link
Contributor Author

@zikaeroh zikaeroh Oct 16, 2020

Choose a reason for hiding this comment

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

Oh, I see, you mean that if the name matches a version, remove the suffix from the whole path and then try again. It wouldn't distinguish two versions of the same module, but I guess it's no worse than any other name aliasing within the same graph. Would be short; I can do that if preferred.

Copy link
Contributor Author

@zikaeroh zikaeroh Oct 16, 2020

Choose a reason for hiding this comment

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

I think I misinterpreted again (sorry!), so I'll wait for clarification.

I think you meant just starting with something like github.com/jackc/pgx/v4/foo.bar, then replacing the first instance of /v4/ with /, then running the regex again. Not quite a suffix, but functional enough. This is all heuristics after all.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, /v[1-9][0-9]*[./].

Copy link
Collaborator

Choose a reason for hiding this comment

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

And not all occurrences but at most one occurrence (assuming there can't be two version substrings in the name).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All instances wouldn't work, because it's legal for me to write github.com/foo/bar/v4/something/v8 or similar. First instance I believe would work as intended, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll give it a try. Would be v([2-9]|[1-9][0-9]+)\., though, as v0 and v1 don't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, and retitled to match the new fix. regexp doesn't have a nice "just replace once", so I used the replace all with two capture groups method.

}
return name
}
}
return f
}

func shortenGoFunc(f string, name string) string {
if !goVerRegExp.MatchString(name) {
return name
}

// The shortened name could start with a module version (like "v2"). Go back one slash.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Keep comments in 80 columns please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

end := len(f) - len(name) - 1
if end >= 0 {
prefix := f[:end]
if idx := strings.LastIndex(prefix, "/"); idx >= 0 {
end = idx
}
}
return f[end+1:]
}

// TrimTree trims a Graph in forest form, keeping only the nodes in kept. This
// will not work correctly if even a single node has multiple parents.
func (g *Graph) TrimTree(kept NodePtrSet) {
Expand Down
24 changes: 24 additions & 0 deletions internal/graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,30 @@ func TestShortenFunctionName(t *testing.T) {
"github.com/blah/blah/vendor/gopkg.in/redis.v3.(*baseClient).(github.com/blah/blah/vendor/gopkg.in/redis.v3.process)-fm",
"redis.v3.(*baseClient).(github.com/blah/blah/vendor/gopkg.in/redis.v3.process)-fm",
},
{
"github.com/foo/bar/v4.(*Foo).Bar",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious - why is version string sometimes a separate subdirectory and sometimes a prefix of the package name? Is this something that the package owners choose? Are these options restricted at these two, or are there more?

Oh, I guess it's a function of how deep below the versioning level the actual symbol is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you're referring to the tests; some of the tests I've added are where the "version" isn't a version at all. The only valid version in paths are "v2", "v3", ... "v1234", etc. So you'd have github.com/foo/bar, github.com/foo/bar/v2, github.com/foo/bar/v3, and so on, then subpackages like github.com/foo/bar/v3/baz. Custom domains mean you can have things like gotest.tools/assert, gotest.tools/v3/assert. But a package can be at the level where the version appears, so when github.com/jackc/pgx was bumped to github.com/jackc/pgx/v4, it's still referred to as pgx in the code.

But if it isn't a valid version part, then I don't want to treat it as one naively (i.e. "something.com/hello/v123xyz" isn't versioned, "something.com/hello/v123/xyz" is because the version is its own element).

"bar/v4.(*Foo).Bar",
},
{
"github.com/foo/bar/v4/baz.Foo.Bar",
"baz.Foo.Bar",
},
{
"github.com/foo/bar/v123.(*Foo).Bar",
"bar/v123.(*Foo).Bar",
},
{
"github.com/foobar/v0.(*Foo).Bar",
"v0.(*Foo).Bar",
},
{
"github.com/foobar/v1.(*Foo).Bar",
"v1.(*Foo).Bar",
},
{
"example.org/v2xyz.Foo",
"v2xyz.Foo",
},
{
"java.util.concurrent.ThreadPoolExecutor$Worker.run",
"ThreadPoolExecutor$Worker.run",
Expand Down