Skip to content

Commit

Permalink
Allow ~/home in downloaderConfig of RepositoryOptions
Browse files Browse the repository at this point in the history
Similar to `--distdir` it makes sense to provide a `--experimental_downloader_config` from `~/.bazelrc` where it is more convenient to address a file in ones home directory by using `~/...` syntax instead of on absolute path.

Closes #24618.

PiperOrigin-RevId: 708007102
Change-Id: Ia8d0ebc1cb79c95ea58888c08746adc917d3fb63
  • Loading branch information
kiron1 authored and copybara-github committed Dec 19, 2024
1 parent 51a6c38 commit 0ce2d7d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,15 @@ public class RepositoryOptions extends OptionsBase {
defaultValue = "null",
documentationCategory = OptionDocumentationCategory.REMOTE,
effectTags = {OptionEffectTag.UNKNOWN},
converter = OptionsUtils.PathFragmentConverter.class,
help =
"Specify a file to configure the remote downloader with. This file consists of lines, "
+ "each of which starts with a directive (`allow`, `block` or `rewrite`) followed "
+ "by either a host name (for `allow` and `block`) or two patterns, one to match "
+ "against, and one to use as a substitute URL, with back-references starting from "
+ "`$1`. It is possible for multiple `rewrite` directives for the same URL to be "
+ "give, and in this case multiple URLs will be returned.")
public String downloaderConfig;
public PathFragment downloaderConfig;

/** See {@link #workerForRepoFetching}. */
public enum WorkerForRepoFetching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand All @@ -32,6 +31,7 @@
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -86,11 +86,12 @@ public class UrlRewriter {
* @param reporter Used for logging when URLs are rewritten.
*/
public static UrlRewriter getDownloaderUrlRewriter(
Path workspaceRoot, String configPath, Reporter reporter) throws UrlRewriterParseException {
Path workspaceRoot, @Nullable PathFragment 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)) {
if (configPath == null || configPath.isEmpty()) {
return new UrlRewriter(log, "", new StringReader(""));
}

Expand All @@ -101,13 +102,13 @@ public static UrlRewriter getDownloaderUrlRewriter(

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

try (InputStream inputStream = actualConfigPath.getInputStream();
Reader inputStreamReader = new InputStreamReader(inputStream);
Reader reader = new BufferedReader(inputStreamReader)) {
return new UrlRewriter(log, configPath, reader);
return new UrlRewriter(log, configPath.getPathString(), reader);
} catch (IOException e) {
throw new UrlRewriterParseException(e.getMessage());
}
Expand Down

0 comments on commit 0ce2d7d

Please sign in to comment.