Skip to content

Commit

Permalink
fix(bzlmod): throw on json parse exception (bazelbuild#15429)
Browse files Browse the repository at this point in the history
[Fixes](bazelbuild#14437)

Get rid of new lines in `bazel_registry.json` and return an `Optional.empty()` aka registry descriptor without mirrors and throw an IOException for malformed JSON to stick to official json.org specs

Closes bazelbuild#15109.

PiperOrigin-RevId: 436992309

Co-authored-by: Thomas Zayouna <tzayouna@gmail.com>
  • Loading branch information
ckolli5 and Tomsons authored May 9, 2022
1 parent ad74d52 commit 374400d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -115,7 +116,14 @@ private <T> Optional<T> grabJson(String url, Class<T> klass, ExtendedEventHandle
return Optional.empty();
}
String jsonString = new String(bytes.get(), UTF_8);
return Optional.of(gson.fromJson(jsonString, klass));
if (jsonString.strip().isEmpty()) {
return Optional.empty();
}
try {
return Optional.of(gson.fromJson(jsonString, klass));
} catch (JsonParseException e) {
throw new IOException(String.format("Unable to parse json at url %s", url), e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import static com.google.common.truth.Truth8.assertThat;
import static com.google.devtools.build.lib.bazel.bzlmod.BzlmodTestUtil.createModuleKey;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.bazel.repository.downloader.HttpDownloader;
import com.google.devtools.build.lib.testutil.FoundationTestCase;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import org.junit.Before;
Expand Down Expand Up @@ -140,4 +142,54 @@ public void testGetRepoSpec() throws Exception {
.setRemotePatchStrip(3)
.build());
}

@Test
public void testGetRepoInvalidRegistryJsonSpec() throws Exception {
server.serve("/bazel_registry.json", "", "", "", "");
server.start();
server.serve(
"/modules/foo/1.0/source.json",
"{",
" \"url\": \"http://mysite.com/thing.zip\",",
" \"integrity\": \"sha256-blah\",",
" \"strip_prefix\": \"pref\"",
"}");

Registry registry = registryFactory.getRegistryWithUrl(server.getUrl());
assertThat(registry.getRepoSpec(createModuleKey("foo", "1.0"), "foorepo", reporter))
.isEqualTo(
new ArchiveRepoSpecBuilder()
.setRepoName("foorepo")
.setUrls(ImmutableList.of("http://mysite.com/thing.zip"))
.setIntegrity("sha256-blah")
.setStripPrefix("pref")
.setRemotePatches(ImmutableMap.of())
.setRemotePatchStrip(0)
.build());
}

@Test
public void testGetRepoInvalidModuleJsonSpec() throws Exception {
server.serve(
"/bazel_registry.json",
"{",
" \"mirrors\": [",
" \"https://mirror.bazel.build/\",",
" \"file:///home/bazel/mymirror/\"",
" ]",
"}");
server.serve(
"/modules/foo/1.0/source.json",
"{",
" \"url\": \"http://mysite.com/thing.zip\",",
" \"integrity\": \"sha256-blah\",",
" \"strip_prefix\": \"pref\",",
"}");
server.start();

Registry registry = registryFactory.getRegistryWithUrl(server.getUrl());
assertThrows(
IOException.class,
() -> registry.getRepoSpec(createModuleKey("foo", "1.0"), "foorepo", reporter));
}
}

0 comments on commit 374400d

Please sign in to comment.