Skip to content

Commit

Permalink
Add debug puts during go.mod update process
Browse files Browse the repository at this point in the history
  • Loading branch information
mctofu committed Dec 23, 2021
1 parent 3d274fd commit c5389f6
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def update_files # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
in_repo_path do
# Map paths in local replace directives to path hashes
original_go_mod = File.read("go.mod")
puts "** original go.mod:\n#{original_go_mod}"
original_manifest = parse_manifest
original_go_sum = File.read("go.sum") if File.exist?("go.sum")

Expand All @@ -92,24 +93,30 @@ def update_files # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity

# Replace full paths with path hashes in the go.mod
substitute_all(substitutions)
puts "** substitute go.mod:\n#{File.read("go.mod")}"

# Bump the deps we want to upgrade using `go get lib@version`
run_go_get(dependencies)
puts "** updated dep go.mod:\n#{File.read("go.mod")}"

# Run `go get`'s internal validation checks against _each_ module in `go.mod`
# by running `go get` w/o specifying any library. It finds problems like when a
# module declares itself using a different name than specified in our `go.mod` etc.
run_go_get
puts "** updated go.mod:\n#{File.read("go.mod")}"

# If we stubbed modules, don't run `go mod {tidy,vendor}` as
# dependencies are incomplete
if substitutions.empty?
# go mod tidy should run before go mod vendor to ensure any
# dependencies removed by go mod tidy are also removed from vendors.
run_go_mod_tidy
puts "** tidy go.mod:\n#{File.read("go.mod")}"
run_go_vendor
puts "** vendor go.mod:\n#{File.read("go.mod")}"
else
substitute_all(substitutions.invert)
puts "** unsubstitute go.mod:\n#{File.read("go.mod")}"
end

updated_go_sum = original_go_sum ? File.read("go.sum") : nil
Expand All @@ -119,6 +126,7 @@ def update_files # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
original_go_version = original_go_mod.match(GO_MOD_VERSION)&.to_a&.first
updated_go_version = updated_go_mod.match(GO_MOD_VERSION)&.to_a&.first
if original_go_version != updated_go_version
puts "** adjusting go version #{original_go_version} -> #{updated_go_version}"
go_mod_lines = updated_go_mod.lines
go_mod_lines.each_with_index do |line, i|
next unless line&.match?(GO_MOD_VERSION)
Expand All @@ -132,6 +140,8 @@ def update_files # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
updated_go_mod = go_mod_lines.compact.join
end

puts "** version adjusted go.mod:\n#{updated_go_mod}"

{ go_mod: updated_go_mod, go_sum: updated_go_sum }
end
end
Expand All @@ -141,17 +151,23 @@ def run_go_mod_tidy

command = "go mod tidy -e"

puts "** run #{command}"

# we explicitly don't raise an error for 'go mod tidy' and silently
# continue here. `go mod tidy` shouldn't block updating versions
# because there are some edge cases where it's OK to fail (such as
# generated files not available yet to us).
Open3.capture3(ENVIRONMENT, command)
_, stderr, status = Open3.capture3(ENVIRONMENT, command)
puts "** #{command} failed\n#{stderr}" unless status.success?
end

def run_go_vendor
return unless vendor?

command = "go mod vendor"

puts "** run #{command}"

_, stderr, status = Open3.capture3(ENVIRONMENT, command)
handle_subprocess_error(stderr) unless status.success?
end
Expand All @@ -163,6 +179,8 @@ def run_go_get(dependencies = [])
!File.read(path).include?("// +build")
end

puts "** package: #{package}"

File.write(tmp_go_file, "package dummypkg\n") unless package

# TODO: go 1.18 will make `-d` the default behavior, so remove the flag then
Expand All @@ -174,6 +192,8 @@ def run_go_get(dependencies = [])
end
command = SharedHelpers.escape_command(command)

puts "** run #{command}"

_, stderr, status = Open3.capture3(ENVIRONMENT, command)
handle_subprocess_error(stderr) unless status.success?
ensure
Expand Down Expand Up @@ -201,6 +221,7 @@ def build_module_stubs(stub_paths)
# `go get -d` works, even if some modules have been `replace`d
# with a local module that we don't have access to.
stub_paths.each do |stub_path|
puts "** stubbing #{stub_path}"
Dir.mkdir(stub_path) unless Dir.exist?(stub_path)
FileUtils.touch(File.join(stub_path, "go.mod"))
FileUtils.touch(File.join(stub_path, "main.go"))
Expand All @@ -223,6 +244,7 @@ def replace_directive_substitutions(manifest)

def substitute_all(substitutions)
body = substitutions.reduce(File.read("go.mod")) do |text, (a, b)|
puts "** substitute #{a} -> #{b}"
text.sub(a, b)
end

Expand Down

0 comments on commit c5389f6

Please sign in to comment.