Skip to content

Commit

Permalink
Make repo marker file parsing robust to version changes
Browse files Browse the repository at this point in the history
Bazel 7.1.0 and 7.2.0 contains a bug where + characters in labels in the
repository marker files cannot be parsed. This was fixed in
commit d62e0a0. To reduce the rusk of
future bugs in the same area, this change skips parsing the file if the
first line shows that the content will not be used anyway, which should
be reasonably safe if introducing new formats.

Improves bazelbuild#23336 that fixed bazelbuild#23322.
  • Loading branch information
moroten committed Sep 17, 2024
1 parent 79ab580 commit 6164d18
Showing 1 changed file with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -685,39 +685,38 @@ byte[] areRepositoryAndMarkerFileConsistent(
String content;
try {
content = FileSystemUtils.readContent(markerPath, UTF_8);
String markerRuleKey = readMarkerFile(content, recordedInputValues);
boolean verified = false;
if (Preconditions.checkNotNull(ruleKey).equals(markerRuleKey)) {
verified = handler.verifyRecordedInputs(rule, directories, recordedInputValues, env);
if (env.valuesMissing()) {
return null;
}
if (!readMarkerFile(content, Preconditions.checkNotNull(ruleKey), recordedInputValues)) {
return null;
}

if (verified) {
return new Fingerprint().addString(content).digestAndReset();
} else {
if (!handler.verifyRecordedInputs(rule, directories, recordedInputValues, env)) {
return null;
}
if (env.valuesMissing()) {
return null;
}
return new Fingerprint().addString(content).digestAndReset();
} catch (IOException e) {
throw new RepositoryFunctionException(e, Transience.TRANSIENT);
}
}

@Nullable
private static String readMarkerFile(
String content, Map<RepoRecordedInput, String> recordedInputValues) {
String markerRuleKey = null;
private static boolean readMarkerFile(
String content, String expectedRuleKey, Map<RepoRecordedInput, String> recordedInputValues) {
Iterable<String> lines = Splitter.on('\n').split(content);

boolean firstLine = true;
boolean firstLineVerified = false;
for (String line : lines) {
if (line.isEmpty()) {
continue;
}
if (firstLine) {
markerRuleKey = line;
firstLine = false;
if (!firstLineVerified) {
if (!line.equals(expectedRuleKey)) {
// Break early, need to reload anyway. This also detects marker file version changes
// so that unknown formats are not parsed.
return false;
}
firstLineVerified = true;
} else {
int sChar = line.indexOf(' ');
if (sChar > 0) {
Expand All @@ -733,7 +732,7 @@ private static String readMarkerFile(
break;
}
}
return markerRuleKey;
return firstLineVerified;
}

private String computeRuleKey(Rule rule, StarlarkSemantics starlarkSemantics) {
Expand Down

0 comments on commit 6164d18

Please sign in to comment.