Skip to content

Commit a32f1bd

Browse files
committed
Refactor GoModUpdater to improve go.sum reconciliation logic and update GoModGraph type annotation
1 parent 6b4b62e commit a32f1bd

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

go_modules/lib/dependabot/go_modules/file_updater/go_mod_graph.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# typed: strict
1+
# typed: strong
22
# frozen_string_literal: true
33

44
require "sorbet-runtime"

go_modules/lib/dependabot/go_modules/file_updater/go_mod_updater.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,8 @@ def update_files
230230
updated_go_sum = original_go_sum ? File.read("go.sum") : nil
231231
updated_go_mod = File.read("go.mod")
232232

233-
# Restore go.sum lines that were removed for modules unrelated
234-
# to the update — Go tooling can over-prune /go.mod checksums.
235233
if original_go_sum && updated_go_sum
236-
graph_after = GoModGraph.capture
237-
unless graph_before.empty? || graph_after.empty?
238-
updated_go_sum = reconcile_go_sum(
239-
original_go_sum,
240-
updated_go_sum,
241-
graph_before.changed_modules(graph_after)
242-
)
243-
end
234+
updated_go_sum = reconcile_updated_go_sum(original_go_sum, updated_go_sum, graph_before)
244235
end
245236

246237
{ go_mod: updated_go_mod, go_sum: updated_go_sum }
@@ -423,6 +414,22 @@ def in_repo_path(&block)
423414
end
424415
end
425416

417+
# Captures the post-update module graph, diffs it against the
418+
# pre-update graph, and restores over-pruned go.sum lines.
419+
sig do
420+
params(
421+
original_go_sum: String,
422+
updated_go_sum: String,
423+
graph_before: GoModGraph
424+
).returns(String)
425+
end
426+
def reconcile_updated_go_sum(original_go_sum, updated_go_sum, graph_before)
427+
graph_after = GoModGraph.capture
428+
return updated_go_sum if graph_before.empty? || graph_after.empty?
429+
430+
reconcile_go_sum(original_go_sum, updated_go_sum, graph_before.changed_modules(graph_after))
431+
end
432+
426433
# Restores /go.mod checksum lines in go.sum that were removed for
427434
# modules unrelated to the dependency update. Go tooling can
428435
# over-prune these entries, causing CI failures downstream.

0 commit comments

Comments
 (0)