@@ -40,26 +40,48 @@ func (s *NixCandidateSource) WhereSet(somePath string) *PathSetIn {
4040}
4141
4242func (s * NixCandidateSource ) crawlKnownPaths () {
43- ForEachKnownPath (func (originalSource , expandedSource string ) {
44- if s .expandedPathsAlreadyCrawled [expandedSource ] {
45- return
43+ ForEachKnownPath (s .crawlSource )
44+ }
45+
46+ func (s * NixCandidateSource ) crawlSource (originalSource , expandedSource string ) {
47+ if s .expandedPathsAlreadyCrawled [expandedSource ] || isDir (expandedSource ) {
48+ return
49+ }
50+ // do not crawl this file again
51+ s .expandedPathsAlreadyCrawled [expandedSource ] = true
52+
53+ // try getting the contents
54+ input , err := readAllText (expandedSource )
55+ if err != nil {
56+ return
57+ }
58+
59+ ForEachVariableAssignment (s .key , input , func (value string ) {
60+ harvestedPaths := s .harvestPaths (value )
61+ for _ , harvestedPath := range harvestedPaths {
62+ s .tryUpdatePathMap (harvestedPath , originalSource , expandedSource )
4663 }
47- // do not crawl this file again
48- s .expandedPathsAlreadyCrawled [expandedSource ] = true
64+ })
65+
66+ sourcedQueue := []string {}
4967
50- // try getting the contents
51- input , err := readAllText ( expandedSource )
52- if err != nil {
68+ ForEachSourcedScript ( input , func ( sourcedPath string ) {
69+ normalizedSourcedPath := s . fs . GetAbsolutePath ( sourcedPath )
70+ if s . expandedPathsAlreadyCrawled [ normalizedSourcedPath ] {
5371 return
5472 }
55-
56- ForEachVariableAssignment (s .key , input , func (value string ) {
57- harvestedPaths := s .harvestPaths (value )
58- for _ , harvestedPath := range harvestedPaths {
59- s .tryUpdatePathMap (harvestedPath , originalSource , expandedSource )
60- }
61- })
73+ sourcedQueue = append (sourcedQueue , sourcedPath )
6274 })
75+
76+ for {
77+ if len (sourcedQueue ) == 0 {
78+ break
79+ }
80+ next := sourcedQueue [0 ]
81+ sourcedQueue = sourcedQueue [1 :]
82+ s .crawlSource (next , s .fs .GetAbsolutePath (next ))
83+ }
84+
6385}
6486
6587func (s * NixCandidateSource ) tryUpdatePathMap (harvestedPath string , originalSource string , expandedSource string ) {
@@ -93,14 +115,17 @@ func (s *NixCandidateSource) crawlPathLists() {
93115
94116// input is some path definition
95117func (s * NixCandidateSource ) harvestPaths (input string ) []string {
118+ input = fixHomeExpansion (input )
96119 input = strings .TrimSpace (input )
97120 input = strings .Trim (input , fmt .Sprintf ("%v\" '" , os .PathListSeparator ))
98121 inputExpanded := expand .Expand (input , func (key string ) (value string , ok bool ) {
122+ switch key {
99123 // do not expand the desired variable itself
100- if key == s .key {
124+ case s .key :
101125 return "" , false
126+ default :
127+ return os .LookupEnv (key )
102128 }
103- return os .LookupEnv (key )
104129 })
105130 all := strings .Split (inputExpanded , fmt .Sprintf ("%c" , os .PathListSeparator ))
106131 res := hashset .New [string ](uint64 (len (all )), g .Equals [string ], g .HashString )
@@ -141,3 +166,11 @@ func appendIfNotInSlice[T any](input []T, value T, eq func(T, T) bool) []T {
141166
142167 return res
143168}
169+
170+ func isDir (p string ) bool {
171+ f , err := os .Stat (p )
172+ if err != nil {
173+ return false
174+ }
175+ return f .IsDir ()
176+ }
0 commit comments