blob: 2c080a11a937aaaedeeda84bc4831e4b0493822e [file] [log] [blame]
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bazel
16
17import (
Chris Parsons0bfb1c02022-05-12 16:43:01 -040018 "crypto/sha256"
Usta Shrestha2ccdb422022-06-02 10:19:13 -040019 "encoding/base64"
Chris Parsonsaffbb602020-12-23 12:02:11 -050020 "fmt"
21 "path/filepath"
Chris Parsons0bfb1c02022-05-12 16:43:01 -040022 "reflect"
Chris Parsons0bfb1c02022-05-12 16:43:01 -040023 "sort"
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050024 "strings"
Liz Kammera4655a92023-02-10 17:17:28 -050025 "sync"
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050026
Liz Kammer690fbac2023-02-10 11:11:17 -050027 analysis_v2_proto "prebuilts/bazel/common/proto/analysis_v2"
28
29 "github.com/google/blueprint/metrics"
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050030 "github.com/google/blueprint/proptools"
Jason Wu118fd2b2022-10-27 18:41:15 +000031 "google.golang.org/protobuf/proto"
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050032)
33
Usta Shrestha6298cc52022-05-27 17:40:21 -040034type artifactId int
35type depsetId int
36type pathFragmentId int
37
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050038// artifact contains relevant portions of Bazel's aquery proto, Artifact.
39// Represents a single artifact, whether it's a source file or a derived output file.
40type artifact struct {
Usta Shrestha6298cc52022-05-27 17:40:21 -040041 Id artifactId
42 PathFragmentId pathFragmentId
Chris Parsonsaffbb602020-12-23 12:02:11 -050043}
44
45type pathFragment struct {
Usta Shrestha6298cc52022-05-27 17:40:21 -040046 Id pathFragmentId
Chris Parsonsaffbb602020-12-23 12:02:11 -050047 Label string
Usta Shrestha6298cc52022-05-27 17:40:21 -040048 ParentId pathFragmentId
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050049}
50
51// KeyValuePair represents Bazel's aquery proto, KeyValuePair.
52type KeyValuePair struct {
53 Key string
54 Value string
55}
56
Chris Parsons1a7aca02022-04-25 22:35:15 -040057// AqueryDepset is a depset definition from Bazel's aquery response. This is
Chris Parsons0bfb1c02022-05-12 16:43:01 -040058// akin to the `depSetOfFiles` in the response proto, except:
Colin Crossd079e0b2022-08-16 10:27:33 -070059// - direct artifacts are enumerated by full path instead of by ID
60// - it has a hash of the depset contents, instead of an int ID (for determinism)
61//
Chris Parsons1a7aca02022-04-25 22:35:15 -040062// A depset is a data structure for efficient transitive handling of artifact
63// paths. A single depset consists of one or more artifact paths and one or
64// more "child" depsets.
65type AqueryDepset struct {
Chris Parsons0bfb1c02022-05-12 16:43:01 -040066 ContentHash string
67 DirectArtifacts []string
68 TransitiveDepSetHashes []string
Chris Parsons1a7aca02022-04-25 22:35:15 -040069}
70
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050071// depSetOfFiles contains relevant portions of Bazel's aquery proto, DepSetOfFiles.
72// Represents a data structure containing one or more files. Depsets in Bazel are an efficient
73// data structure for storing large numbers of file paths.
74type depSetOfFiles struct {
Usta Shrestha6298cc52022-05-27 17:40:21 -040075 Id depsetId
76 DirectArtifactIds []artifactId
77 TransitiveDepSetIds []depsetId
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050078}
79
80// action contains relevant portions of Bazel's aquery proto, Action.
81// Represents a single command line invocation in the Bazel build graph.
82type action struct {
83 Arguments []string
84 EnvironmentVariables []KeyValuePair
Usta Shrestha6298cc52022-05-27 17:40:21 -040085 InputDepSetIds []depsetId
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050086 Mnemonic string
Usta Shrestha6298cc52022-05-27 17:40:21 -040087 OutputIds []artifactId
Wei Li455ba832021-11-04 22:58:12 +000088 TemplateContent string
89 Substitutions []KeyValuePair
Sasha Smundak1da064c2022-06-08 16:36:16 -070090 FileContents string
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -050091}
92
93// actionGraphContainer contains relevant portions of Bazel's aquery proto, ActionGraphContainer.
94// An aquery response from Bazel contains a single ActionGraphContainer proto.
95type actionGraphContainer struct {
96 Artifacts []artifact
97 Actions []action
98 DepSetOfFiles []depSetOfFiles
Chris Parsonsaffbb602020-12-23 12:02:11 -050099 PathFragments []pathFragment
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500100}
101
102// BuildStatement contains information to register a build statement corresponding (one to one)
103// with a Bazel action from Bazel's action graph.
104type BuildStatement struct {
Liz Kammerc49e6822021-06-08 15:04:11 -0400105 Command string
106 Depfile *string
107 OutputPaths []string
Liz Kammerc49e6822021-06-08 15:04:11 -0400108 SymlinkPaths []string
Liz Kammer00629db2023-02-09 14:28:15 -0500109 Env []*analysis_v2_proto.KeyValuePair
Liz Kammerc49e6822021-06-08 15:04:11 -0400110 Mnemonic string
Chris Parsons1a7aca02022-04-25 22:35:15 -0400111
112 // Inputs of this build statement, either as unexpanded depsets or expanded
113 // input paths. There should be no overlap between these fields; an input
114 // path should either be included as part of an unexpanded depset or a raw
115 // input path string, but not both.
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400116 InputDepsetHashes []string
117 InputPaths []string
Sasha Smundak1da064c2022-06-08 16:36:16 -0700118 FileContents string
Spandan Dasaf4ccaa2023-06-29 01:15:51 +0000119 // If ShouldRunInSbox is true, Soong will use sbox to created an isolated environment
120 // and run the mixed build action there
121 ShouldRunInSbox bool
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500122}
123
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400124// A helper type for aquery processing which facilitates retrieval of path IDs from their
125// less readable Bazel structures (depset and path fragment).
126type aqueryArtifactHandler struct {
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400127 // Maps depset id to AqueryDepset, a representation of depset which is
128 // post-processed for middleman artifact handling, unhandled artifact
129 // dropping, content hashing, etc.
Usta Shrestha6298cc52022-05-27 17:40:21 -0400130 depsetIdToAqueryDepset map[depsetId]AqueryDepset
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500131 emptyDepsetIds map[depsetId]struct{}
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400132 // Maps content hash to AqueryDepset.
133 depsetHashToAqueryDepset map[string]AqueryDepset
134
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400135 // depsetIdToArtifactIdsCache is a memoization of depset flattening, because flattening
136 // may be an expensive operation.
Liz Kammera4655a92023-02-10 17:17:28 -0500137 depsetHashToArtifactPathsCache sync.Map
Usta Shrestha6298cc52022-05-27 17:40:21 -0400138 // Maps artifact ids to fully expanded paths.
139 artifactIdToPath map[artifactId]string
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400140}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500141
Wei Li455ba832021-11-04 22:58:12 +0000142// The tokens should be substituted with the value specified here, instead of the
143// one returned in 'substitutions' of TemplateExpand action.
Usta Shrestha6298cc52022-05-27 17:40:21 -0400144var templateActionOverriddenTokens = map[string]string{
Wei Li455ba832021-11-04 22:58:12 +0000145 // Uses "python3" for %python_binary% instead of the value returned by aquery
146 // which is "py3wrapper.sh". See removePy3wrapperScript.
147 "%python_binary%": "python3",
148}
149
Liz Kammer00629db2023-02-09 14:28:15 -0500150const (
151 middlemanMnemonic = "Middleman"
152 // The file name of py3wrapper.sh, which is used by py_binary targets.
153 py3wrapperFileName = "/py3wrapper.sh"
154)
Wei Li455ba832021-11-04 22:58:12 +0000155
Usta Shrestha6298cc52022-05-27 17:40:21 -0400156func indexBy[K comparable, V any](values []V, keyFn func(v V) K) map[K]V {
157 m := map[K]V{}
158 for _, v := range values {
159 m[keyFn(v)] = v
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500160 }
Usta Shrestha6298cc52022-05-27 17:40:21 -0400161 return m
162}
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400163
Liz Kammer00629db2023-02-09 14:28:15 -0500164func newAqueryHandler(aqueryResult *analysis_v2_proto.ActionGraphContainer) (*aqueryArtifactHandler, error) {
165 pathFragments := indexBy(aqueryResult.PathFragments, func(pf *analysis_v2_proto.PathFragment) pathFragmentId {
166 return pathFragmentId(pf.Id)
Usta Shrestha6298cc52022-05-27 17:40:21 -0400167 })
168
Liz Kammer00629db2023-02-09 14:28:15 -0500169 artifactIdToPath := make(map[artifactId]string, len(aqueryResult.Artifacts))
Chris Parsonsaffbb602020-12-23 12:02:11 -0500170 for _, artifact := range aqueryResult.Artifacts {
Liz Kammer00629db2023-02-09 14:28:15 -0500171 artifactPath, err := expandPathFragment(pathFragmentId(artifact.PathFragmentId), pathFragments)
Chris Parsonsaffbb602020-12-23 12:02:11 -0500172 if err != nil {
Chris Parsons4f069892021-01-15 12:22:41 -0500173 return nil, err
Chris Parsonsaffbb602020-12-23 12:02:11 -0500174 }
Romain Jobredeauxe3989a12023-07-19 20:58:27 +0000175 artifactIdToPath[artifactId(artifact.Id)] = artifactPath
Chris Parsonsaffbb602020-12-23 12:02:11 -0500176 }
Chris Parsons943f2432021-01-19 11:36:50 -0500177
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400178 // Map middleman artifact ContentHash to input artifact depset ID.
Chris Parsons1a7aca02022-04-25 22:35:15 -0400179 // Middleman artifacts are treated as "substitute" artifacts for mixed builds. For example,
Usta Shrestha16ac1352022-06-22 11:01:55 -0400180 // if we find a middleman action which has inputs [foo, bar], and output [baz_middleman], then,
Chris Parsons1a7aca02022-04-25 22:35:15 -0400181 // for each other action which has input [baz_middleman], we add [foo, bar] to the inputs for
182 // that action instead.
Liz Kammer00629db2023-02-09 14:28:15 -0500183 middlemanIdToDepsetIds := map[artifactId][]uint32{}
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500184 for _, actionEntry := range aqueryResult.Actions {
Liz Kammer00629db2023-02-09 14:28:15 -0500185 if actionEntry.Mnemonic == middlemanMnemonic {
Chris Parsons8d6e4332021-02-22 16:13:50 -0500186 for _, outputId := range actionEntry.OutputIds {
Liz Kammer00629db2023-02-09 14:28:15 -0500187 middlemanIdToDepsetIds[artifactId(outputId)] = actionEntry.InputDepSetIds
Chris Parsons8d6e4332021-02-22 16:13:50 -0500188 }
189 }
190 }
Chris Parsons1a7aca02022-04-25 22:35:15 -0400191
Liz Kammer00629db2023-02-09 14:28:15 -0500192 depsetIdToDepset := indexBy(aqueryResult.DepSetOfFiles, func(d *analysis_v2_proto.DepSetOfFiles) depsetId {
193 return depsetId(d.Id)
Usta Shrestha6298cc52022-05-27 17:40:21 -0400194 })
Chris Parsons1a7aca02022-04-25 22:35:15 -0400195
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400196 aqueryHandler := aqueryArtifactHandler{
Usta Shrestha6298cc52022-05-27 17:40:21 -0400197 depsetIdToAqueryDepset: map[depsetId]AqueryDepset{},
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400198 depsetHashToAqueryDepset: map[string]AqueryDepset{},
Liz Kammera4655a92023-02-10 17:17:28 -0500199 depsetHashToArtifactPathsCache: sync.Map{},
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500200 emptyDepsetIds: make(map[depsetId]struct{}, 0),
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400201 artifactIdToPath: artifactIdToPath,
202 }
203
204 // Validate and adjust aqueryResult.DepSetOfFiles values.
205 for _, depset := range aqueryResult.DepSetOfFiles {
206 _, err := aqueryHandler.populateDepsetMaps(depset, middlemanIdToDepsetIds, depsetIdToDepset)
207 if err != nil {
208 return nil, err
209 }
210 }
211
212 return &aqueryHandler, nil
213}
214
215// Ensures that the handler's depsetIdToAqueryDepset map contains an entry for the given
216// depset.
Liz Kammer00629db2023-02-09 14:28:15 -0500217func (a *aqueryArtifactHandler) populateDepsetMaps(depset *analysis_v2_proto.DepSetOfFiles, middlemanIdToDepsetIds map[artifactId][]uint32, depsetIdToDepset map[depsetId]*analysis_v2_proto.DepSetOfFiles) (*AqueryDepset, error) {
218 if aqueryDepset, containsDepset := a.depsetIdToAqueryDepset[depsetId(depset.Id)]; containsDepset {
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500219 return &aqueryDepset, nil
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400220 }
221 transitiveDepsetIds := depset.TransitiveDepSetIds
Liz Kammer00629db2023-02-09 14:28:15 -0500222 directArtifactPaths := make([]string, 0, len(depset.DirectArtifactIds))
223 for _, id := range depset.DirectArtifactIds {
224 aId := artifactId(id)
225 path, pathExists := a.artifactIdToPath[aId]
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400226 if !pathExists {
Liz Kammer00629db2023-02-09 14:28:15 -0500227 return nil, fmt.Errorf("undefined input artifactId %d", aId)
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400228 }
229 // Filter out any inputs which are universally dropped, and swap middleman
230 // artifacts with their corresponding depsets.
Liz Kammer00629db2023-02-09 14:28:15 -0500231 if depsetsToUse, isMiddleman := middlemanIdToDepsetIds[aId]; isMiddleman {
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400232 // Swap middleman artifacts with their corresponding depsets and drop the middleman artifacts.
233 transitiveDepsetIds = append(transitiveDepsetIds, depsetsToUse...)
Usta Shresthaef922252022-06-02 14:23:02 -0400234 } else if strings.HasSuffix(path, py3wrapperFileName) ||
Usta Shresthaef922252022-06-02 14:23:02 -0400235 strings.HasPrefix(path, "../bazel_tools") {
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500236 continue
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400237 // Drop these artifacts.
238 // See go/python-binary-host-mixed-build for more details.
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700239 // 1) Drop py3wrapper.sh, just use python binary, the launcher script generated by the
240 // TemplateExpandAction handles everything necessary to launch a Pythin application.
241 // 2) ../bazel_tools: they have MODIFY timestamp 10years in the future and would cause the
Usta Shresthaef922252022-06-02 14:23:02 -0400242 // containing depset to always be considered newer than their outputs.
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400243 } else {
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400244 directArtifactPaths = append(directArtifactPaths, path)
245 }
246 }
247
Liz Kammer00629db2023-02-09 14:28:15 -0500248 childDepsetHashes := make([]string, 0, len(transitiveDepsetIds))
249 for _, id := range transitiveDepsetIds {
250 childDepsetId := depsetId(id)
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400251 childDepset, exists := depsetIdToDepset[childDepsetId]
252 if !exists {
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500253 if _, empty := a.emptyDepsetIds[childDepsetId]; empty {
254 continue
255 } else {
256 return nil, fmt.Errorf("undefined input depsetId %d (referenced by depsetId %d)", childDepsetId, depset.Id)
257 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400258 }
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500259 if childAqueryDepset, err := a.populateDepsetMaps(childDepset, middlemanIdToDepsetIds, depsetIdToDepset); err != nil {
260 return nil, err
261 } else if childAqueryDepset == nil {
262 continue
263 } else {
264 childDepsetHashes = append(childDepsetHashes, childAqueryDepset.ContentHash)
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400265 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400266 }
Usta Shresthaef922252022-06-02 14:23:02 -0400267 if len(directArtifactPaths) == 0 && len(childDepsetHashes) == 0 {
Liz Kammer00629db2023-02-09 14:28:15 -0500268 a.emptyDepsetIds[depsetId(depset.Id)] = struct{}{}
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500269 return nil, nil
Usta Shresthaef922252022-06-02 14:23:02 -0400270 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400271 aqueryDepset := AqueryDepset{
272 ContentHash: depsetContentHash(directArtifactPaths, childDepsetHashes),
273 DirectArtifacts: directArtifactPaths,
274 TransitiveDepSetHashes: childDepsetHashes,
275 }
Liz Kammer00629db2023-02-09 14:28:15 -0500276 a.depsetIdToAqueryDepset[depsetId(depset.Id)] = aqueryDepset
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400277 a.depsetHashToAqueryDepset[aqueryDepset.ContentHash] = aqueryDepset
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500278 return &aqueryDepset, nil
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400279}
280
Chris Parsons1a7aca02022-04-25 22:35:15 -0400281// getInputPaths flattens the depsets of the given IDs and returns all transitive
282// input paths contained in these depsets.
283// This is a potentially expensive operation, and should not be invoked except
284// for actions which need specialized input handling.
Liz Kammer00629db2023-02-09 14:28:15 -0500285func (a *aqueryArtifactHandler) getInputPaths(depsetIds []uint32) ([]string, error) {
Usta Shrestha6298cc52022-05-27 17:40:21 -0400286 var inputPaths []string
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400287
Liz Kammer00629db2023-02-09 14:28:15 -0500288 for _, id := range depsetIds {
289 inputDepSetId := depsetId(id)
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400290 depset := a.depsetIdToAqueryDepset[inputDepSetId]
291 inputArtifacts, err := a.artifactPathsFromDepsetHash(depset.ContentHash)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400292 if err != nil {
293 return nil, err
294 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400295 for _, inputPath := range inputArtifacts {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400296 inputPaths = append(inputPaths, inputPath)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400297 }
298 }
Wei Li455ba832021-11-04 22:58:12 +0000299
Chris Parsons1a7aca02022-04-25 22:35:15 -0400300 return inputPaths, nil
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400301}
302
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400303func (a *aqueryArtifactHandler) artifactPathsFromDepsetHash(depsetHash string) ([]string, error) {
Liz Kammera4655a92023-02-10 17:17:28 -0500304 if result, exists := a.depsetHashToArtifactPathsCache.Load(depsetHash); exists {
305 return result.([]string), nil
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400306 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400307 if depset, exists := a.depsetHashToAqueryDepset[depsetHash]; exists {
308 result := depset.DirectArtifacts
309 for _, childHash := range depset.TransitiveDepSetHashes {
310 childArtifactIds, err := a.artifactPathsFromDepsetHash(childHash)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400311 if err != nil {
312 return nil, err
313 }
314 result = append(result, childArtifactIds...)
315 }
Liz Kammera4655a92023-02-10 17:17:28 -0500316 a.depsetHashToArtifactPathsCache.Store(depsetHash, result)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400317 return result, nil
318 } else {
Usta Shrestha2ccdb422022-06-02 10:19:13 -0400319 return nil, fmt.Errorf("undefined input depset hash %s", depsetHash)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400320 }
321}
322
Chris Parsons1a7aca02022-04-25 22:35:15 -0400323// AqueryBuildStatements returns a slice of BuildStatements and a slice of AqueryDepset
Usta Shrestha6298cc52022-05-27 17:40:21 -0400324// which should be registered (and output to a ninja file) to correspond with Bazel's
Chris Parsons1a7aca02022-04-25 22:35:15 -0400325// action graph, as described by the given action graph json proto.
326// BuildStatements are one-to-one with actions in the given action graph, and AqueryDepsets
327// are one-to-one with Bazel's depSetOfFiles objects.
Liz Kammera4655a92023-02-10 17:17:28 -0500328func AqueryBuildStatements(aqueryJsonProto []byte, eventHandler *metrics.EventHandler) ([]*BuildStatement, []AqueryDepset, error) {
Jason Wu118fd2b2022-10-27 18:41:15 +0000329 aqueryProto := &analysis_v2_proto.ActionGraphContainer{}
330 err := proto.Unmarshal(aqueryJsonProto, aqueryProto)
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400331 if err != nil {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400332 return nil, nil, err
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400333 }
Chris Parsons8d6e4332021-02-22 16:13:50 -0500334
Liz Kammer690fbac2023-02-10 11:11:17 -0500335 var aqueryHandler *aqueryArtifactHandler
336 {
337 eventHandler.Begin("init_handler")
338 defer eventHandler.End("init_handler")
Liz Kammer00629db2023-02-09 14:28:15 -0500339 aqueryHandler, err = newAqueryHandler(aqueryProto)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400340 if err != nil {
341 return nil, nil, err
Chris Parsons8d6e4332021-02-22 16:13:50 -0500342 }
Liz Kammer690fbac2023-02-10 11:11:17 -0500343 }
344
Liz Kammera4655a92023-02-10 17:17:28 -0500345 // allocate both length and capacity so each goroutine can write to an index independently without
346 // any need for synchronization for slice access.
347 buildStatements := make([]*BuildStatement, len(aqueryProto.Actions))
Liz Kammer690fbac2023-02-10 11:11:17 -0500348 {
349 eventHandler.Begin("build_statements")
350 defer eventHandler.End("build_statements")
Liz Kammera4655a92023-02-10 17:17:28 -0500351 wg := sync.WaitGroup{}
352 var errOnce sync.Once
353
354 for i, actionEntry := range aqueryProto.Actions {
355 wg.Add(1)
356 go func(i int, actionEntry *analysis_v2_proto.Action) {
357 buildStatement, aErr := aqueryHandler.actionToBuildStatement(actionEntry)
358 if aErr != nil {
359 errOnce.Do(func() {
360 err = aErr
361 })
362 } else {
363 // set build statement at an index rather than appending such that each goroutine does not
364 // impact other goroutines
365 buildStatements[i] = buildStatement
366 }
367 wg.Done()
368 }(i, actionEntry)
Liz Kammer690fbac2023-02-10 11:11:17 -0500369 }
Liz Kammera4655a92023-02-10 17:17:28 -0500370 wg.Wait()
371 }
372 if err != nil {
373 return nil, nil, err
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500374 }
375
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400376 depsetsByHash := map[string]AqueryDepset{}
Liz Kammer00629db2023-02-09 14:28:15 -0500377 depsets := make([]AqueryDepset, 0, len(aqueryHandler.depsetIdToAqueryDepset))
Liz Kammer690fbac2023-02-10 11:11:17 -0500378 {
379 eventHandler.Begin("depsets")
380 defer eventHandler.End("depsets")
381 for _, aqueryDepset := range aqueryHandler.depsetIdToAqueryDepset {
382 if prevEntry, hasKey := depsetsByHash[aqueryDepset.ContentHash]; hasKey {
383 // Two depsets collide on hash. Ensure that their contents are identical.
384 if !reflect.DeepEqual(aqueryDepset, prevEntry) {
385 return nil, nil, fmt.Errorf("two different depsets have the same hash: %v, %v", prevEntry, aqueryDepset)
386 }
387 } else {
388 depsetsByHash[aqueryDepset.ContentHash] = aqueryDepset
389 depsets = append(depsets, aqueryDepset)
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400390 }
Chris Parsons1a7aca02022-04-25 22:35:15 -0400391 }
Chris Parsons1a7aca02022-04-25 22:35:15 -0400392 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400393
Liz Kammer690fbac2023-02-10 11:11:17 -0500394 eventHandler.Do("build_statement_sort", func() {
395 // Build Statements and depsets must be sorted by their content hash to
396 // preserve determinism between builds (this will result in consistent ninja file
397 // output). Note they are not sorted by their original IDs nor their Bazel ordering,
398 // as Bazel gives nondeterministic ordering / identifiers in aquery responses.
399 sort.Slice(buildStatements, func(i, j int) bool {
Liz Kammera4655a92023-02-10 17:17:28 -0500400 // Sort all nil statements to the end of the slice
401 if buildStatements[i] == nil {
402 return false
403 } else if buildStatements[j] == nil {
404 return true
405 }
406 //For build statements, compare output lists. In Bazel, each output file
Liz Kammer690fbac2023-02-10 11:11:17 -0500407 // may only have one action which generates it, so this will provide
408 // a deterministic ordering.
409 outputs_i := buildStatements[i].OutputPaths
410 outputs_j := buildStatements[j].OutputPaths
411 if len(outputs_i) != len(outputs_j) {
412 return len(outputs_i) < len(outputs_j)
413 }
414 if len(outputs_i) == 0 {
415 // No outputs for these actions, so compare commands.
416 return buildStatements[i].Command < buildStatements[j].Command
417 }
418 // There may be multiple outputs, but the output ordering is deterministic.
419 return outputs_i[0] < outputs_j[0]
420 })
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400421 })
Liz Kammer690fbac2023-02-10 11:11:17 -0500422 eventHandler.Do("depset_sort", func() {
423 sort.Slice(depsets, func(i, j int) bool {
424 return depsets[i].ContentHash < depsets[j].ContentHash
425 })
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400426 })
Chris Parsons1a7aca02022-04-25 22:35:15 -0400427 return buildStatements, depsets, nil
428}
429
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400430// depsetContentHash computes and returns a SHA256 checksum of the contents of
431// the given depset. This content hash may serve as the depset's identifier.
432// Using a content hash for an identifier is superior for determinism. (For example,
433// using an integer identifier which depends on the order in which the depsets are
434// created would result in nondeterministic depset IDs.)
435func depsetContentHash(directPaths []string, transitiveDepsetHashes []string) string {
436 h := sha256.New()
437 // Use newline as delimiter, as paths cannot contain newline.
438 h.Write([]byte(strings.Join(directPaths, "\n")))
Usta Shrestha2ccdb422022-06-02 10:19:13 -0400439 h.Write([]byte(strings.Join(transitiveDepsetHashes, "")))
440 fullHash := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400441 return fullHash
442}
443
Liz Kammer00629db2023-02-09 14:28:15 -0500444func (a *aqueryArtifactHandler) depsetContentHashes(inputDepsetIds []uint32) ([]string, error) {
Usta Shrestha6298cc52022-05-27 17:40:21 -0400445 var hashes []string
Liz Kammer00629db2023-02-09 14:28:15 -0500446 for _, id := range inputDepsetIds {
447 dId := depsetId(id)
448 if aqueryDepset, exists := a.depsetIdToAqueryDepset[dId]; !exists {
449 if _, empty := a.emptyDepsetIds[dId]; !empty {
450 return nil, fmt.Errorf("undefined (not even empty) input depsetId %d", dId)
Usta Shrestha13fd5ae2023-01-27 10:55:34 -0500451 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400452 } else {
453 hashes = append(hashes, aqueryDepset.ContentHash)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400454 }
455 }
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400456 return hashes, nil
Chris Parsons1a7aca02022-04-25 22:35:15 -0400457}
458
Spandan Dasda724862023-06-16 23:35:55 +0000459// escapes the args received from aquery and creates a command string
460func commandString(actionEntry *analysis_v2_proto.Action) string {
461 switch actionEntry.Mnemonic {
Spandan Das2d93ebb2023-07-27 23:46:24 +0000462 case "GoCompilePkg", "GoStdlib":
Spandan Dasda724862023-06-16 23:35:55 +0000463 argsEscaped := []string{}
464 for _, arg := range actionEntry.Arguments {
465 if arg == "" {
466 // If this is an empty string, add ''
467 // And not
468 // 1. (literal empty)
469 // 2. `''\'''\'''` (escaped version of '')
470 //
471 // If we had used (1), then this would appear as a whitespace when we strings.Join
472 argsEscaped = append(argsEscaped, "''")
473 } else {
474 argsEscaped = append(argsEscaped, proptools.ShellEscapeIncludingSpaces(arg))
475 }
476 }
477 return strings.Join(argsEscaped, " ")
478 default:
479 return strings.Join(proptools.ShellEscapeListIncludingSpaces(actionEntry.Arguments), " ")
480 }
481}
482
Liz Kammer00629db2023-02-09 14:28:15 -0500483func (a *aqueryArtifactHandler) normalActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) {
Spandan Dasda724862023-06-16 23:35:55 +0000484 command := commandString(actionEntry)
Usta Shresthac2372492022-05-27 10:45:00 -0400485 inputDepsetHashes, err := a.depsetContentHashes(actionEntry.InputDepSetIds)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400486 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500487 return nil, err
Chris Parsons1a7aca02022-04-25 22:35:15 -0400488 }
Usta Shresthac2372492022-05-27 10:45:00 -0400489 outputPaths, depfile, err := a.getOutputPaths(actionEntry)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400490 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500491 return nil, err
Chris Parsons1a7aca02022-04-25 22:35:15 -0400492 }
493
Liz Kammer00629db2023-02-09 14:28:15 -0500494 buildStatement := &BuildStatement{
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400495 Command: command,
496 Depfile: depfile,
497 OutputPaths: outputPaths,
498 InputDepsetHashes: inputDepsetHashes,
499 Env: actionEntry.EnvironmentVariables,
500 Mnemonic: actionEntry.Mnemonic,
Chris Parsons1a7aca02022-04-25 22:35:15 -0400501 }
Spandan Dasaf4ccaa2023-06-29 01:15:51 +0000502 if buildStatement.Mnemonic == "GoToolchainBinaryBuild" {
503 // Unlike b's execution root, mixed build execution root contains a symlink to prebuilts/go
504 // This causes issues for `GOCACHE=$(mktemp -d) go build ...`
505 // To prevent this, sandbox this action in mixed builds as well
506 buildStatement.ShouldRunInSbox = true
507 }
Chris Parsons1a7aca02022-04-25 22:35:15 -0400508 return buildStatement, nil
509}
510
Liz Kammer00629db2023-02-09 14:28:15 -0500511func (a *aqueryArtifactHandler) templateExpandActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) {
Usta Shresthac2372492022-05-27 10:45:00 -0400512 outputPaths, depfile, err := a.getOutputPaths(actionEntry)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400513 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500514 return nil, err
Chris Parsons1a7aca02022-04-25 22:35:15 -0400515 }
516 if len(outputPaths) != 1 {
Liz Kammer00629db2023-02-09 14:28:15 -0500517 return nil, fmt.Errorf("Expect 1 output to template expand action, got: output %q", outputPaths)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400518 }
519 expandedTemplateContent := expandTemplateContent(actionEntry)
520 // The expandedTemplateContent is escaped for being used in double quotes and shell unescape,
521 // and the new line characters (\n) are also changed to \\n which avoids some Ninja escape on \n, which might
522 // change \n to space and mess up the format of Python programs.
523 // sed is used to convert \\n back to \n before saving to output file.
524 // See go/python-binary-host-mixed-build for more details.
525 command := fmt.Sprintf(`/bin/bash -c 'echo "%[1]s" | sed "s/\\\\n/\\n/g" > %[2]s && chmod a+x %[2]s'`,
526 escapeCommandlineArgument(expandedTemplateContent), outputPaths[0])
Usta Shresthac2372492022-05-27 10:45:00 -0400527 inputDepsetHashes, err := a.depsetContentHashes(actionEntry.InputDepSetIds)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400528 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500529 return nil, err
Chris Parsons1a7aca02022-04-25 22:35:15 -0400530 }
531
Liz Kammer00629db2023-02-09 14:28:15 -0500532 buildStatement := &BuildStatement{
Chris Parsons0bfb1c02022-05-12 16:43:01 -0400533 Command: command,
534 Depfile: depfile,
535 OutputPaths: outputPaths,
536 InputDepsetHashes: inputDepsetHashes,
537 Env: actionEntry.EnvironmentVariables,
538 Mnemonic: actionEntry.Mnemonic,
Chris Parsons1a7aca02022-04-25 22:35:15 -0400539 }
540 return buildStatement, nil
541}
542
Liz Kammer00629db2023-02-09 14:28:15 -0500543func (a *aqueryArtifactHandler) fileWriteActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) {
Sasha Smundak1da064c2022-06-08 16:36:16 -0700544 outputPaths, _, err := a.getOutputPaths(actionEntry)
545 var depsetHashes []string
546 if err == nil {
547 depsetHashes, err = a.depsetContentHashes(actionEntry.InputDepSetIds)
548 }
549 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500550 return nil, err
Sasha Smundak1da064c2022-06-08 16:36:16 -0700551 }
Liz Kammer00629db2023-02-09 14:28:15 -0500552 return &BuildStatement{
Sasha Smundak1da064c2022-06-08 16:36:16 -0700553 Depfile: nil,
554 OutputPaths: outputPaths,
555 Env: actionEntry.EnvironmentVariables,
556 Mnemonic: actionEntry.Mnemonic,
557 InputDepsetHashes: depsetHashes,
558 FileContents: actionEntry.FileContents,
559 }, nil
560}
561
Liz Kammer00629db2023-02-09 14:28:15 -0500562func (a *aqueryArtifactHandler) symlinkTreeActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) {
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700563 outputPaths, _, err := a.getOutputPaths(actionEntry)
564 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500565 return nil, err
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700566 }
567 inputPaths, err := a.getInputPaths(actionEntry.InputDepSetIds)
568 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500569 return nil, err
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700570 }
571 if len(inputPaths) != 1 || len(outputPaths) != 1 {
Liz Kammer00629db2023-02-09 14:28:15 -0500572 return nil, fmt.Errorf("Expect 1 input and 1 output to symlink action, got: input %q, output %q", inputPaths, outputPaths)
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700573 }
574 // The actual command is generated in bazelSingleton.GenerateBuildActions
Liz Kammer00629db2023-02-09 14:28:15 -0500575 return &BuildStatement{
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700576 Depfile: nil,
577 OutputPaths: outputPaths,
578 Env: actionEntry.EnvironmentVariables,
579 Mnemonic: actionEntry.Mnemonic,
580 InputPaths: inputPaths,
581 }, nil
582}
583
Liz Kammer00629db2023-02-09 14:28:15 -0500584func (a *aqueryArtifactHandler) symlinkActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) {
Usta Shresthac2372492022-05-27 10:45:00 -0400585 outputPaths, depfile, err := a.getOutputPaths(actionEntry)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400586 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500587 return nil, err
Chris Parsons1a7aca02022-04-25 22:35:15 -0400588 }
589
Usta Shresthac2372492022-05-27 10:45:00 -0400590 inputPaths, err := a.getInputPaths(actionEntry.InputDepSetIds)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400591 if err != nil {
Liz Kammer00629db2023-02-09 14:28:15 -0500592 return nil, err
Chris Parsons1a7aca02022-04-25 22:35:15 -0400593 }
594 if len(inputPaths) != 1 || len(outputPaths) != 1 {
Liz Kammer00629db2023-02-09 14:28:15 -0500595 return nil, fmt.Errorf("Expect 1 input and 1 output to symlink action, got: input %q, output %q", inputPaths, outputPaths)
Chris Parsons1a7aca02022-04-25 22:35:15 -0400596 }
597 out := outputPaths[0]
598 outDir := proptools.ShellEscapeIncludingSpaces(filepath.Dir(out))
599 out = proptools.ShellEscapeIncludingSpaces(out)
600 in := filepath.Join("$PWD", proptools.ShellEscapeIncludingSpaces(inputPaths[0]))
601 // Use absolute paths, because some soong actions don't play well with relative paths (for example, `cp -d`).
602 command := fmt.Sprintf("mkdir -p %[1]s && rm -f %[2]s && ln -sf %[3]s %[2]s", outDir, out, in)
603 symlinkPaths := outputPaths[:]
604
Liz Kammer00629db2023-02-09 14:28:15 -0500605 buildStatement := &BuildStatement{
Chris Parsons1a7aca02022-04-25 22:35:15 -0400606 Command: command,
607 Depfile: depfile,
608 OutputPaths: outputPaths,
609 InputPaths: inputPaths,
610 Env: actionEntry.EnvironmentVariables,
611 Mnemonic: actionEntry.Mnemonic,
612 SymlinkPaths: symlinkPaths,
613 }
614 return buildStatement, nil
615}
616
Liz Kammer00629db2023-02-09 14:28:15 -0500617func (a *aqueryArtifactHandler) getOutputPaths(actionEntry *analysis_v2_proto.Action) (outputPaths []string, depfile *string, err error) {
Chris Parsons1a7aca02022-04-25 22:35:15 -0400618 for _, outputId := range actionEntry.OutputIds {
Liz Kammer00629db2023-02-09 14:28:15 -0500619 outputPath, exists := a.artifactIdToPath[artifactId(outputId)]
Chris Parsons1a7aca02022-04-25 22:35:15 -0400620 if !exists {
621 err = fmt.Errorf("undefined outputId %d", outputId)
622 return
623 }
624 ext := filepath.Ext(outputPath)
625 if ext == ".d" {
626 if depfile != nil {
627 err = fmt.Errorf("found multiple potential depfiles %q, %q", *depfile, outputPath)
628 return
629 } else {
630 depfile = &outputPath
631 }
632 } else {
633 outputPaths = append(outputPaths, outputPath)
634 }
635 }
636 return
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -0500637}
Chris Parsonsaffbb602020-12-23 12:02:11 -0500638
Wei Li455ba832021-11-04 22:58:12 +0000639// expandTemplateContent substitutes the tokens in a template.
Liz Kammer00629db2023-02-09 14:28:15 -0500640func expandTemplateContent(actionEntry *analysis_v2_proto.Action) string {
641 replacerString := make([]string, len(actionEntry.Substitutions)*2)
642 for i, pair := range actionEntry.Substitutions {
Wei Li455ba832021-11-04 22:58:12 +0000643 value := pair.Value
Usta Shrestha6298cc52022-05-27 17:40:21 -0400644 if val, ok := templateActionOverriddenTokens[pair.Key]; ok {
Wei Li455ba832021-11-04 22:58:12 +0000645 value = val
646 }
Liz Kammer00629db2023-02-09 14:28:15 -0500647 replacerString[i*2] = pair.Key
648 replacerString[i*2+1] = value
Wei Li455ba832021-11-04 22:58:12 +0000649 }
650 replacer := strings.NewReplacer(replacerString...)
651 return replacer.Replace(actionEntry.TemplateContent)
652}
653
Liz Kammerf15a0792023-02-09 14:28:36 -0500654// \->\\, $->\$, `->\`, "->\", \n->\\n, '->'"'"'
655var commandLineArgumentReplacer = strings.NewReplacer(
656 `\`, `\\`,
657 `$`, `\$`,
658 "`", "\\`",
659 `"`, `\"`,
660 "\n", "\\n",
661 `'`, `'"'"'`,
662)
663
Wei Li455ba832021-11-04 22:58:12 +0000664func escapeCommandlineArgument(str string) string {
Liz Kammerf15a0792023-02-09 14:28:36 -0500665 return commandLineArgumentReplacer.Replace(str)
Wei Li455ba832021-11-04 22:58:12 +0000666}
667
Liz Kammer00629db2023-02-09 14:28:15 -0500668func (a *aqueryArtifactHandler) actionToBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) {
669 switch actionEntry.Mnemonic {
Chris Parsonsc4fb1332021-05-18 12:31:25 -0400670 // Middleman actions are not handled like other actions; they are handled separately as a
671 // preparatory step so that their inputs may be relayed to actions depending on middleman
672 // artifacts.
Liz Kammer00629db2023-02-09 14:28:15 -0500673 case middlemanMnemonic:
674 return nil, nil
Sasha Smundakc180dbd2022-07-03 14:55:58 -0700675 // PythonZipper is bogus action returned by aquery, ignore it (b/236198693)
Liz Kammer00629db2023-02-09 14:28:15 -0500676 case "PythonZipper":
677 return nil, nil
Chris Parsons8d6e4332021-02-22 16:13:50 -0500678 // Skip "Fail" actions, which are placeholder actions designed to always fail.
Liz Kammer00629db2023-02-09 14:28:15 -0500679 case "Fail":
680 return nil, nil
681 case "BaselineCoverage":
682 return nil, nil
683 case "Symlink", "SolibSymlink", "ExecutableSymlink":
684 return a.symlinkActionBuildStatement(actionEntry)
685 case "TemplateExpand":
686 if len(actionEntry.Arguments) < 1 {
687 return a.templateExpandActionBuildStatement(actionEntry)
688 }
Cole Faust950689a2023-06-21 15:07:21 -0700689 case "FileWrite", "SourceSymlinkManifest", "RepoMappingManifest":
Liz Kammer00629db2023-02-09 14:28:15 -0500690 return a.fileWriteActionBuildStatement(actionEntry)
691 case "SymlinkTree":
692 return a.symlinkTreeActionBuildStatement(actionEntry)
Chris Parsons8d6e4332021-02-22 16:13:50 -0500693 }
Liz Kammer00629db2023-02-09 14:28:15 -0500694
695 if len(actionEntry.Arguments) < 1 {
696 return nil, fmt.Errorf("received action with no command: [%s]", actionEntry.Mnemonic)
Yu Liu8d82ac52022-05-17 15:13:28 -0700697 }
Liz Kammer00629db2023-02-09 14:28:15 -0500698 return a.normalActionBuildStatement(actionEntry)
699
Chris Parsons8d6e4332021-02-22 16:13:50 -0500700}
701
Liz Kammer00629db2023-02-09 14:28:15 -0500702func expandPathFragment(id pathFragmentId, pathFragmentsMap map[pathFragmentId]*analysis_v2_proto.PathFragment) (string, error) {
Usta Shrestha6298cc52022-05-27 17:40:21 -0400703 var labels []string
Chris Parsonsaffbb602020-12-23 12:02:11 -0500704 currId := id
705 // Only positive IDs are valid for path fragments. An ID of zero indicates a terminal node.
706 for currId > 0 {
707 currFragment, ok := pathFragmentsMap[currId]
708 if !ok {
Chris Parsons4f069892021-01-15 12:22:41 -0500709 return "", fmt.Errorf("undefined path fragment id %d", currId)
Chris Parsonsaffbb602020-12-23 12:02:11 -0500710 }
711 labels = append([]string{currFragment.Label}, labels...)
Liz Kammer00629db2023-02-09 14:28:15 -0500712 parentId := pathFragmentId(currFragment.ParentId)
713 if currId == parentId {
Sasha Smundakfe9a5b82022-07-27 14:51:45 -0700714 return "", fmt.Errorf("fragment cannot refer to itself as parent %#v", currFragment)
Liz Kammerc49e6822021-06-08 15:04:11 -0400715 }
Liz Kammer00629db2023-02-09 14:28:15 -0500716 currId = parentId
Chris Parsonsaffbb602020-12-23 12:02:11 -0500717 }
718 return filepath.Join(labels...), nil
719}