3333load ("//private/lib:coordinates.bzl" , "to_key" , "unpack_coordinates" )
3434load ("//private/lib:urls.bzl" , "remove_auth_from_url" )
3535load ("//private/rules:v1_lock_file.bzl" , "v1_lock_file" )
36- load ("//private/rules:v2_lock_file .bzl" , "v2_lock_file" )
36+ load ("//private/rules:v3_lock_file .bzl" , "v2_lock_file" , "v3_lock_file " )
3737
3838_BUILD = """
3939# package(default_visibility = [{visibilities}]) # https://github.com/bazelbuild/bazel/issues/13681
@@ -341,6 +341,13 @@ def _stable_artifact(artifact):
341341 keys = sorted (parsed .keys ())
342342 return ":" .join (["%s=%s" % (key , parsed [key ]) for key in keys ])
343343
344+ def _add_to_hash_dictionary (dictionary , artifact , salt ):
345+ artifact_dict = json .decode (artifact )
346+ key = artifact_dict ["group" ] + ":" + artifact_dict ["artifact" ]
347+ value = dictionary .get (key , [])
348+ value .append (hash (_stable_artifact (artifact ) + salt ))
349+ dictionary [key ] = value
350+
344351# Compute a signature of the list of artifacts that will be used to build
345352# the dependency tree. This is used as a check to see whether the dependency
346353# tree needs to be repinned.
@@ -359,24 +366,37 @@ def compute_dependency_inputs_signature(boms = [], artifacts = [], repositories
359366 artifact_inputs = []
360367 excluded_artifact_inputs = []
361368
369+ all_hashes = dict ()
370+
362371 if boms and len (boms ):
363372 for bom in sorted (boms ):
364373 artifact_inputs .append (_stable_artifact (bom ))
374+ _add_to_hash_dictionary (all_hashes , bom , "bom" )
365375
366376 for artifact in sorted (artifacts ):
367377 artifact_inputs .append (_stable_artifact (artifact ))
378+ _add_to_hash_dictionary (all_hashes , artifact , "artifact" )
368379
369380 for artifact in sorted (excluded_artifacts ):
370381 excluded_artifact_inputs .append (_stable_artifact (artifact ))
382+ _add_to_hash_dictionary (all_hashes , artifact , "excluded_artifact" )
371383
372384 v1_sig = hash (repr (sorted (artifact_inputs ))) ^ hash (repr (sorted (repositories )))
373385
374386 hash_parts = [sorted (artifact_inputs ), sorted (repositories ), sorted (excluded_artifact_inputs )]
375- current_version_sig = 0
387+ v2_sig = 0
376388 for part in hash_parts :
377- current_version_sig ^= hash (repr (part ))
389+ v2_sig ^= hash (repr (part ))
390+
391+ for k , v in all_hashes .items ():
392+ if len (v ) == 1 :
393+ all_hashes [k ] = v [0 ]
394+ else :
395+ all_hashes [k ] = hash (repr (sorted (v )))
378396
379- return (current_version_sig , [v1_sig ])
397+ all_hashes ["repositories" ] = hash (repr (sorted (repositories )))
398+
399+ return (all_hashes , [v1_sig , v2_sig ])
380400
381401def get_netrc_lines_from_entries (netrc_entries ):
382402 netrc_lines = []
@@ -540,21 +560,26 @@ def _pinned_coursier_fetch_impl(repository_ctx):
540560 "artifacts" : {},
541561 "dependencies" : {},
542562 "repositories" : {},
543- "version" : "2 " ,
563+ "version" : "3 " ,
544564 }
545565 else :
546566 maven_install_json_content = json .decode (lock_file_content )
547567
548- if v1_lock_file .is_valid_lock_file (maven_install_json_content ):
568+ if v3_lock_file .is_valid_lock_file (maven_install_json_content ):
569+ importer = v3_lock_file
570+ elif v2_lock_file .is_valid_lock_file (maven_install_json_content ):
571+ importer = v2_lock_file
572+ elif v1_lock_file .is_valid_lock_file (maven_install_json_content ):
549573 importer = v1_lock_file
574+ else :
575+ fail ("Unable to read lock file: %s" % repository_ctx .attr .maven_install_json )
576+
577+ # Check if using the most recent lock file format.
578+ if importer != v3_lock_file :
550579 print_if_not_repinning (
551580 repository_ctx ,
552581 "Lock file should be updated. Please run `REPIN=1 bazel run @unpinned_%s//:pin`" % repository_ctx .name ,
553582 )
554- elif v2_lock_file .is_valid_lock_file (maven_install_json_content ):
555- importer = v2_lock_file
556- else :
557- fail ("Unable to read lock file: %s" % repository_ctx .attr .maven_install_json )
558583
559584 # Validation steps for maven_install.json.
560585
@@ -612,10 +637,10 @@ def _pinned_coursier_fetch_impl(repository_ctx):
612637 )
613638 elif computed_artifacts_hash != input_artifacts_hash :
614639 if _get_fail_if_repin_required (repository_ctx ):
615- fail ("%s_install.json contains an invalid input signature (expected %s and got %s) and must be regenerated. " % (
640+ to_print = importer .print_friendly_hash_difference (input_artifacts_hash , computed_artifacts_hash )
641+ fail ("%s_install.json contains an invalid input signature (%s) and must be regenerated. " % (
616642 user_provided_name ,
617- input_artifacts_hash ,
618- computed_artifacts_hash ,
643+ to_print ,
619644 ) +
620645 "This typically happens when the maven_install artifacts have been changed but not repinned. " +
621646 "PLEASE DO NOT MODIFY THIS FILE DIRECTLY! To generate a new " +
@@ -641,11 +666,12 @@ def _pinned_coursier_fetch_impl(repository_ctx):
641666 # Then, validate that the signature provided matches the contents of the dependency_tree.
642667 # This is to stop users from manually modifying maven_install.json.
643668 if _get_fail_if_repin_required (repository_ctx ):
669+ computed_hash = importer .compute_lock_file_hash (maven_install_json_content )
670+ to_print = importer .print_friendly_hash_difference (dep_tree_signature , computed_hash )
644671 fail (
645- "%s_install.json contains an invalid signature (expected %s and got %s) and may be corrupted. " % (
672+ "%s_install.json contains an invalid signature (%s) and may be corrupted. " % (
646673 user_provided_name ,
647- dep_tree_signature ,
648- importer .compute_lock_file_hash (maven_install_json_content ),
674+ to_print ,
649675 ) +
650676 "PLEASE DO NOT MODIFY THIS FILE DIRECTLY! To generate a new " +
651677 "%s_install.json and re-pin the artifacts, follow these steps: \n \n " % user_provided_name +
@@ -1415,7 +1441,7 @@ def _coursier_fetch_impl(repository_ctx):
14151441
14161442 repository_ctx .file (
14171443 "unsorted_deps.json" ,
1418- content = v2_lock_file .render_lock_file (
1444+ content = v3_lock_file .render_lock_file (
14191445 lock_file_contents ,
14201446 inputs_hash ,
14211447 ),
@@ -1424,7 +1450,7 @@ def _coursier_fetch_impl(repository_ctx):
14241450 repository_ctx .report_progress ("Generating BUILD targets.." )
14251451 (generated_imports , jar_versionless_target_labels ) = parser .generate_imports (
14261452 repository_ctx = repository_ctx ,
1427- dependencies = v2_lock_file .get_artifacts (lock_file_contents ),
1453+ dependencies = v3_lock_file .get_artifacts (lock_file_contents ),
14281454 explicit_artifacts = {
14291455 a ["group" ] + ":" + a ["artifact" ] + (":" + a ["classifier" ] if "classifier" in a else "" ): True
14301456 for a in artifacts
0 commit comments