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

Conversation

Tomsons
Copy link
Contributor

@Tomsons Tomsons commented Mar 24, 2022

Fixes

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

Copy link
Member

@meteorcloudy meteorcloudy left a comment

Choose a reason for hiding this comment

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

Thank you!

@@ -115,7 +117,14 @@ private String constructUrl(String base, String... segments) {
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!

@Tomsons
Copy link
Contributor Author

Tomsons commented Mar 24, 2022

You're welcome @Wyverald @meteorcloudy

@bazel-io bazel-io closed this in ac21910 Mar 24, 2022
@Tomsons Tomsons deleted the fix/malformed-repo-spec-json branch March 24, 2022 15:06
@brentleyjones
Copy link
Contributor

@bazel-io fork 5.2

1 similar comment
@Wyverald
Copy link
Member

Wyverald commented Mar 24, 2022

@bazel-io fork 5.2

ckolli5 added a commit that referenced this pull request May 9, 2022
[Fixes](#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 #15109.

PiperOrigin-RevId: 436992309

Co-authored-by: Thomas Zayouna <tzayouna@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants