Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package bazel |
| 16 | |
| 17 | import ( |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 18 | "crypto/sha256" |
Usta Shrestha | 2ccdb42 | 2022-06-02 10:19:13 -0400 | [diff] [blame] | 19 | "encoding/base64" |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 20 | "encoding/json" |
usta | a79afd7 | 2023-09-22 17:29:56 -0400 | [diff] [blame] | 21 | "errors" |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 22 | "fmt" |
| 23 | "path/filepath" |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 24 | "reflect" |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 25 | "sort" |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 26 | "strings" |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 27 | "sync" |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 28 | |
usta | a79afd7 | 2023-09-22 17:29:56 -0400 | [diff] [blame] | 29 | analysis_v2_proto "prebuilts/bazel/common/proto/analysis_v2" |
| 30 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 31 | "github.com/google/blueprint/metrics" |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 32 | "github.com/google/blueprint/proptools" |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 33 | "google.golang.org/protobuf/proto" |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 34 | ) |
| 35 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 36 | type artifactId int |
| 37 | type depsetId int |
| 38 | type pathFragmentId int |
| 39 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 40 | // artifact contains relevant portions of Bazel's aquery proto, Artifact. |
| 41 | // Represents a single artifact, whether it's a source file or a derived output file. |
| 42 | type artifact struct { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 43 | Id artifactId |
| 44 | PathFragmentId pathFragmentId |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | type pathFragment struct { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 48 | Id pathFragmentId |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 49 | Label string |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 50 | ParentId pathFragmentId |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // KeyValuePair represents Bazel's aquery proto, KeyValuePair. |
| 54 | type KeyValuePair struct { |
| 55 | Key string |
| 56 | Value string |
| 57 | } |
| 58 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 59 | // AqueryDepset is a depset definition from Bazel's aquery response. This is |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 60 | // akin to the `depSetOfFiles` in the response proto, except: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 61 | // - direct artifacts are enumerated by full path instead of by ID |
| 62 | // - it has a hash of the depset contents, instead of an int ID (for determinism) |
| 63 | // |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 64 | // A depset is a data structure for efficient transitive handling of artifact |
| 65 | // paths. A single depset consists of one or more artifact paths and one or |
| 66 | // more "child" depsets. |
| 67 | type AqueryDepset struct { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 68 | ContentHash string |
| 69 | DirectArtifacts []string |
| 70 | TransitiveDepSetHashes []string |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 73 | // depSetOfFiles contains relevant portions of Bazel's aquery proto, DepSetOfFiles. |
| 74 | // Represents a data structure containing one or more files. Depsets in Bazel are an efficient |
| 75 | // data structure for storing large numbers of file paths. |
| 76 | type depSetOfFiles struct { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 77 | Id depsetId |
| 78 | DirectArtifactIds []artifactId |
| 79 | TransitiveDepSetIds []depsetId |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // action contains relevant portions of Bazel's aquery proto, Action. |
| 83 | // Represents a single command line invocation in the Bazel build graph. |
| 84 | type action struct { |
| 85 | Arguments []string |
| 86 | EnvironmentVariables []KeyValuePair |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 87 | InputDepSetIds []depsetId |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 88 | Mnemonic string |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 89 | OutputIds []artifactId |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 90 | TemplateContent string |
| 91 | Substitutions []KeyValuePair |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 92 | FileContents string |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // actionGraphContainer contains relevant portions of Bazel's aquery proto, ActionGraphContainer. |
| 96 | // An aquery response from Bazel contains a single ActionGraphContainer proto. |
| 97 | type actionGraphContainer struct { |
| 98 | Artifacts []artifact |
| 99 | Actions []action |
| 100 | DepSetOfFiles []depSetOfFiles |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 101 | PathFragments []pathFragment |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // BuildStatement contains information to register a build statement corresponding (one to one) |
| 105 | // with a Bazel action from Bazel's action graph. |
| 106 | type BuildStatement struct { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 107 | Command string |
| 108 | Depfile *string |
| 109 | OutputPaths []string |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 110 | SymlinkPaths []string |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 111 | Env []*analysis_v2_proto.KeyValuePair |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 112 | Mnemonic string |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 113 | |
| 114 | // Inputs of this build statement, either as unexpanded depsets or expanded |
| 115 | // input paths. There should be no overlap between these fields; an input |
| 116 | // path should either be included as part of an unexpanded depset or a raw |
| 117 | // input path string, but not both. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 118 | InputDepsetHashes []string |
| 119 | InputPaths []string |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 120 | FileContents string |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 121 | // If ShouldRunInSbox is true, Soong will use sbox to created an isolated environment |
| 122 | // and run the mixed build action there |
| 123 | ShouldRunInSbox bool |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 124 | // A list of files to add as implicit deps to the outputs of this BuildStatement. |
| 125 | // Unlike most properties in BuildStatement, these paths must be relative to the root of |
| 126 | // the whole out/ folder, instead of relative to ctx.Config().BazelContext.OutputBase() |
| 127 | ImplicitDeps []string |
Cole Faust | 20f2030 | 2023-08-31 11:00:25 -0700 | [diff] [blame] | 128 | IsExecutable bool |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 131 | // A helper type for aquery processing which facilitates retrieval of path IDs from their |
| 132 | // less readable Bazel structures (depset and path fragment). |
| 133 | type aqueryArtifactHandler struct { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 134 | // Maps depset id to AqueryDepset, a representation of depset which is |
| 135 | // post-processed for middleman artifact handling, unhandled artifact |
| 136 | // dropping, content hashing, etc. |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 137 | depsetIdToAqueryDepset map[depsetId]AqueryDepset |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 138 | emptyDepsetIds map[depsetId]struct{} |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 139 | // Maps content hash to AqueryDepset. |
| 140 | depsetHashToAqueryDepset map[string]AqueryDepset |
| 141 | |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 142 | // depsetIdToArtifactIdsCache is a memoization of depset flattening, because flattening |
| 143 | // may be an expensive operation. |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 144 | depsetHashToArtifactPathsCache sync.Map |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 145 | // Maps artifact ids to fully expanded paths. |
| 146 | artifactIdToPath map[artifactId]string |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 147 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 148 | |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 149 | // The tokens should be substituted with the value specified here, instead of the |
| 150 | // one returned in 'substitutions' of TemplateExpand action. |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 151 | var templateActionOverriddenTokens = map[string]string{ |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 152 | // Uses "python3" for %python_binary% instead of the value returned by aquery |
| 153 | // which is "py3wrapper.sh". See removePy3wrapperScript. |
| 154 | "%python_binary%": "python3", |
| 155 | } |
| 156 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 157 | const ( |
| 158 | middlemanMnemonic = "Middleman" |
| 159 | // The file name of py3wrapper.sh, which is used by py_binary targets. |
| 160 | py3wrapperFileName = "/py3wrapper.sh" |
| 161 | ) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 162 | |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 163 | func indexBy[K comparable, V any](values []V, keyFn func(v V) K) map[K]V { |
| 164 | m := map[K]V{} |
| 165 | for _, v := range values { |
| 166 | m[keyFn(v)] = v |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 167 | } |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 168 | return m |
| 169 | } |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 170 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 171 | func newAqueryHandler(aqueryResult *analysis_v2_proto.ActionGraphContainer) (*aqueryArtifactHandler, error) { |
| 172 | pathFragments := indexBy(aqueryResult.PathFragments, func(pf *analysis_v2_proto.PathFragment) pathFragmentId { |
| 173 | return pathFragmentId(pf.Id) |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 174 | }) |
| 175 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 176 | artifactIdToPath := make(map[artifactId]string, len(aqueryResult.Artifacts)) |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 177 | for _, artifact := range aqueryResult.Artifacts { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 178 | artifactPath, err := expandPathFragment(pathFragmentId(artifact.PathFragmentId), pathFragments) |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 179 | if err != nil { |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 180 | return nil, err |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 181 | } |
Cole Faust | bdee8f9 | 2023-09-07 14:28:51 -0700 | [diff] [blame] | 182 | if artifact.IsTreeArtifact && |
| 183 | !strings.HasPrefix(artifactPath, "bazel-out/io_bazel_rules_go/") && |
| 184 | !strings.HasPrefix(artifactPath, "bazel-out/rules_java_builtin/") { |
| 185 | // Since we're using ninja as an executor, we can't use tree artifacts. Ninja only |
| 186 | // considers a file/directory "dirty" when it's mtime changes. Directories' mtimes will |
| 187 | // only change when a file in the directory is added/removed, but not when files in |
| 188 | // the directory are changed, or when files in subdirectories are changed/added/removed. |
| 189 | // Bazel handles this by walking the directory and generating a hash for it after the |
| 190 | // action runs, which we would have to do as well if we wanted to support these |
| 191 | // artifacts in mixed builds. |
| 192 | // |
| 193 | // However, there are some bazel built-in rules that use tree artifacts. Allow those, |
| 194 | // but keep in mind that they'll have incrementality issues. |
| 195 | return nil, fmt.Errorf("tree artifacts are currently not supported in mixed builds: " + artifactPath) |
| 196 | } |
Romain Jobredeaux | e3989a1 | 2023-07-19 20:58:27 +0000 | [diff] [blame] | 197 | artifactIdToPath[artifactId(artifact.Id)] = artifactPath |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 198 | } |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 199 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 200 | // Map middleman artifact ContentHash to input artifact depset ID. |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 201 | // Middleman artifacts are treated as "substitute" artifacts for mixed builds. For example, |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 202 | // if we find a middleman action which has inputs [foo, bar], and output [baz_middleman], then, |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 203 | // for each other action which has input [baz_middleman], we add [foo, bar] to the inputs for |
| 204 | // that action instead. |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 205 | middlemanIdToDepsetIds := map[artifactId][]uint32{} |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 206 | for _, actionEntry := range aqueryResult.Actions { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 207 | if actionEntry.Mnemonic == middlemanMnemonic { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 208 | for _, outputId := range actionEntry.OutputIds { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 209 | middlemanIdToDepsetIds[artifactId(outputId)] = actionEntry.InputDepSetIds |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 213 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 214 | depsetIdToDepset := indexBy(aqueryResult.DepSetOfFiles, func(d *analysis_v2_proto.DepSetOfFiles) depsetId { |
| 215 | return depsetId(d.Id) |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 216 | }) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 217 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 218 | aqueryHandler := aqueryArtifactHandler{ |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 219 | depsetIdToAqueryDepset: map[depsetId]AqueryDepset{}, |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 220 | depsetHashToAqueryDepset: map[string]AqueryDepset{}, |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 221 | depsetHashToArtifactPathsCache: sync.Map{}, |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 222 | emptyDepsetIds: make(map[depsetId]struct{}, 0), |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 223 | artifactIdToPath: artifactIdToPath, |
| 224 | } |
| 225 | |
| 226 | // Validate and adjust aqueryResult.DepSetOfFiles values. |
| 227 | for _, depset := range aqueryResult.DepSetOfFiles { |
| 228 | _, err := aqueryHandler.populateDepsetMaps(depset, middlemanIdToDepsetIds, depsetIdToDepset) |
| 229 | if err != nil { |
| 230 | return nil, err |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return &aqueryHandler, nil |
| 235 | } |
| 236 | |
| 237 | // Ensures that the handler's depsetIdToAqueryDepset map contains an entry for the given |
| 238 | // depset. |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 239 | func (a *aqueryArtifactHandler) populateDepsetMaps(depset *analysis_v2_proto.DepSetOfFiles, middlemanIdToDepsetIds map[artifactId][]uint32, depsetIdToDepset map[depsetId]*analysis_v2_proto.DepSetOfFiles) (*AqueryDepset, error) { |
| 240 | if aqueryDepset, containsDepset := a.depsetIdToAqueryDepset[depsetId(depset.Id)]; containsDepset { |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 241 | return &aqueryDepset, nil |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 242 | } |
| 243 | transitiveDepsetIds := depset.TransitiveDepSetIds |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 244 | directArtifactPaths := make([]string, 0, len(depset.DirectArtifactIds)) |
| 245 | for _, id := range depset.DirectArtifactIds { |
| 246 | aId := artifactId(id) |
| 247 | path, pathExists := a.artifactIdToPath[aId] |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 248 | if !pathExists { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 249 | return nil, fmt.Errorf("undefined input artifactId %d", aId) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 250 | } |
| 251 | // Filter out any inputs which are universally dropped, and swap middleman |
| 252 | // artifacts with their corresponding depsets. |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 253 | if depsetsToUse, isMiddleman := middlemanIdToDepsetIds[aId]; isMiddleman { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 254 | // Swap middleman artifacts with their corresponding depsets and drop the middleman artifacts. |
| 255 | transitiveDepsetIds = append(transitiveDepsetIds, depsetsToUse...) |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 256 | } else if strings.HasSuffix(path, py3wrapperFileName) || |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 257 | strings.HasPrefix(path, "../bazel_tools") { |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 258 | continue |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 259 | // Drop these artifacts. |
| 260 | // See go/python-binary-host-mixed-build for more details. |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 261 | // 1) Drop py3wrapper.sh, just use python binary, the launcher script generated by the |
| 262 | // TemplateExpandAction handles everything necessary to launch a Pythin application. |
| 263 | // 2) ../bazel_tools: they have MODIFY timestamp 10years in the future and would cause the |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 264 | // containing depset to always be considered newer than their outputs. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 265 | } else { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 266 | directArtifactPaths = append(directArtifactPaths, path) |
| 267 | } |
| 268 | } |
| 269 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 270 | childDepsetHashes := make([]string, 0, len(transitiveDepsetIds)) |
| 271 | for _, id := range transitiveDepsetIds { |
| 272 | childDepsetId := depsetId(id) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 273 | childDepset, exists := depsetIdToDepset[childDepsetId] |
| 274 | if !exists { |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 275 | if _, empty := a.emptyDepsetIds[childDepsetId]; empty { |
| 276 | continue |
| 277 | } else { |
| 278 | return nil, fmt.Errorf("undefined input depsetId %d (referenced by depsetId %d)", childDepsetId, depset.Id) |
| 279 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 280 | } |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 281 | if childAqueryDepset, err := a.populateDepsetMaps(childDepset, middlemanIdToDepsetIds, depsetIdToDepset); err != nil { |
| 282 | return nil, err |
| 283 | } else if childAqueryDepset == nil { |
| 284 | continue |
| 285 | } else { |
| 286 | childDepsetHashes = append(childDepsetHashes, childAqueryDepset.ContentHash) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 287 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 288 | } |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 289 | if len(directArtifactPaths) == 0 && len(childDepsetHashes) == 0 { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 290 | a.emptyDepsetIds[depsetId(depset.Id)] = struct{}{} |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 291 | return nil, nil |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 292 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 293 | aqueryDepset := AqueryDepset{ |
| 294 | ContentHash: depsetContentHash(directArtifactPaths, childDepsetHashes), |
| 295 | DirectArtifacts: directArtifactPaths, |
| 296 | TransitiveDepSetHashes: childDepsetHashes, |
| 297 | } |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 298 | a.depsetIdToAqueryDepset[depsetId(depset.Id)] = aqueryDepset |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 299 | a.depsetHashToAqueryDepset[aqueryDepset.ContentHash] = aqueryDepset |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 300 | return &aqueryDepset, nil |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 303 | // getInputPaths flattens the depsets of the given IDs and returns all transitive |
| 304 | // input paths contained in these depsets. |
| 305 | // This is a potentially expensive operation, and should not be invoked except |
| 306 | // for actions which need specialized input handling. |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 307 | func (a *aqueryArtifactHandler) getInputPaths(depsetIds []uint32) ([]string, error) { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 308 | var inputPaths []string |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 309 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 310 | for _, id := range depsetIds { |
| 311 | inputDepSetId := depsetId(id) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 312 | depset := a.depsetIdToAqueryDepset[inputDepSetId] |
| 313 | inputArtifacts, err := a.artifactPathsFromDepsetHash(depset.ContentHash) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 314 | if err != nil { |
| 315 | return nil, err |
| 316 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 317 | for _, inputPath := range inputArtifacts { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 318 | inputPaths = append(inputPaths, inputPath) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 319 | } |
| 320 | } |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 321 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 322 | return inputPaths, nil |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 325 | func (a *aqueryArtifactHandler) artifactPathsFromDepsetHash(depsetHash string) ([]string, error) { |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 326 | if result, exists := a.depsetHashToArtifactPathsCache.Load(depsetHash); exists { |
| 327 | return result.([]string), nil |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 328 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 329 | if depset, exists := a.depsetHashToAqueryDepset[depsetHash]; exists { |
| 330 | result := depset.DirectArtifacts |
| 331 | for _, childHash := range depset.TransitiveDepSetHashes { |
| 332 | childArtifactIds, err := a.artifactPathsFromDepsetHash(childHash) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 333 | if err != nil { |
| 334 | return nil, err |
| 335 | } |
| 336 | result = append(result, childArtifactIds...) |
| 337 | } |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 338 | a.depsetHashToArtifactPathsCache.Store(depsetHash, result) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 339 | return result, nil |
| 340 | } else { |
Usta Shrestha | 2ccdb42 | 2022-06-02 10:19:13 -0400 | [diff] [blame] | 341 | return nil, fmt.Errorf("undefined input depset hash %s", depsetHash) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 345 | // AqueryBuildStatements returns a slice of BuildStatements and a slice of AqueryDepset |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 346 | // which should be registered (and output to a ninja file) to correspond with Bazel's |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 347 | // action graph, as described by the given action graph json proto. |
| 348 | // BuildStatements are one-to-one with actions in the given action graph, and AqueryDepsets |
| 349 | // are one-to-one with Bazel's depSetOfFiles objects. |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 350 | func AqueryBuildStatements(aqueryJsonProto []byte, eventHandler *metrics.EventHandler) ([]*BuildStatement, []AqueryDepset, error) { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 351 | aqueryProto := &analysis_v2_proto.ActionGraphContainer{} |
| 352 | err := proto.Unmarshal(aqueryJsonProto, aqueryProto) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 353 | if err != nil { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 354 | return nil, nil, err |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 355 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 356 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 357 | var aqueryHandler *aqueryArtifactHandler |
| 358 | { |
| 359 | eventHandler.Begin("init_handler") |
| 360 | defer eventHandler.End("init_handler") |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 361 | aqueryHandler, err = newAqueryHandler(aqueryProto) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 362 | if err != nil { |
| 363 | return nil, nil, err |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 364 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 365 | } |
| 366 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 367 | // allocate both length and capacity so each goroutine can write to an index independently without |
| 368 | // any need for synchronization for slice access. |
| 369 | buildStatements := make([]*BuildStatement, len(aqueryProto.Actions)) |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 370 | { |
| 371 | eventHandler.Begin("build_statements") |
| 372 | defer eventHandler.End("build_statements") |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 373 | wg := sync.WaitGroup{} |
| 374 | var errOnce sync.Once |
| 375 | |
| 376 | for i, actionEntry := range aqueryProto.Actions { |
| 377 | wg.Add(1) |
| 378 | go func(i int, actionEntry *analysis_v2_proto.Action) { |
usta | a79afd7 | 2023-09-22 17:29:56 -0400 | [diff] [blame] | 379 | if buildStatement, aErr := aqueryHandler.actionToBuildStatement(actionEntry); aErr != nil { |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 380 | errOnce.Do(func() { |
usta | a79afd7 | 2023-09-22 17:29:56 -0400 | [diff] [blame] | 381 | for _, t := range aqueryProto.Targets { |
| 382 | if t.GetId() == actionEntry.GetTargetId() { |
| 383 | aErr = fmt.Errorf("%s: [%s] [%s]", aErr.Error(), actionEntry.GetMnemonic(), t.GetLabel()) |
| 384 | break |
| 385 | } |
| 386 | } |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 387 | err = aErr |
| 388 | }) |
| 389 | } else { |
| 390 | // set build statement at an index rather than appending such that each goroutine does not |
| 391 | // impact other goroutines |
| 392 | buildStatements[i] = buildStatement |
| 393 | } |
| 394 | wg.Done() |
| 395 | }(i, actionEntry) |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 396 | } |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 397 | wg.Wait() |
| 398 | } |
| 399 | if err != nil { |
| 400 | return nil, nil, err |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 401 | } |
| 402 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 403 | depsetsByHash := map[string]AqueryDepset{} |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 404 | depsets := make([]AqueryDepset, 0, len(aqueryHandler.depsetIdToAqueryDepset)) |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 405 | { |
| 406 | eventHandler.Begin("depsets") |
| 407 | defer eventHandler.End("depsets") |
| 408 | for _, aqueryDepset := range aqueryHandler.depsetIdToAqueryDepset { |
| 409 | if prevEntry, hasKey := depsetsByHash[aqueryDepset.ContentHash]; hasKey { |
| 410 | // Two depsets collide on hash. Ensure that their contents are identical. |
| 411 | if !reflect.DeepEqual(aqueryDepset, prevEntry) { |
| 412 | return nil, nil, fmt.Errorf("two different depsets have the same hash: %v, %v", prevEntry, aqueryDepset) |
| 413 | } |
| 414 | } else { |
| 415 | depsetsByHash[aqueryDepset.ContentHash] = aqueryDepset |
| 416 | depsets = append(depsets, aqueryDepset) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 417 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 418 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 419 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 420 | |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 421 | eventHandler.Do("build_statement_sort", func() { |
| 422 | // Build Statements and depsets must be sorted by their content hash to |
| 423 | // preserve determinism between builds (this will result in consistent ninja file |
| 424 | // output). Note they are not sorted by their original IDs nor their Bazel ordering, |
| 425 | // as Bazel gives nondeterministic ordering / identifiers in aquery responses. |
| 426 | sort.Slice(buildStatements, func(i, j int) bool { |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 427 | // Sort all nil statements to the end of the slice |
| 428 | if buildStatements[i] == nil { |
| 429 | return false |
| 430 | } else if buildStatements[j] == nil { |
| 431 | return true |
| 432 | } |
| 433 | //For build statements, compare output lists. In Bazel, each output file |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 434 | // may only have one action which generates it, so this will provide |
| 435 | // a deterministic ordering. |
| 436 | outputs_i := buildStatements[i].OutputPaths |
| 437 | outputs_j := buildStatements[j].OutputPaths |
| 438 | if len(outputs_i) != len(outputs_j) { |
| 439 | return len(outputs_i) < len(outputs_j) |
| 440 | } |
| 441 | if len(outputs_i) == 0 { |
| 442 | // No outputs for these actions, so compare commands. |
| 443 | return buildStatements[i].Command < buildStatements[j].Command |
| 444 | } |
| 445 | // There may be multiple outputs, but the output ordering is deterministic. |
| 446 | return outputs_i[0] < outputs_j[0] |
| 447 | }) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 448 | }) |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 449 | eventHandler.Do("depset_sort", func() { |
| 450 | sort.Slice(depsets, func(i, j int) bool { |
| 451 | return depsets[i].ContentHash < depsets[j].ContentHash |
| 452 | }) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 453 | }) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 454 | return buildStatements, depsets, nil |
| 455 | } |
| 456 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 457 | // depsetContentHash computes and returns a SHA256 checksum of the contents of |
| 458 | // the given depset. This content hash may serve as the depset's identifier. |
| 459 | // Using a content hash for an identifier is superior for determinism. (For example, |
| 460 | // using an integer identifier which depends on the order in which the depsets are |
| 461 | // created would result in nondeterministic depset IDs.) |
| 462 | func depsetContentHash(directPaths []string, transitiveDepsetHashes []string) string { |
| 463 | h := sha256.New() |
| 464 | // Use newline as delimiter, as paths cannot contain newline. |
| 465 | h.Write([]byte(strings.Join(directPaths, "\n"))) |
Usta Shrestha | 2ccdb42 | 2022-06-02 10:19:13 -0400 | [diff] [blame] | 466 | h.Write([]byte(strings.Join(transitiveDepsetHashes, ""))) |
| 467 | fullHash := base64.RawURLEncoding.EncodeToString(h.Sum(nil)) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 468 | return fullHash |
| 469 | } |
| 470 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 471 | func (a *aqueryArtifactHandler) depsetContentHashes(inputDepsetIds []uint32) ([]string, error) { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 472 | var hashes []string |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 473 | for _, id := range inputDepsetIds { |
| 474 | dId := depsetId(id) |
| 475 | if aqueryDepset, exists := a.depsetIdToAqueryDepset[dId]; !exists { |
| 476 | if _, empty := a.emptyDepsetIds[dId]; !empty { |
| 477 | return nil, fmt.Errorf("undefined (not even empty) input depsetId %d", dId) |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 478 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 479 | } else { |
| 480 | hashes = append(hashes, aqueryDepset.ContentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 481 | } |
| 482 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 483 | return hashes, nil |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 484 | } |
| 485 | |
Spandan Das | da72486 | 2023-06-16 23:35:55 +0000 | [diff] [blame] | 486 | // escapes the args received from aquery and creates a command string |
| 487 | func commandString(actionEntry *analysis_v2_proto.Action) string { |
| 488 | switch actionEntry.Mnemonic { |
Spandan Das | 2d93ebb | 2023-07-27 23:46:24 +0000 | [diff] [blame] | 489 | case "GoCompilePkg", "GoStdlib": |
Spandan Das | da72486 | 2023-06-16 23:35:55 +0000 | [diff] [blame] | 490 | argsEscaped := []string{} |
| 491 | for _, arg := range actionEntry.Arguments { |
| 492 | if arg == "" { |
| 493 | // If this is an empty string, add '' |
| 494 | // And not |
| 495 | // 1. (literal empty) |
| 496 | // 2. `''\'''\'''` (escaped version of '') |
| 497 | // |
| 498 | // If we had used (1), then this would appear as a whitespace when we strings.Join |
| 499 | argsEscaped = append(argsEscaped, "''") |
| 500 | } else { |
| 501 | argsEscaped = append(argsEscaped, proptools.ShellEscapeIncludingSpaces(arg)) |
| 502 | } |
| 503 | } |
| 504 | return strings.Join(argsEscaped, " ") |
| 505 | default: |
| 506 | return strings.Join(proptools.ShellEscapeListIncludingSpaces(actionEntry.Arguments), " ") |
| 507 | } |
| 508 | } |
| 509 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 510 | func (a *aqueryArtifactHandler) normalActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) { |
Spandan Das | da72486 | 2023-06-16 23:35:55 +0000 | [diff] [blame] | 511 | command := commandString(actionEntry) |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 512 | inputDepsetHashes, err := a.depsetContentHashes(actionEntry.InputDepSetIds) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 513 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 514 | return nil, err |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 515 | } |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 516 | outputPaths, depfile, err := a.getOutputPaths(actionEntry) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 517 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 518 | return nil, err |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 519 | } |
| 520 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 521 | buildStatement := &BuildStatement{ |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 522 | Command: command, |
| 523 | Depfile: depfile, |
| 524 | OutputPaths: outputPaths, |
| 525 | InputDepsetHashes: inputDepsetHashes, |
| 526 | Env: actionEntry.EnvironmentVariables, |
| 527 | Mnemonic: actionEntry.Mnemonic, |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 528 | } |
Spandan Das | af4ccaa | 2023-06-29 01:15:51 +0000 | [diff] [blame] | 529 | if buildStatement.Mnemonic == "GoToolchainBinaryBuild" { |
| 530 | // Unlike b's execution root, mixed build execution root contains a symlink to prebuilts/go |
| 531 | // This causes issues for `GOCACHE=$(mktemp -d) go build ...` |
| 532 | // To prevent this, sandbox this action in mixed builds as well |
| 533 | buildStatement.ShouldRunInSbox = true |
| 534 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 535 | return buildStatement, nil |
| 536 | } |
| 537 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 538 | func (a *aqueryArtifactHandler) templateExpandActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) { |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 539 | outputPaths, depfile, err := a.getOutputPaths(actionEntry) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 540 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 541 | return nil, err |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 542 | } |
| 543 | if len(outputPaths) != 1 { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 544 | return nil, fmt.Errorf("Expect 1 output to template expand action, got: output %q", outputPaths) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 545 | } |
| 546 | expandedTemplateContent := expandTemplateContent(actionEntry) |
| 547 | // The expandedTemplateContent is escaped for being used in double quotes and shell unescape, |
| 548 | // and the new line characters (\n) are also changed to \\n which avoids some Ninja escape on \n, which might |
| 549 | // change \n to space and mess up the format of Python programs. |
| 550 | // sed is used to convert \\n back to \n before saving to output file. |
| 551 | // See go/python-binary-host-mixed-build for more details. |
| 552 | command := fmt.Sprintf(`/bin/bash -c 'echo "%[1]s" | sed "s/\\\\n/\\n/g" > %[2]s && chmod a+x %[2]s'`, |
| 553 | escapeCommandlineArgument(expandedTemplateContent), outputPaths[0]) |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 554 | inputDepsetHashes, err := a.depsetContentHashes(actionEntry.InputDepSetIds) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 555 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 556 | return nil, err |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 557 | } |
| 558 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 559 | buildStatement := &BuildStatement{ |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 560 | Command: command, |
| 561 | Depfile: depfile, |
| 562 | OutputPaths: outputPaths, |
| 563 | InputDepsetHashes: inputDepsetHashes, |
| 564 | Env: actionEntry.EnvironmentVariables, |
| 565 | Mnemonic: actionEntry.Mnemonic, |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 566 | } |
| 567 | return buildStatement, nil |
| 568 | } |
| 569 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 570 | func (a *aqueryArtifactHandler) fileWriteActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) { |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 571 | outputPaths, _, err := a.getOutputPaths(actionEntry) |
| 572 | var depsetHashes []string |
| 573 | if err == nil { |
| 574 | depsetHashes, err = a.depsetContentHashes(actionEntry.InputDepSetIds) |
| 575 | } |
| 576 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 577 | return nil, err |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 578 | } |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 579 | return &BuildStatement{ |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 580 | Depfile: nil, |
| 581 | OutputPaths: outputPaths, |
| 582 | Env: actionEntry.EnvironmentVariables, |
| 583 | Mnemonic: actionEntry.Mnemonic, |
| 584 | InputDepsetHashes: depsetHashes, |
| 585 | FileContents: actionEntry.FileContents, |
Cole Faust | 20f2030 | 2023-08-31 11:00:25 -0700 | [diff] [blame] | 586 | IsExecutable: actionEntry.IsExecutable, |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 587 | }, nil |
| 588 | } |
| 589 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 590 | func (a *aqueryArtifactHandler) symlinkTreeActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) { |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 591 | outputPaths, _, err := a.getOutputPaths(actionEntry) |
| 592 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 593 | return nil, err |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 594 | } |
| 595 | inputPaths, err := a.getInputPaths(actionEntry.InputDepSetIds) |
| 596 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 597 | return nil, err |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 598 | } |
| 599 | if len(inputPaths) != 1 || len(outputPaths) != 1 { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 600 | return nil, fmt.Errorf("Expect 1 input and 1 output to symlink action, got: input %q, output %q", inputPaths, outputPaths) |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 601 | } |
| 602 | // The actual command is generated in bazelSingleton.GenerateBuildActions |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 603 | return &BuildStatement{ |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 604 | Depfile: nil, |
| 605 | OutputPaths: outputPaths, |
| 606 | Env: actionEntry.EnvironmentVariables, |
| 607 | Mnemonic: actionEntry.Mnemonic, |
| 608 | InputPaths: inputPaths, |
| 609 | }, nil |
| 610 | } |
| 611 | |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 612 | type bazelSandwichJson struct { |
| 613 | Target string `json:"target"` |
| 614 | DependOnTarget *bool `json:"depend_on_target,omitempty"` |
| 615 | ImplicitDeps []string `json:"implicit_deps"` |
| 616 | } |
| 617 | |
| 618 | func (a *aqueryArtifactHandler) unresolvedSymlinkActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) { |
| 619 | outputPaths, depfile, err := a.getOutputPaths(actionEntry) |
| 620 | if err != nil { |
| 621 | return nil, err |
| 622 | } |
| 623 | if len(actionEntry.InputDepSetIds) != 0 || len(outputPaths) != 1 { |
| 624 | return nil, fmt.Errorf("expected 0 inputs and 1 output to symlink action, got: input %q, output %q", actionEntry.InputDepSetIds, outputPaths) |
| 625 | } |
| 626 | target := actionEntry.UnresolvedSymlinkTarget |
| 627 | if target == "" { |
| 628 | return nil, fmt.Errorf("expected an unresolved_symlink_target, but didn't get one") |
| 629 | } |
| 630 | if filepath.Clean(target) != target { |
| 631 | return nil, fmt.Errorf("expected %q, got %q", filepath.Clean(target), target) |
| 632 | } |
| 633 | if strings.HasPrefix(target, "/") { |
| 634 | return nil, fmt.Errorf("no absolute symlinks allowed: %s", target) |
| 635 | } |
| 636 | |
| 637 | out := outputPaths[0] |
| 638 | outDir := filepath.Dir(out) |
| 639 | var implicitDeps []string |
| 640 | if strings.HasPrefix(target, "bazel_sandwich:") { |
| 641 | j := bazelSandwichJson{} |
| 642 | err := json.Unmarshal([]byte(target[len("bazel_sandwich:"):]), &j) |
| 643 | if err != nil { |
| 644 | return nil, err |
| 645 | } |
| 646 | if proptools.BoolDefault(j.DependOnTarget, true) { |
| 647 | implicitDeps = append(implicitDeps, j.Target) |
| 648 | } |
| 649 | implicitDeps = append(implicitDeps, j.ImplicitDeps...) |
| 650 | dotDotsToReachCwd := "" |
| 651 | if outDir != "." { |
| 652 | dotDotsToReachCwd = strings.Repeat("../", strings.Count(outDir, "/")+1) |
| 653 | } |
| 654 | target = proptools.ShellEscapeIncludingSpaces(j.Target) |
| 655 | target = "{DOTDOTS_TO_OUTPUT_ROOT}" + dotDotsToReachCwd + target |
| 656 | } else { |
| 657 | target = proptools.ShellEscapeIncludingSpaces(target) |
| 658 | } |
| 659 | |
| 660 | outDir = proptools.ShellEscapeIncludingSpaces(outDir) |
| 661 | out = proptools.ShellEscapeIncludingSpaces(out) |
| 662 | // Use absolute paths, because some soong actions don't play well with relative paths (for example, `cp -d`). |
| 663 | command := fmt.Sprintf("mkdir -p %[1]s && rm -f %[2]s && ln -sf %[3]s %[2]s", outDir, out, target) |
| 664 | symlinkPaths := outputPaths[:] |
| 665 | |
| 666 | buildStatement := &BuildStatement{ |
| 667 | Command: command, |
| 668 | Depfile: depfile, |
| 669 | OutputPaths: outputPaths, |
| 670 | Env: actionEntry.EnvironmentVariables, |
| 671 | Mnemonic: actionEntry.Mnemonic, |
| 672 | SymlinkPaths: symlinkPaths, |
| 673 | ImplicitDeps: implicitDeps, |
| 674 | } |
| 675 | return buildStatement, nil |
| 676 | } |
| 677 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 678 | func (a *aqueryArtifactHandler) symlinkActionBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) { |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 679 | outputPaths, depfile, err := a.getOutputPaths(actionEntry) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 680 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 681 | return nil, err |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 682 | } |
| 683 | |
Usta Shrestha | c237249 | 2022-05-27 10:45:00 -0400 | [diff] [blame] | 684 | inputPaths, err := a.getInputPaths(actionEntry.InputDepSetIds) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 685 | if err != nil { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 686 | return nil, err |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 687 | } |
| 688 | if len(inputPaths) != 1 || len(outputPaths) != 1 { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 689 | return nil, fmt.Errorf("Expect 1 input and 1 output to symlink action, got: input %q, output %q", inputPaths, outputPaths) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 690 | } |
| 691 | out := outputPaths[0] |
| 692 | outDir := proptools.ShellEscapeIncludingSpaces(filepath.Dir(out)) |
| 693 | out = proptools.ShellEscapeIncludingSpaces(out) |
| 694 | in := filepath.Join("$PWD", proptools.ShellEscapeIncludingSpaces(inputPaths[0])) |
| 695 | // Use absolute paths, because some soong actions don't play well with relative paths (for example, `cp -d`). |
| 696 | command := fmt.Sprintf("mkdir -p %[1]s && rm -f %[2]s && ln -sf %[3]s %[2]s", outDir, out, in) |
| 697 | symlinkPaths := outputPaths[:] |
| 698 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 699 | buildStatement := &BuildStatement{ |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 700 | Command: command, |
| 701 | Depfile: depfile, |
| 702 | OutputPaths: outputPaths, |
| 703 | InputPaths: inputPaths, |
| 704 | Env: actionEntry.EnvironmentVariables, |
| 705 | Mnemonic: actionEntry.Mnemonic, |
| 706 | SymlinkPaths: symlinkPaths, |
| 707 | } |
| 708 | return buildStatement, nil |
| 709 | } |
| 710 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 711 | func (a *aqueryArtifactHandler) getOutputPaths(actionEntry *analysis_v2_proto.Action) (outputPaths []string, depfile *string, err error) { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 712 | for _, outputId := range actionEntry.OutputIds { |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 713 | outputPath, exists := a.artifactIdToPath[artifactId(outputId)] |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 714 | if !exists { |
| 715 | err = fmt.Errorf("undefined outputId %d", outputId) |
| 716 | return |
| 717 | } |
| 718 | ext := filepath.Ext(outputPath) |
| 719 | if ext == ".d" { |
| 720 | if depfile != nil { |
| 721 | err = fmt.Errorf("found multiple potential depfiles %q, %q", *depfile, outputPath) |
| 722 | return |
| 723 | } else { |
| 724 | depfile = &outputPath |
| 725 | } |
| 726 | } else { |
| 727 | outputPaths = append(outputPaths, outputPath) |
| 728 | } |
| 729 | } |
| 730 | return |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 731 | } |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 732 | |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 733 | // expandTemplateContent substitutes the tokens in a template. |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 734 | func expandTemplateContent(actionEntry *analysis_v2_proto.Action) string { |
| 735 | replacerString := make([]string, len(actionEntry.Substitutions)*2) |
| 736 | for i, pair := range actionEntry.Substitutions { |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 737 | value := pair.Value |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 738 | if val, ok := templateActionOverriddenTokens[pair.Key]; ok { |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 739 | value = val |
| 740 | } |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 741 | replacerString[i*2] = pair.Key |
| 742 | replacerString[i*2+1] = value |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 743 | } |
| 744 | replacer := strings.NewReplacer(replacerString...) |
| 745 | return replacer.Replace(actionEntry.TemplateContent) |
| 746 | } |
| 747 | |
Liz Kammer | f15a079 | 2023-02-09 14:28:36 -0500 | [diff] [blame] | 748 | // \->\\, $->\$, `->\`, "->\", \n->\\n, '->'"'"' |
| 749 | var commandLineArgumentReplacer = strings.NewReplacer( |
| 750 | `\`, `\\`, |
| 751 | `$`, `\$`, |
| 752 | "`", "\\`", |
| 753 | `"`, `\"`, |
| 754 | "\n", "\\n", |
| 755 | `'`, `'"'"'`, |
| 756 | ) |
| 757 | |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 758 | func escapeCommandlineArgument(str string) string { |
Liz Kammer | f15a079 | 2023-02-09 14:28:36 -0500 | [diff] [blame] | 759 | return commandLineArgumentReplacer.Replace(str) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 762 | func (a *aqueryArtifactHandler) actionToBuildStatement(actionEntry *analysis_v2_proto.Action) (*BuildStatement, error) { |
| 763 | switch actionEntry.Mnemonic { |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 764 | // Middleman actions are not handled like other actions; they are handled separately as a |
| 765 | // preparatory step so that their inputs may be relayed to actions depending on middleman |
| 766 | // artifacts. |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 767 | case middlemanMnemonic: |
| 768 | return nil, nil |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 769 | // PythonZipper is bogus action returned by aquery, ignore it (b/236198693) |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 770 | case "PythonZipper": |
| 771 | return nil, nil |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 772 | // Skip "Fail" actions, which are placeholder actions designed to always fail. |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 773 | case "Fail": |
| 774 | return nil, nil |
| 775 | case "BaselineCoverage": |
| 776 | return nil, nil |
| 777 | case "Symlink", "SolibSymlink", "ExecutableSymlink": |
| 778 | return a.symlinkActionBuildStatement(actionEntry) |
| 779 | case "TemplateExpand": |
| 780 | if len(actionEntry.Arguments) < 1 { |
| 781 | return a.templateExpandActionBuildStatement(actionEntry) |
| 782 | } |
Cole Faust | 950689a | 2023-06-21 15:07:21 -0700 | [diff] [blame] | 783 | case "FileWrite", "SourceSymlinkManifest", "RepoMappingManifest": |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 784 | return a.fileWriteActionBuildStatement(actionEntry) |
| 785 | case "SymlinkTree": |
| 786 | return a.symlinkTreeActionBuildStatement(actionEntry) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 787 | case "UnresolvedSymlink": |
| 788 | return a.unresolvedSymlinkActionBuildStatement(actionEntry) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 789 | } |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 790 | |
| 791 | if len(actionEntry.Arguments) < 1 { |
usta | a79afd7 | 2023-09-22 17:29:56 -0400 | [diff] [blame] | 792 | return nil, errors.New("received action with no command") |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 793 | } |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 794 | return a.normalActionBuildStatement(actionEntry) |
| 795 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 796 | } |
| 797 | |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 798 | func expandPathFragment(id pathFragmentId, pathFragmentsMap map[pathFragmentId]*analysis_v2_proto.PathFragment) (string, error) { |
Usta Shrestha | 6298cc5 | 2022-05-27 17:40:21 -0400 | [diff] [blame] | 799 | var labels []string |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 800 | currId := id |
| 801 | // Only positive IDs are valid for path fragments. An ID of zero indicates a terminal node. |
| 802 | for currId > 0 { |
| 803 | currFragment, ok := pathFragmentsMap[currId] |
| 804 | if !ok { |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 805 | return "", fmt.Errorf("undefined path fragment id %d", currId) |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 806 | } |
| 807 | labels = append([]string{currFragment.Label}, labels...) |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 808 | parentId := pathFragmentId(currFragment.ParentId) |
| 809 | if currId == parentId { |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 810 | return "", fmt.Errorf("fragment cannot refer to itself as parent %#v", currFragment) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 811 | } |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 812 | currId = parentId |
Chris Parsons | affbb60 | 2020-12-23 12:02:11 -0500 | [diff] [blame] | 813 | } |
| 814 | return filepath.Join(labels...), nil |
| 815 | } |