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_modules: don't raise error for go mod tidy #2830

Merged
merged 6 commits into from
Dec 10, 2020

Conversation

fatih
Copy link
Contributor

@fatih fatih commented Dec 8, 2020

go mod tidy makes sure that go.mod matches the source code in a Go
module. This also means that dependabot is doing a quassi linting on
the code, which prevents updating versions in a go.mod if the source
code doesn't got build. One particular use case is when a project is
missing generated files. Sometimes people don't submit and commit the
generated files of a code generator (i.e: protobuf) and do it in
pre-script in the CI. Because dependabot is not aware of this
pre-script, it'll run go mod tidy and fail for the user.

We should silently continue if go mod tidy fails and not raise an
error. This will allow dependabot still to run go mod tidy, but will
not block users for these kind of edge cases.

@fatih fatih requested a review from a team as a code owner December 8, 2020 20:10
@fatih fatih requested a review from feelepxyz December 8, 2020 20:12
# 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).
return if istidy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still fail with Dependabot::DependencyFileNotResolvable and Dependabot::GoModulePathMismatch when running a go mod tidy. Initially I wanted to move up this line so it never fails, but it looks we have some test cases that cover these two specific error types. I know this you PR fixes one particular case, but wonder if we should also not take care of Dependabot::DependencyFileNotResolvable and Dependabot::GoModulePathMismatch ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We run go get (here) before we run go mod tidy. With that in mind, if there's something in the manifest that would trigger a Dependabot::DependencyFileNotResolvable error or a Dependabot::GoModulePathMismatch error, can we reasonably assume that those errors would have occurred during go get? If so, then we know that we won't even get to the go mod tidy step in that case. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point Jason. go get would definitely catch some of these (especially the Dependabot::DependencyFileNotResolvable errors).

Comment on lines 125 to 126
_, stderr, status = Open3.capture3(ENVIRONMENT, command)
handle_subprocess_error(stderr) unless status.success?
handle_subprocess_error(stderr, true) unless status.success?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me like we're trying to silently ignore any problems that occur during go mod tidy. If that's accurate, then instead of adding a new arg to handle_subprocess_error, would it make sense to skip the invocation handle_subprocess_error entirely?

Suggested change
_, stderr, status = Open3.capture3(ENVIRONMENT, command)
handle_subprocess_error(stderr) unless status.success?
handle_subprocess_error(stderr, true) unless status.success?
# Attempt command, but ignore any errors
Open3.capture3(ENVIRONMENT, command)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense and I think we should use this idiom. Initially I changed this line and removed handling the error, but I wasn't sure about some of the edge cases and whether we should return the specific error types described above. I'll update the tests as well to accommodate the changes 👍🏼

@@ -118,9 +118,15 @@ def update_files # rubocop:disable Metrics/AbcSize
def run_go_mod_tidy
return unless tidy?

# NOTE(arslan): use `go mod tidy -e` once Go 1.16 is out:
# https://github.com/golang/go/commit/3aa09489ab3aa13a3ac78b1ff012b148ffffe367
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@feelepxyz
Copy link
Contributor

@fatih left a spec comment, otherwise LGTM! 🙌 The unrelated spec failures should be fixed on main now

fatih and others added 4 commits December 9, 2020 10:12
`go mod tidy` makes sure that go.mod matches the source code in a Go
module. This also means that `dependabot` is doing a quassi linting on
the code, which prevents updating versions in a go.mod if the source
code doesn't got build. One particular use case is when a project is
missing generated files. Sometimes people don't submit and commit the
generated files of a code generator (i.e: `protobuf`) and do it in
pre-script in the CI. Because `dependabot` is not aware of this
pre-script, it'll run `go mod tidy` and fail for the user.

We should silently continue if `go mod tidy` fails and not raise an
error. This will allow dependabot still to run `go mod tidy`, but will
not block users for these kind of edge cases.
…ter_spec.rb

Co-authored-by: Philip Harrison <philip@mailharrison.com>
@fatih fatih force-pushed the fatih/silently-fail-go-mod-tidy branch from 4998941 to c3995ee Compare December 9, 2020 18:12
error_class = Dependabot::DependencyFileNotResolvable
expect { updater.updated_go_sum_content }.
to raise_error(error_class) do |error|
to_not raise_error(error_class) do |error|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 @fatih: Can you help me understand why this behavior is changing in this pull request? Since we're only changing code related to go mod tidy, it's not yet clear to me why this test needs to change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular change was caused previously by go mod tidy. Because we don't return an error anymore, this test fails as it expects an error. So I either had to remove it, or change it so it doesn't return an error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @fatih. Since #2627 introduced this test, and since that PR doesn't mention anything about go mod tidy, I'd like to ask @jurre to take a look before we ship this PR.

@jurre: Given your context on #2627, should we be changing this test here, or would it make more sense to delete it entirely?

Copy link
Contributor

@feelepxyz feelepxyz Dec 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this case is only caught by go mod tidy where the import path doesn't match the exported package name, import here:

_ "github.com/googleapis/gnostic/OpenAPIv2"

Exported as openapi_v2: https://github.com/google/gnostic/blob/ed308846fc77db7667936d27d7ac1fb278b74216/openapiv2/OpenAPIv2.go#L17

I think we can delete this test case and rely on the below updates the go.mod as this would fail if we raised a resolvability error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, @feelepxyz is right 👍👍

Copy link
Contributor

@feelepxyz feelepxyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 pushed a tiny change to the test, seemed like a good idea to keep it around for future.

@feelepxyz feelepxyz merged commit 8c45d5a into main Dec 10, 2020
@feelepxyz feelepxyz deleted the fatih/silently-fail-go-mod-tidy branch December 10, 2020 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants