Skip to content

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

Closed
Tomsons wants to merge 2 commits intobazelbuild:masterfrom
Tomsons:fix/malformed-repo-spec-json
Closed

fix(bzlmod): throw on json parse exception#15109
Tomsons wants to merge 2 commits intobazelbuild:masterfrom
Tomsons:fix/malformed-repo-spec-json

Conversation

@Tomsons
Copy link
Copy Markdown
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

@sgowroji sgowroji added the team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. label Mar 24, 2022
@sgowroji sgowroji requested a review from meteorcloudy March 24, 2022 04:02
Copy link
Copy Markdown
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!

}
String jsonString = new String(bytes.get(), UTF_8);
return Optional.of(gson.fromJson(jsonString, klass));
if (jsonString.strip().equals("")) {
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

@bazel-io fork 5.2

1 similar comment
@Wyverald
Copy link
Copy Markdown
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 <[email protected]>
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.

5 participants