Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.
Merged
Changes from all 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
28 changes: 26 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,14 @@ func NewAgent(options ...Option) (*Agent, error) {
cSRoot := sRoot.(string)
cSRoot = filepath.Clean(cSRoot)
if sRootEx, err := homedir.Expand(cSRoot); err == nil {
sourceRoot = sRootEx
agent.metadata[tags.SourceRoot] = sRootEx
cSRoot = sRootEx
}
sourceRoot = cSRoot
}
if sourceRoot == "" {
sourceRoot = getGoModDir()
}
agent.metadata[tags.SourceRoot] = sourceRoot

if !agent.testingMode {
if env.ScopeTestingMode.IsSet {
Expand Down Expand Up @@ -349,6 +353,26 @@ func NewAgent(options ...Option) (*Agent, error) {
return agent, nil
}

func getGoModDir() string {
dir, err := os.Getwd()
if err != nil {
return filepath.Dir("/")
}
for {
rel, _ := filepath.Rel("/", dir)
// Exit the loop once we reach the basePath.
if rel == "." {
return filepath.Dir("/")
}
modPath := fmt.Sprintf("%v/go.mod", dir)
if _, err := os.Stat(modPath); err == nil {
return dir
}
// Going up!
dir += "/.."
}
}

func (a *Agent) setupLogging() error {
filename := fmt.Sprintf("scope-go-%s-%s.log", time.Now().Format("20060102150405"), a.agentId)
dir, err := getLogPath()
Expand Down