Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go mod: Handle multi-line error messages #3383

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/oras@v0.10.0 requires\n"\
" github.com/docker/distribution@v0.0.0-00010101000000-000000000000: "\
"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() {
}