Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bzlmod): throw on json parse exception #15109

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
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 +117,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().equals("")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the empty string treated specially?

also prefer using String#isEmpty over equals("")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will come with another commit.

The empty string is treated specially because the bazel_registry.json can be empty without being explicitly declared as an empty json object {}, this should not throw an exception as it is simply ignored at getRepoSpec

    if (bazelRegistryJson.isPresent() && bazelRegistryJson.get().mirrors != null) {
      for (String mirror : bazelRegistryJson.get().mirrors) {
        try {
          new URL(mirror);
        } catch (MalformedURLException e) {
          throw new IOException("Malformed mirror URL", e);
        }

        urls.add(constructUrl(mirror, sourceUrl.getAuthority(), sourceUrl.getFile()));
      }
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still rather the file always be valid JSON, but this is such a minor point that I won't push it. Thanks for the contribution!

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,57 @@ 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));
}
}