This repository was archived by the owner on Aug 17, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +26
-2
lines changed
Expand file tree Collapse file tree 1 file changed +26
-2
lines changed Original file line number Diff line number Diff 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+
352376func (a * Agent ) setupLogging () error {
353377 filename := fmt .Sprintf ("scope-go-%s-%s.log" , time .Now ().Format ("20060102150405" ), a .agentId )
354378 dir , err := getLogPath ()
You can’t perform that action at this time.
0 commit comments