Skip to content

Commit

Permalink
[7.3.0] Be more precise about where to read the URL rewriter config f…
Browse files Browse the repository at this point in the history
…rom (#22832)

There is an [open
issue](#22104) where bazel
will occasionally fail at start up time because it is unable to parse
the downloader config. While I've yet to create a reliable reproduction
for this issue, the one time I've triggered this error with a build with
additional logging, it seemed to be because the current working
directory wasn't the root of the workspace. While I'm not sure this will
actually fully resolve the problem (since I don't know what's causing
it) being clearer about where the config file is meant to be can only be
a Good Thing.

Addresses #22104

Closes #22770.

PiperOrigin-RevId: 645115556
Change-Id: I7e9bea1c55a106cd767ebfd6d557a7c59c47cd66

Commit
0e220f0

Co-authored-by: Simon Mavi Stewart <[email protected]>
Co-authored-by: Yun Peng <[email protected]>
  • Loading branch information
3 people authored Jun 21, 2024
1 parent 9f3b4f7 commit 7ce5850
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
}
try {
UrlRewriter rewriter =
UrlRewriter.getDownloaderUrlRewriter(repoOptions.downloaderConfig, env.getReporter());
UrlRewriter.getDownloaderUrlRewriter(
env.getWorkspace(), repoOptions.downloaderConfig, env.getReporter());
downloadManager.setUrlRewriter(rewriter);
} catch (UrlRewriterParseException e) {
// It's important that the build stops ASAP, because this config file may be required for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -85,16 +84,26 @@ public class UrlRewriter {
* @param configPath Path to the config file to use. May be null.
* @param reporter Used for logging when URLs are rewritten.
*/
public static UrlRewriter getDownloaderUrlRewriter(String configPath, Reporter reporter)
throws UrlRewriterParseException {
public static UrlRewriter getDownloaderUrlRewriter(
Path workspaceRoot, String configPath, Reporter reporter) throws UrlRewriterParseException {
Consumer<String> log = str -> reporter.handle(Event.info(str));

// "empty" UrlRewriter shouldn't alter auth headers
if (Strings.isNullOrEmpty(configPath)) {
return new UrlRewriter(log, "", new StringReader(""));
}

try (BufferedReader reader = Files.newBufferedReader(Paths.get(configPath))) {
// There have been reports (eg. https://github.com/bazelbuild/bazel/issues/22104) that
// there are occasional errors when `configFile` can't be found, and when this happens
// investigation suggests that the current working directory isn't the workspace root.
Path actualConfigPath = workspaceRoot.getRelative(configPath);

if (!actualConfigPath.exists()) {
throw new UrlRewriterParseException(
String.format("Unable to find downloader config file %s", configPath));
}

try (BufferedReader reader = Files.newBufferedReader(actualConfigPath.getPathFile().toPath())) {
return new UrlRewriter(log, configPath, reader);
} catch (IOException e) {
throw new UrlRewriterParseException(e.getMessage());
Expand Down

0 comments on commit 7ce5850

Please sign in to comment.