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

Determine the caller pkg path using Go's native package logic #117

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

Conversation

tabboud
Copy link
Contributor

@tabboud tabboud commented Jan 14, 2021

Before this PR

Inferring the caller pkg path from the filename breaks when repos are not in the GOPATH, such as those that use go modules. See #116 for more information.

After this PR

==COMMIT_MSG==
Use Go's native package logic to determine the caller pkg path and use the GOPATH trimming heuristic as a fallback.
Fixes #116
==COMMIT_MSG==

Possible downsides?

  • There are some side affects to using the go package path with modules since the module version will be included github.com/tabboud/test-repo/v2

This change is Reviewable

@changelog-app
Copy link

changelog-app bot commented Jan 14, 2021

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Use Go's native package logic to determine the caller pkg path and use the GOPATH trimming heuristic as a fallback.
Fixes #116

Check the box to generate changelog(s)

  • Generate changelog entry

}
return file, line, ok
cfg := &packages.Config{Mode: packages.NeedModule | packages.NeedName}
pkgs, err := packages.Load(cfg, "file="+file)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See the docs for packages to see where this file=... pattern comes from
https://pkg.go.dev/golang.org/x/tools/go/packages

@tabboud
Copy link
Contributor Author

tabboud commented Jan 14, 2021

I am still testing this out to make sure this is what we want, but opening this up to get some directional feedback. I have noticed that using the resolved package path results in the module version being appending onto the path as well (which is not in the on-disk file path), and may not be ideal for our log filtering by origin.

For example, the following is produced for the following test-repo at v2 module

File: "/Volumes/git/go/src/github.com/tabboud/test-repo/server/server.go"
PkgPath: "github.com/tabboud/test-repo/v2/server"
ModPath: "github.com/tabboud/test-repo/v2"

CallerPkg(0, 1): "github.com/tabboud/test-repo/v2"

Whereas for v1 the following would be produced:

File: "/Volumes/git/go/src/github.com/tabboud/test-repo/server/server.go"
PkgPath: "github.com/tabboud/test-repo/server"
ModPath: "github.com/tabboud/test-repo"

CallerPkg(0, 1): "github.com/tabboud/test-repo"

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.

svc1log caller pkg functions are not module aware
1 participant