Skip to content

Commit

Permalink
Be more precise about where to read the URL rewriter config from
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shs96c authored and copybara-github committed Jun 20, 2024
1 parent 432a9a1 commit 0e220f0
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 @@ -389,7 +389,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 0e220f0

Please sign in to comment.