Skip to content

Commit

Permalink
Merge pull request #3383 from dependabot/feelepxyz/fix-go-mod-errors
Browse files Browse the repository at this point in the history
Go mod: Handle multi-line error messages
  • Loading branch information
feelepxyz authored Mar 29, 2021
2 parents 8357ef0 + 7d45586 commit 417b78c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
24 changes: 14 additions & 10 deletions go_modules/lib/dependabot/go_modules/file_updater/go_mod_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,20 @@ def substitute_all(substitutions)
write_go_mod(body)
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/PerceivedComplexity
def handle_subprocess_error(stderr)
stderr = stderr.gsub(Dir.getwd, "")

# Package version doesn't match the module major version
error_regex = RESOLVABILITY_ERROR_REGEXES.find { |r| stderr =~ r }
if error_regex
lines = stderr.lines.drop_while { |l| error_regex !~ l }
raise Dependabot::DependencyFileNotResolvable, lines.join
error_message = filter_error_message(message: stderr, regex: error_regex)
raise Dependabot::DependencyFileNotResolvable, error_message
end

repo_error_regex = REPO_RESOLVABILITY_ERROR_REGEXES.find { |r| stderr =~ r }
if repo_error_regex
lines = stderr.lines.drop_while { |l| repo_error_regex !~ l }
ResolvabilityErrors.handle(lines.join, credentials: credentials)
error_message = filter_error_message(message: stderr, regex: repo_error_regex)
ResolvabilityErrors.handle(error_message, credentials: credentials)
end

path_regex = MODULE_PATH_MISMATCH_REGEXES.find { |r| stderr =~ r }
Expand All @@ -296,16 +294,22 @@ def handle_subprocess_error(stderr)

out_of_disk_regex = OUT_OF_DISK_REGEXES.find { |r| stderr =~ r }
if out_of_disk_regex
lines = stderr.lines.select { |l| out_of_disk_regex =~ l }
raise Dependabot::OutOfDisk.new, lines.join
error_message = filter_error_message(message: stderr, regex: out_of_disk_regex)
raise Dependabot::OutOfDisk.new, error_message
end

# We don't know what happened so we raise a generic error
msg = stderr.lines.last(10).join.strip
raise Dependabot::DependabotError, msg
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/AbcSize

def filter_error_message(message:, regex:)
lines = message.lines.select { |l| regex =~ l }
return lines.join if lines.any?

# In case the regex is multi-line, match the whole string
message.match(regex).to_s
end

def go_mod_path
return "go.mod" if directory == "/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,39 @@
end
end
end

context "for an unknown revision version" do
let(:project_name) { "unknown_revision_version" }
let(:dependency_name) do
"github.com/deislabs/oras"
end
let(:dependency_version) { "v0.10.0" }
let(:dependency_previous_version) { "v0.9.0" }
let(:requirements) do
[{
file: "go.mod",
requirement: dependency_version,
groups: [],
source: {
type: "default",
source: "github.com/deislabs/oras"
}
}]
end
let(:previous_requirements) { [] }

it "raises the correct error" do
error_class = Dependabot::DependencyFileNotResolvable
expect { updater.updated_go_sum_content }.
to raise_error(error_class) do |error|
expect(error.message).to include(
"go: github.com/deislabs/[email protected] requires\n"\
" github.com/docker/[email protected]: "\
"invalid version: unknown revision"
)
end
end
end
end

describe "#updated_go_sum_content" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/dependabot/vgotest

go 1.16

require (
github.com/deislabs/oras v0.9.0
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
_ "github.com/deislabs/oras"
)

func main() {
}

0 comments on commit 417b78c

Please sign in to comment.