27
27
import com .google .devtools .build .lib .actions .CommandLineItem ;
28
28
import com .google .devtools .build .lib .actions .CommandLineItem .ExceptionlessMapFn ;
29
29
import com .google .devtools .build .lib .actions .CommandLineItem .MapFn ;
30
- import com .google .devtools .build .lib .actions .CommandLines .ParamFileActionInput ;
31
30
import com .google .devtools .build .lib .actions .PathMapper ;
32
31
import com .google .devtools .build .lib .actions .Spawn ;
32
+ import com .google .devtools .build .lib .actions .cache .VirtualActionInput ;
33
33
import com .google .devtools .build .lib .starlarkbuildapi .FileRootApi ;
34
34
import com .google .devtools .build .lib .vfs .PathFragment ;
35
35
import java .util .HashMap ;
@@ -145,7 +145,7 @@ static Optional<PathMapper> tryCreate(AbstractAction action, boolean isStarlarkA
145
145
146
146
@ Override
147
147
public String getMappedExecPathString (ActionInput artifact ) {
148
- if (isSupportedInputType (artifact ) && isOutputPath ( artifact , outputRoot )) {
148
+ if (isSupported (artifact )) {
149
149
return strip (artifact .getExecPath ()).getPathString ();
150
150
} else {
151
151
return artifact .getExecPathString ();
@@ -157,6 +157,15 @@ public PathFragment map(PathFragment execPath) {
157
157
return isOutputPath (execPath , outputRoot ) ? strip (execPath ) : execPath ;
158
158
}
159
159
160
+ @ Override
161
+ public int computeExecPathLengthDiff (DerivedArtifact artifact ) {
162
+ String unmappedPath = artifact .getExecPathString ();
163
+ // bazel-out/k8-fastbuild/... is mapped to bazel-out/${FIXED_CONFIG_SEGMENT}/...
164
+ int firstSlash = outputRoot .getPathString ().length () + 1 ;
165
+ int secondSlash = unmappedPath .indexOf ('/' , firstSlash + 1 );
166
+ return (secondSlash - firstSlash ) - FIXED_CONFIG_SEGMENT .length ();
167
+ }
168
+
160
169
@ Override
161
170
public ArgChunk mapCustomStarlarkArgs (ArgChunk chunk ) {
162
171
if (!isStarlarkAction ) {
@@ -174,7 +183,7 @@ public ArgChunk mapCustomStarlarkArgs(ArgChunk chunk) {
174
183
175
184
// TODO: b/327187486 - This materializes strings when totalArgLength() is called. Can it
176
185
// compute the total arg length without creating garbage strings?
177
- Iterable <String > args = chunk .arguments ();
186
+ Iterable <String > args = chunk .arguments (this );
178
187
return new SimpleArgChunk (() -> new CustomStarlarkArgsIterator (args .iterator (), argStripper ));
179
188
}
180
189
@@ -204,10 +213,14 @@ public FileRootApi mapRoot(Artifact artifact) {
204
213
return PathMapper .super .mapRoot (artifact );
205
214
}
206
215
207
- private boolean isSupportedInputType (ActionInput artifact ) {
208
- return artifact instanceof DerivedArtifact
209
- || artifact instanceof ParamFileActionInput
210
- || artifact instanceof BasicActionInput ;
216
+ private boolean isSupported (ActionInput artifact ) {
217
+ if (artifact instanceof DerivedArtifact ) {
218
+ return true ;
219
+ }
220
+ if (artifact instanceof BasicActionInput || artifact instanceof VirtualActionInput ) {
221
+ return isOutputPath (artifact , outputRoot );
222
+ }
223
+ return false ;
211
224
}
212
225
213
226
private static final class CustomStarlarkArgsIterator implements Iterator <String > {
@@ -339,12 +352,6 @@ private static boolean isPathStrippable(
339
352
* Strips the configuration prefix from an output artifact's exec path.
340
353
*/
341
354
private static PathFragment strip (PathFragment execPath ) {
342
- if (execPath .subFragment (1 , 2 ).getPathString ().equals ("tmp" )) {
343
- return execPath
344
- .subFragment (0 , 2 )
345
- .getRelative (FIXED_CONFIG_SEGMENT )
346
- .getRelative (execPath .subFragment (3 ));
347
- }
348
355
return execPath
349
356
.subFragment (0 , 1 )
350
357
// Keep the config segment, but replace it with a fixed string to improve cacheability while
0 commit comments