Skip to content

Commit 47b1557

Browse files
committed
Respond to review comments
1 parent 26571a7 commit 47b1557

File tree

8 files changed

+59
-16
lines changed

8 files changed

+59
-16
lines changed

private/lib/coordinates.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def unpack_coordinates(coords):
5757
def _is_version_number(part):
5858
return part[0].isdigit()
5959

60-
def _unpack_if_necssary(coords):
60+
def _unpack_if_necessary(coords):
6161
if type(coords) == "string":
6262
unpacked = unpack_coordinates(coords)
6363
elif type(coords) == "dict":
@@ -80,7 +80,7 @@ def to_external_form(coords):
8080
syntax: `group:name:version:classifier@packaging`
8181
"""
8282

83-
unpacked = _unpack_if_necssary(coords)
83+
unpacked = _unpack_if_necessary(coords)
8484

8585
to_return = "%s:%s:%s" % (unpacked.group, unpacked.artifact, unpacked.version)
8686

@@ -97,7 +97,7 @@ def to_external_form(coords):
9797
# This matches the `Coordinates#asKey` method in the Java tree, and the
9898
# implementations must be kept in sync.
9999
def to_key(coords):
100-
unpacked = _unpack_if_necssary(coords)
100+
unpacked = _unpack_if_necessary(coords)
101101

102102
key = unpacked.group + ":" + unpacked.artifact
103103

private/rules/v2_lock_file.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def _original_compute_lock_file_hash(lock_file_contents):
4848
return hash(repr(to_hash))
4949

5050
def _compute_lock_file_hash(lock_file_contents):
51+
# Note: this function is exactly equivalent to the one in `ArtifactsHash.java` if you
52+
# make a change there please make it here, and vice versa.
5153
lines = []
5254
artifacts = _get_artifacts(lock_file_contents)
5355

private/tools/java/com/github/bazelbuild/rules_jvm_external/Coordinates.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ public class Coordinates implements Comparable<Coordinates> {
2727
private final String classifier;
2828
private final String extension;
2929

30+
/**
31+
* Converts a `String` in one of two formats and extracts the information from it.
32+
*
33+
* <p>The two supported formats are:
34+
*
35+
* <ol>
36+
* <li>group:artifact:version:classifier@extension
37+
* <li>group:artifact:extension:classifier:version.
38+
* </ol>
39+
*
40+
* The first of these matches Gradle's <a
41+
* href="https://docs.gradle.org/8.11.1/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html#N1739F">
42+
* external dependencies</a> form, and is the preferred format to use since it matches
43+
* expectations of users of other tools. The second format is the one used within
44+
* `rules_jvm_external` since its inception.
45+
*
46+
* <p>Note that there is potential confusion when only three segments are given (that is, the
47+
* value could be one of `group:artifact:version` or `group:artifact:extension`) In this case, we
48+
* assume the value is `group:artifact:version` as this is far more widely used.
49+
*/
3050
public Coordinates(String coordinates) {
3151
Objects.requireNonNull(coordinates, "Coordinates");
3252

private/tools/java/com/github/bazelbuild/rules_jvm_external/resolver/ArtifactsHash.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2024 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package com.github.bazelbuild.rules_jvm_external.resolver;
216

317
import com.github.bazelbuild.rules_jvm_external.Coordinates;
@@ -6,21 +20,27 @@
620
import java.util.TreeSet;
721
import java.util.stream.Collectors;
822

9-
public class ArtifactsHash {
23+
/** Utility class for calculating artifact hashes for lock files. */
24+
public final class ArtifactsHash {
25+
26+
public static final int NO_HASH = -1;
27+
private static final String SEPARATOR = " | ";
1028

1129
private ArtifactsHash() {
1230
// utility class
1331
}
1432

1533
public static int calculateArtifactsHash(Collection<DependencyInfo> infos) {
34+
// Note: this function is exactly equivalent to the one in `v2_lock_file.bzl`
35+
// if you make a change there please make it here, and vice versa.
1636
Set<String> lines = new TreeSet<>();
1737

1838
for (DependencyInfo info : infos) {
1939
StringBuilder line = new StringBuilder();
2040
line.append(info.getCoordinates().toString())
21-
.append(" | ")
41+
.append(SEPARATOR)
2242
.append(info.getSha256().orElseGet(() -> ""))
23-
.append(" | ");
43+
.append(SEPARATOR);
2444

2545
line.append(
2646
info.getDependencies().stream()

private/tools/java/com/github/bazelbuild/rules_jvm_external/resolver/lockfile/V2LockFile.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.github.bazelbuild.rules_jvm_external.resolver.lockfile;
1616

17+
import static com.github.bazelbuild.rules_jvm_external.resolver.ArtifactsHash.NO_HASH;
1718
import static com.github.bazelbuild.rules_jvm_external.resolver.ArtifactsHash.calculateArtifactsHash;
1819
import static com.google.common.base.StandardSystemProperty.USER_HOME;
1920

@@ -179,7 +180,7 @@ public static V2LockFile create(String from) {
179180
}
180181

181182
Number rawInputHash = (Number) raw.get("__INPUT_ARTIFACTS_HASH");
182-
int inputHash = rawInputHash == null ? -1 : rawInputHash.intValue();
183+
int inputHash = rawInputHash == null ? NO_HASH : rawInputHash.intValue();
183184

184185
return new V2LockFile(inputHash, repos, infos, conflicts);
185186
}

tests/com/github/bazelbuild/rules_jvm_external/resolver/lockfile/V2LockFileTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.github.bazelbuild.rules_jvm_external.resolver.lockfile;
1616

17+
import static com.github.bazelbuild.rules_jvm_external.resolver.ArtifactsHash.NO_HASH;
1718
import static org.junit.Assert.assertEquals;
1819

1920
import com.github.bazelbuild.rules_jvm_external.Coordinates;
@@ -45,7 +46,8 @@ public void shouldRenderAggregatingJarsAsJarWithNullShasum() {
4546
Set.of(),
4647
new TreeMap<>());
4748

48-
Map<String, Object> rendered = new V2LockFile(-1, repos, Set.of(aggregator), Set.of()).render();
49+
Map<String, Object> rendered =
50+
new V2LockFile(NO_HASH, repos, Set.of(aggregator), Set.of()).render();
4951

5052
Map<?, ?> artifacts = (Map<?, ?>) rendered.get("artifacts");
5153
Map<?, ?> data = (Map<?, ?>) artifacts.get("com.example:aggregator");
@@ -58,7 +60,7 @@ public void shouldRenderAggregatingJarsAsJarWithNullShasum() {
5860

5961
@Test
6062
public void shouldRoundTripASimpleSetOfDependencies() {
61-
V2LockFile roundTripped = roundTrip(new V2LockFile(-1, repos, Set.of(), Set.of()));
63+
V2LockFile roundTripped = roundTrip(new V2LockFile(NO_HASH, repos, Set.of(), Set.of()));
6264

6365
assertEquals(repos, roundTripped.getRepositories());
6466
assertEquals(Set.of(), roundTripped.getDependencyInfos());
@@ -67,7 +69,7 @@ public void shouldRoundTripASimpleSetOfDependencies() {
6769

6870
@Test
6971
public void shouldRoundTripM2Local() {
70-
V2LockFile lockFile = new V2LockFile(-1, repos, Set.of(), Set.of());
72+
V2LockFile lockFile = new V2LockFile(NO_HASH, repos, Set.of(), Set.of());
7173
Map<String, Object> rendered = lockFile.render();
7274
rendered.put("m2local", true);
7375

@@ -90,7 +92,7 @@ public void shouldRoundTripASingleArtifact() {
9092
Set.of(),
9193
new TreeMap<>());
9294

93-
V2LockFile lockFile = roundTrip(new V2LockFile(-1, repos, Set.of(info), Set.of()));
95+
V2LockFile lockFile = roundTrip(new V2LockFile(NO_HASH, repos, Set.of(info), Set.of()));
9496

9597
assertEquals(Set.of(info), lockFile.getDependencyInfos());
9698
}
@@ -119,7 +121,7 @@ public void shouldRoundTripASingleArtifactWithADependency() {
119121
Set.of(),
120122
new TreeMap<>());
121123

122-
V2LockFile lockFile = roundTrip(new V2LockFile(-1, repos, Set.of(info, dep), Set.of()));
124+
V2LockFile lockFile = roundTrip(new V2LockFile(NO_HASH, repos, Set.of(info, dep), Set.of()));
123125

124126
assertEquals(Set.of(info, dep), lockFile.getDependencyInfos());
125127
}
@@ -133,7 +135,7 @@ public void shouldRoundTripConflicts() {
133135
new Conflict(
134136
new Coordinates("com.foo:bar:1.2.3"), new Coordinates("com.foo:bar:1.2.1")));
135137

136-
V2LockFile lockFile = roundTrip(new V2LockFile(-1, repos, Set.of(), conflicts));
138+
V2LockFile lockFile = roundTrip(new V2LockFile(NO_HASH, repos, Set.of(), conflicts));
137139

138140
assertEquals(conflicts, lockFile.getConflicts());
139141
}

tests/integration/maven_bom/BUILD

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
21
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
3-
load("@rules_java//java:defs.bzl", "java_library")
2+
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
43
load("//:defs.bzl", "artifact", "java_export", "maven_bom")
54

65
maven_bom(

tests/unit/exports/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
load("@rules_java//java:defs.bzl", "java_library")
21
load("//tests/unit/exports:exports_test.bzl", "exports_tests")
32

43
exports_tests(name = "exports_tests")

0 commit comments

Comments
 (0)