Skip to content

Commit 2a8d0ad

Browse files
sluongngcopybara-github
authored andcommitted
target pattern file: allow comments
`--target_pattern_file` could be a great tool assisting migration of a code base from one Bazel config to another. User could define a list of targets that can be built with the new configs and incrementally expand that list with each change that was introduced. Multiple users / teams could collaborate on such migration by adding / removing their targets into / from the pattern file. Having the ability to provide comments to annotate the targets in the pattern file with extra information during the migration process added a great value and context for build maintainers to track migration progress. Add support for Bash-style comments to the target-pattern file. Closes bazelbuild#15903. PiperOrigin-RevId: 461861131 Change-Id: I401f71fcf1e343c8689424979e4f48fb723fba9f
1 parent 6bbef00 commit 2a8d0ad

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/main/java/com/google/devtools/build/lib/runtime/commands/TargetPatternsHelper.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.devtools.build.lib.runtime.commands;
1616

17+
import static com.google.common.collect.ImmutableList.toImmutableList;
1718
import static java.nio.charset.StandardCharsets.UTF_8;
1819

1920
import com.google.common.base.Preconditions;
@@ -29,6 +30,7 @@
2930
import com.google.devtools.common.options.OptionsParsingResult;
3031
import java.io.IOException;
3132
import java.util.List;
33+
import java.util.function.Predicate;
3234

3335
/** Provides support for reading target patterns from a file or the command-line. */
3436
final class TargetPatternsHelper {
@@ -54,7 +56,12 @@ public static List<String> readFrom(CommandEnvironment env, OptionsParsingResult
5456
Path residuePath =
5557
env.getWorkingDirectory().getRelative(buildRequestOptions.targetPatternFile);
5658
try {
57-
targets = FileSystemUtils.readLines(residuePath, UTF_8);
59+
targets =
60+
FileSystemUtils.readLines(residuePath, UTF_8).stream()
61+
.map(s -> s.split("#")[0])
62+
.map(String::trim)
63+
.filter(Predicate.not(String::isEmpty))
64+
.collect(toImmutableList());
5865
} catch (IOException e) {
5966
throw new TargetPatternsHelperException(
6067
"I/O error reading from " + residuePath.getPathString() + ": " + e.getMessage(),

src/test/shell/integration/target_pattern_file_test.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ sh_test(name = "y", srcs = ["x.out"])
7373
EOF
7474

7575
cat >build.params <<'EOF'
76-
//:x
76+
# Test comment
77+
//:x # Trailing comment
7778
//:y
7879
EOF
7980
}

0 commit comments

Comments
 (0)