A tiny Go library that resolves the root directory of a project by walking up
from the current working directory until it finds a go.mod.
This is useful whenever code needs to locate files relative to the repository
root rather than relative to the process working directory, for example test
fixtures, configuration files or templates. Tests run with the working
directory set to the package under test, so a hard coded relative path like
../../testdata breaks as soon as a package is moved.
go get github.com/schubergphilis/mcvs-golang-project-rootpackage main
import (
"github.com/schubergphilis/mcvs-golang-project-root/pkg/projectroot"
log "github.com/sirupsen/logrus"
)
func main() {
projectRoot, err := projectroot.FindProjectRoot()
if err != nil {
log.WithError(err).Fatal("unable to find project root")
}
log.Infof("project root found at: %s", projectRoot)
}A runnable example is located in examples. To run it, issue:
go run examples/main.goResolving a path relative to the root:
root, err := projectroot.FindProjectRoot()
if err != nil {
return err
}
fixture := filepath.Join(root, "testdata", "input.json")Returns the absolute path of the first directory, starting at the current
working directory and walking up through its parents, that contains a go.mod
file. It returns an error if the working directory cannot be determined, or the
error project root not found if the filesystem root is reached without
encountering a go.mod.
Note that in a repository with nested modules the nearest enclosing module directory is returned, not the top level of the repository.
This project uses Task with the shared build tasks from mcvs-golang-action. The version of those remote tasks is pinned in Taskfile.yml and kept in sync with the version used by the GitHub Actions workflow.
export TASK_X_REMOTE_TASKFILES=1
task --list-allSome common tasks:
task remote:format
task remote:lint
task remote:test
task remote:coverageThe same checks run in CI via .github/workflows/golang.yml,
which executes the unit, component and integration test suites, linting and the
security scans (golang-modules, grype and trivy).
