@@ -139,18 +139,18 @@ func absoluteSourcesPaths(cloneBase, pkgDir string, srcs []string) []string {
139139// extension (which are from the cache). This is a work around for
140140// https://golang.org/issue/28749: cmd/go puts assembly, C, and C++ files in
141141// CompiledGoFiles.
142- func filterGoFiles (srcs []string ) []string {
142+ func filterGoFiles (srcs []string , pathReplaceFn func ( p string ) string ) []string {
143143 ret := make ([]string , 0 , len (srcs ))
144144 for _ , f := range srcs {
145145 if ext := filepath .Ext (f ); ext == ".go" || ext == "" {
146- ret = append (ret , f )
146+ ret = append (ret , pathReplaceFn ( f ) )
147147 }
148148 }
149149
150150 return ret
151151}
152152
153- func flatPackageForStd (cloneBase string , pkg * goListPackage ) * flatPackage {
153+ func flatPackageForStd (cloneBase string , pkg * goListPackage , pathReplaceFn func ( p string ) string ) * flatPackage {
154154 goFiles := absoluteSourcesPaths (cloneBase , pkg .Dir , pkg .GoFiles )
155155 compiledGoFiles := absoluteSourcesPaths (cloneBase , pkg .Dir , pkg .CompiledGoFiles )
156156
@@ -162,7 +162,7 @@ func flatPackageForStd(cloneBase string, pkg *goListPackage) *flatPackage {
162162 Imports : map [string ]string {},
163163 Standard : pkg .Standard ,
164164 GoFiles : goFiles ,
165- CompiledGoFiles : filterGoFiles (compiledGoFiles ),
165+ CompiledGoFiles : filterGoFiles (compiledGoFiles , pathReplaceFn ),
166166 }
167167
168168 // imports
@@ -194,6 +194,7 @@ func stdliblist(args []string) error {
194194 flags := flag .NewFlagSet ("stdliblist" , flag .ExitOnError )
195195 goenv := envFlags (flags )
196196 out := flags .String ("out" , "" , "Path to output go list json" )
197+ cachePath := flags .String ("cache" , "" , "Path to use for GOCACHE" )
197198 if err := flags .Parse (args ); err != nil {
198199 return err
199200 }
@@ -250,10 +251,10 @@ func stdliblist(args []string) error {
250251 os .Setenv ("CC" , quotePathIfNeeded (abs (ccEnv )))
251252
252253 // We want to keep the cache around so that the processed files can be used by other tools.
253- cachePath := abs (* out + ".gocache" )
254- os .Setenv ("GOCACHE" , cachePath )
255- os .Setenv ("GOMODCACHE" , cachePath )
256- os .Setenv ("GOPATH" , cachePath )
254+ absCachePath := abs (* cachePath )
255+ os .Setenv ("GOCACHE" , absCachePath )
256+ os .Setenv ("GOMODCACHE" , absCachePath )
257+ os .Setenv ("GOPATH" , absCachePath )
257258
258259 listArgs := goenv .goCmd ("list" )
259260 if len (build .Default .BuildTags ) > 0 {
@@ -279,12 +280,19 @@ func stdliblist(args []string) error {
279280
280281 encoder := json .NewEncoder (jsonFile )
281282 decoder := json .NewDecoder (jsonData )
283+ pathReplaceFn := func (s string ) string {
284+ if strings .HasPrefix (s , absCachePath ) {
285+ return strings .Replace (s , absCachePath , filepath .Join ("__BAZEL_EXECROOT__" , * cachePath ), 1 )
286+ }
287+
288+ return s
289+ }
282290 for decoder .More () {
283291 var pkg * goListPackage
284292 if err := decoder .Decode (& pkg ); err != nil {
285293 return err
286294 }
287- if err := encoder .Encode (flatPackageForStd (cloneBase , pkg )); err != nil {
295+ if err := encoder .Encode (flatPackageForStd (cloneBase , pkg , pathReplaceFn )); err != nil {
288296 return err
289297 }
290298 }
0 commit comments