Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit a539917

Browse files
authored
Forbid empty sourceroot and go.mod path finder (#194)
* forbid sourceRoot empty with a / * If sourceRoot is empty try to detect go.mod path * Improve getGoModDir call * Update agent.go
1 parent 65f71f5 commit a539917

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

agent/agent.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,14 @@ func NewAgent(options ...Option) (*Agent, error) {
297297
cSRoot := sRoot.(string)
298298
cSRoot = filepath.Clean(cSRoot)
299299
if sRootEx, err := homedir.Expand(cSRoot); err == nil {
300-
sourceRoot = sRootEx
301-
agent.metadata[tags.SourceRoot] = sRootEx
300+
cSRoot = sRootEx
302301
}
302+
sourceRoot = cSRoot
303303
}
304+
if sourceRoot == "" {
305+
sourceRoot = getGoModDir()
306+
}
307+
agent.metadata[tags.SourceRoot] = sourceRoot
304308

305309
if !agent.testingMode {
306310
if env.ScopeTestingMode.IsSet {
@@ -349,6 +353,26 @@ func NewAgent(options ...Option) (*Agent, error) {
349353
return agent, nil
350354
}
351355

356+
func getGoModDir() string {
357+
dir, err := os.Getwd()
358+
if err != nil {
359+
return filepath.Dir("/")
360+
}
361+
for {
362+
rel, _ := filepath.Rel("/", dir)
363+
// Exit the loop once we reach the basePath.
364+
if rel == "." {
365+
return filepath.Dir("/")
366+
}
367+
modPath := fmt.Sprintf("%v/go.mod", dir)
368+
if _, err := os.Stat(modPath); err == nil {
369+
return dir
370+
}
371+
// Going up!
372+
dir += "/.."
373+
}
374+
}
375+
352376
func (a *Agent) setupLogging() error {
353377
filename := fmt.Sprintf("scope-go-%s-%s.log", time.Now().Format("20060102150405"), a.agentId)
354378
dir, err := getLogPath()

0 commit comments

Comments
 (0)