Skip to content

Commit 139543d

Browse files
vladmosphilwo
authored andcommitted
Fix string representations for root objects
Root objects should not expose their absolute paths as they break hermeticity. PiperOrigin-RevId: 168363549
1 parent 1a72fb7 commit 139543d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/main/java/com/google/devtools/build/lib/actions/Root.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ public String toString() {
222222

223223
@Override
224224
public void repr(SkylarkPrinter printer) {
225+
printer.append(isSourceRoot() ? "<source root>" : "<derived root>");
226+
}
227+
228+
@Override
229+
public void reprLegacy(SkylarkPrinter printer) {
225230
printer.append(toString());
226231
}
227232
}

src/test/java/com/google/devtools/build/lib/skylark/SkylarkStringRepresentationsTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ private void generateFilesToTestStrings() throws Exception {
179179
")",
180180
"",
181181
"def _check_impl(ctx):",
182+
" source_file = ctx.attr.srcs[0].files.to_list()[0]",
183+
" generated_file = ctx.attr.srcs[1].files.to_list()[0]",
182184
" objects = {",
183185
" 'target': ctx.attr.deps[0],",
184186
" 'alias_target': ctx.attr.deps[1],",
@@ -188,8 +190,10 @@ private void generateFilesToTestStrings() throws Exception {
188190
" 'rule_ctx': ctx,",
189191
" 'aspect_ctx': ctx.attr.asp_deps[0][aspect_ctx_provider].ctx,",
190192
" 'aspect_ctx.rule': ctx.attr.asp_deps[0][aspect_ctx_provider].rule,",
191-
" 'source_file': ctx.attr.srcs[0].files.to_list()[0],",
192-
" 'generated_file': ctx.attr.srcs[1].files.to_list()[0],",
193+
" 'source_file': source_file,",
194+
" 'generated_file': generated_file,",
195+
" 'source_root': source_file.root,",
196+
" 'generated_root': generated_file.root,",
193197
" }",
194198
" return struct(**prepare_params(objects))",
195199
"check = rule(",
@@ -334,6 +338,19 @@ public void testStringRepresentations_Files() throws Exception {
334338
}
335339
}
336340

341+
@Test
342+
public void testStringRepresentations_Root() throws Exception {
343+
setSkylarkSemanticsOptions("--incompatible_descriptive_string_representations=true");
344+
345+
generateFilesToTestStrings();
346+
ConfiguredTarget target = getConfiguredTarget("//test/skylark:check");
347+
348+
for (String suffix : SUFFIXES) {
349+
assertThat(target.get("source_root" + suffix)).isEqualTo("<source root>");
350+
assertThat(target.get("generated_root" + suffix)).isEqualTo("<derived root>");
351+
}
352+
}
353+
337354
@Test
338355
public void testStringRepresentations_Glob() throws Exception {
339356
setSkylarkSemanticsOptions("--incompatible_descriptive_string_representations=true");

0 commit comments

Comments
 (0)