@@ -127,9 +127,6 @@ func (c *Cache) pkgActionID(pkg *packages.Package, mode HashMode) (cache.ActionI
127
127
return key .Sum (), nil
128
128
}
129
129
130
- // packageHash computes a package's hash.
131
- // The hash is based on all Go files that make up the package,
132
- // as well as the hashes of imported packages.
133
130
func (c * Cache ) packageHash (pkg * packages.Package , mode HashMode ) (string , error ) {
134
131
hashResI , ok := c .pkgHashes .Load (pkg )
135
132
if ok {
@@ -141,9 +138,27 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error
141
138
return hashRes [mode ], nil
142
139
}
143
140
141
+ hashRes , err := c .computePkgHash (pkg )
142
+ if err != nil {
143
+ return "" , err
144
+ }
145
+
146
+ if _ , ok := hashRes [mode ]; ! ok {
147
+ return "" , fmt .Errorf ("invalid mode %d" , mode )
148
+ }
149
+
150
+ c .pkgHashes .Store (pkg , hashRes )
151
+
152
+ return hashRes [mode ], nil
153
+ }
154
+
155
+ // computePkgHash computes a package's hash.
156
+ // The hash is based on all Go files that make up the package,
157
+ // as well as the hashes of imported packages.
158
+ func (c * Cache ) computePkgHash (pkg * packages.Package ) (hashResults , error ) {
144
159
key , err := cache .NewHash ("package hash" )
145
160
if err != nil {
146
- return "" , fmt .Errorf ("failed to make a hash: %w" , err )
161
+ return nil , fmt .Errorf ("failed to make a hash: %w" , err )
147
162
}
148
163
149
164
hashRes := hashResults {}
@@ -153,7 +168,7 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error
153
168
for _ , f := range pkg .CompiledGoFiles {
154
169
h , fErr := c .fileHash (f )
155
170
if fErr != nil {
156
- return "" , fmt .Errorf ("failed to calculate file %s hash: %w" , f , fErr )
171
+ return nil , fmt .Errorf ("failed to calculate file %s hash: %w" , f , fErr )
157
172
}
158
173
159
174
fmt .Fprintf (key , "file %s %x\n " , f , h )
@@ -169,26 +184,20 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error
169
184
})
170
185
171
186
if err := c .computeDepsHash (HashModeNeedOnlySelf , imps , key ); err != nil {
172
- return "" , err
187
+ return nil , err
173
188
}
174
189
175
190
curSum = key .Sum ()
176
191
hashRes [HashModeNeedDirectDeps ] = hex .EncodeToString (curSum [:])
177
192
178
193
if err := c .computeDepsHash (HashModeNeedAllDeps , imps , key ); err != nil {
179
- return "" , err
194
+ return nil , err
180
195
}
181
196
182
197
curSum = key .Sum ()
183
198
hashRes [HashModeNeedAllDeps ] = hex .EncodeToString (curSum [:])
184
199
185
- if _ , ok := hashRes [mode ]; ! ok {
186
- return "" , fmt .Errorf ("invalid mode %d" , mode )
187
- }
188
-
189
- c .pkgHashes .Store (pkg , hashRes )
190
-
191
- return hashRes [mode ], nil
200
+ return hashRes , nil
192
201
}
193
202
194
203
func (c * Cache ) computeDepsHash (depMode HashMode , imps []* packages.Package , key * cache.Hash ) error {
0 commit comments