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

Make it possible to build with make --warn-undefined-variables #98197

Merged

Conversation

thockin
Copy link
Member

@thockin thockin commented Jan 20, 2021

/kind cleanup

When you run our build with undefined variable warnings in make, it flags a bunch of errors. In digging some of these are real bugs. After this set of commits, it is possible to rebuild k/k with --warn-undefined-variables and get no errors. I specifically said "rebuild". The initial build still flags errors since auto-generated variables have not yet been defined. I'll pursue that separately.

I suggest reading this commit-by-commit. The biggest change is renaming code-generator's "_examples" dir, and that can be discussed if need be.

NONE

@thockin thockin added priority/backlog Higher priority than priority/awaiting-more-evidence. area/build-release sig/release Categorizes an issue or PR as relevant to SIG Release. labels Jan 20, 2021
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jan 20, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: thockin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. area/code-generation sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. labels Jan 20, 2021
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
Copy link
Member

Choose a reason for hiding this comment

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

Are files in .../examples/_examples/... expected?

@lavalamp
Copy link
Member

I think the rename somehow got a duplicate nested _examples, can you check that? the other commits look good.

@thockin thockin force-pushed the makefile-unused-variables-watning branch 2 times, most recently from 94778a3 to 2595722 Compare January 20, 2021 01:02
@thockin
Copy link
Member Author

thockin commented Jan 20, 2021

Woops, bad rename indeed. Sorry

@liggitt
Copy link
Member

liggitt commented Jan 20, 2021

The dependency failure is legitimate, dropping the underscore makes the examples dir visible to go and sets up a module cycle

@thockin
Copy link
Member Author

thockin commented Jan 20, 2021

I'll look at all the fails, but I am missing the import cycle - where is that?

If we do not rename this, the alternatives I see are:

  1. special case staging/src/k8s.io/code-generator/_examples
  2. run codegen on any _name directory, which is just _examples in practice, but future

@thockin thockin force-pushed the makefile-unused-variables-watning branch from 2595722 to 5b2d608 Compare January 20, 2021 18:37
@liggitt
Copy link
Member

liggitt commented Jan 20, 2021

I'll look at all the fails, but I am missing the import cycle - where is that?

Oh, hrmm... I thought it was a cycle at the module level, but I guess it's not.

It does make k8s.io/code-generator depend on k8s.io/{api,apimachinery,client-go}, which will ripple to anyone consuming k8s.io/code-generator. Running update-vendor.sh makes this explicit:

diff --git a/staging/src/k8s.io/code-generator/go.mod b/staging/src/k8s.io/code-generator/go.mod
index 52aa67052e8..c0ac68c27c9 100644
--- a/staging/src/k8s.io/code-generator/go.mod
+++ b/staging/src/k8s.io/code-generator/go.mod
@@ -6,20 +6,23 @@ go 1.15
 
 require (
 	github.com/emicklei/go-restful v2.9.5+incompatible // indirect
+	github.com/go-openapi/spec v0.19.3
 	github.com/gogo/protobuf v1.3.1
-	github.com/google/go-cmp v0.5.2 // indirect
-	github.com/json-iterator/go v1.1.10 // indirect
 	github.com/mailru/easyjson v0.7.0 // indirect
 	github.com/spf13/pflag v1.0.5
-	github.com/stretchr/testify v1.6.1 // indirect
 	golang.org/x/mod v0.3.0 // indirect
-	golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
-	golang.org/x/text v0.3.4 // indirect
 	golang.org/x/tools v0.0.0-20200616133436-c1934b75d054 // indirect
-	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
+	k8s.io/api v0.0.0
+	k8s.io/apimachinery v0.0.0
+	k8s.io/client-go v0.0.0
 	k8s.io/gengo v0.0.0-20201214224949-b6c5ce23f027
 	k8s.io/klog/v2 v2.4.0
 	k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd
 )
 
-replace k8s.io/code-generator => ../code-generator
+replace (
+	k8s.io/api => ../api
+	k8s.io/apimachinery => ../apimachinery
+	k8s.io/client-go => ../client-go
+	k8s.io/code-generator => ../code-generator
+)

Technically, k8s.io/{api,apimachinery,client-go} probably should depend on k8s.io/code-generator, and their generation scripts should actually live beneath their source trees, but we sort of cheat by having the generation code live in the k8s.io/kubernetes root.

@thockin
Copy link
Member Author

thockin commented Jan 20, 2021

It does make k8s.io/code-generator depend on k8s.io/{api,apimachinery,client-go}, which will ripple to anyone consuming k8s.io/code-generator

I mean, it already DOES depend on those, it's just "hidden" in _examples.

Do you object to updating the vendoring on this?

@thockin thockin force-pushed the makefile-unused-variables-watning branch from 5b2d608 to b55a883 Compare January 20, 2021 20:37
@k8s-ci-robot k8s-ci-robot added the area/dependency Issues or PRs related to dependency changes label Jan 20, 2021
@liggitt
Copy link
Member

liggitt commented Jan 20, 2021

It does make k8s.io/code-generator depend on k8s.io/{api,apimachinery,client-go}, which will ripple to anyone consuming k8s.io/code-generator

I mean, it already DOES depend on those, it's just "hidden" in _examples.

The examples dir depends on those... but we don't want to expose all consumers of k8s.io/code-generator to dependencies they don't need (specifically k8s.io/api and k8s.io/client-go...)

Do you object to updating the vendoring on this?

I would probably isolate the examples dir with something like this:

diff --git a/staging/src/k8s.io/code-generator/examples/go.mod b/staging/src/k8s.io/code-generator/examples/go.mod
new file mode 100644
index 00000000000..5a969627e13
--- /dev/null
+++ b/staging/src/k8s.io/code-generator/examples/go.mod
@@ -0,0 +1,5 @@
+// This is a submodule to isolate k8s.io/code-generator from k8s.io/{api,apimachinery,client-go} dependencies in generated code
+
+module k8s.io/code-generator/examples
+
+go 1.15

@lavalamp
Copy link
Member

lavalamp commented Jan 20, 2021 via email

Because this comment is in a `define` which is later evaluated, the
syntactical `$(eval)` is treated like a variable exapansion.  Just
change the comment.

Driving towards `make --warn-undefined-variables`.
This makes them easier to see and find.

Driving towards `make --warn-undefined-variables`.
Driving towards `make --warn-undefined-variables`.
This same dep is expressed a few lines later in the "real" recipe.
Now we can use /dev/null as an argument when running tools manually
The script would get a 1 return code from grep when there are no files
in a directory, after filtering generated filenames.  This would cause
the script to exit unexpectedly.
The alternative to this would be to special-case code-generator.  Since
it legit wants codegen, it seems wrong to make it be _examples (which tools
should ignore).

Make examples an "internal module" so the main go.mod for
k8s.io/code-generator does not get too polluted.
This is a no-op now that _examples is renamed.
@thockin
Copy link
Member Author

thockin commented Jan 25, 2021

Rebased

@thockin thockin force-pushed the makefile-unused-variables-watning branch from c07026a to 6fe6941 Compare January 25, 2021 18:27
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 25, 2021
@liggitt
Copy link
Member

liggitt commented Jan 25, 2021

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 25, 2021
@k8s-ci-robot k8s-ci-robot merged commit 23303dd into kubernetes:master Jan 25, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.21 milestone Jan 25, 2021
nikhita added a commit to nikhita/publishing-bot that referenced this pull request Jan 26, 2021
Currently while zipping modules using the gomod-zip tool, if a repo
contains submodules, we include them in the zip archive.

However, when we do a go mod download, go skips submodules from the
zip archive. This means that the zip file created using gomod-zip
is no longer accurate.

This also causes the following error when go tries to read the zip file
(during `go mod tidy`, etc) and notices submodules in the zip archive:

```
-> unzip /go-workspace/pkg/mod/cache/download/k8s.io/code-generator/@v/v0.0.0-20210126111002-a3ae865d6348.zip: found go.mod file not in module root directory (k8s.io/code-generator@v0.0.0-20210126111002-a3ae865d6348/examples/go.mod)
unzip /go-workspace/pkg/mod/cache/download/k8s.io/code-generator/@v/v0.0.0-20210126111002-a3ae865d6348.zip: found go.mod file not in module root directory (k8s.io/code-generator@v0.0.0-20210126111002-a3ae865d6348/examples/go.mod)
```

This specific error occurs because submodules were recently added to
code-generator in kubernetes/kubernetes#98197.

To fix this issue, this commit ignores submodules while creating the
zip archive. The code is derived from https://github.com/golang/go/blob/b373d31c25e58d0b69cff3521b915f0c06fa6ac8/src/cmd/go/internal/modfetch/coderepo.go#L572
but since we already know the package name, this commit removes some
complexity from the original go code.
nikhita added a commit to nikhita/publishing-bot that referenced this pull request Jan 26, 2021
Currently while zipping modules using the gomod-zip tool, if a repo
contains submodules, we include them in the zip archive.

However, when we do a go mod download, go skips submodules from the
zip archive. This means that the zip file created using gomod-zip
is no longer accurate.

This also causes the following error when go tries to read the zip file
(during `go mod tidy`, etc) and notices submodules in the zip archive:
kubernetes/kubernetes#56876 (comment)

This specific error occurs because submodules were recently added to
code-generator in kubernetes/kubernetes#98197.

To fix this issue, this commit ignores submodules while creating the
zip archive. The code is derived from https://github.com/golang/go/blob/b373d31c25e58d0b69cff3521b915f0c06fa6ac8/src/cmd/go/internal/modfetch/coderepo.go#L572
but since we already know the package name, this commit removes some
complexity from the original go code.
@sttts
Copy link
Contributor

sttts commented Jan 26, 2021

With a new dependency order (am not a fan), has the publishing order been updated as well? Otherwise, we generate wrong go.mod/sum in the bot I believe cc @nikhita

@nikhita
Copy link
Member

nikhita commented Jan 26, 2021

With a new dependency order (am not a fan), has the publishing order been updated as well? Otherwise, we generate wrong go.mod/sum in the bot I believe cc @nikhita

The publishing-bot only looks at the root go.mod file today and cannot handle multi-modules. Since the new dependency order is in examples/go.mod (and not the root go.mod), even after updating the publishing order, the bot will not generate the correct examples/go.mod.

Right now, the bot will generate this for examples/go.mod in the published repo:

// This is a submodule to isolate k8s.io/code-generator from k8s.io/{api,apimachinery,client-go} dependencies in generated code

module k8s.io/code-generator/examples

go 1.15

require (
        github.com/go-openapi/spec v0.19.3
        k8s.io/api v0.0.0
        k8s.io/apimachinery v0.0.0
        k8s.io/client-go v0.0.0
        k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd
)

replace (
        k8s.io/api => ../../api
        k8s.io/apimachinery => ../../apimachinery
        k8s.io/client-go => ../../client-go
)

Are we fine with a wrong published go.mod file for the examples module since it's not meant to be imported? If yes, kubernetes/publishing-bot#244 can help unblock publishing for now.

cc @dims

nikhita added a commit to nikhita/publishing-bot that referenced this pull request Jan 26, 2021
Currently while zipping modules using the gomod-zip tool, if a repo
contains submodules, we include them in the zip archive.

However, when we do a go mod download, go skips submodules from the
zip archive. This means that the zip file created using gomod-zip
is no longer accurate.

This also causes the following error when go tries to read the zip file
(during `go mod tidy`, etc) and notices submodules in the zip archive:
kubernetes/kubernetes#56876 (comment)

This specific error occurs because submodules were recently added to
code-generator in kubernetes/kubernetes#98197.

To fix this issue, this commit ignores submodules while creating the
zip archive. The code is derived from https://github.com/golang/go/blob/b373d31c25e58d0b69cff3521b915f0c06fa6ac8/src/cmd/go/internal/modfetch/coderepo.go#L572
but since we already know the package name, this commit removes some
complexity from the original go code.
@liggitt
Copy link
Member

liggitt commented Jan 26, 2021

Are we fine with a wrong published go.mod file for the examples module since it's not meant to be imported?

incrementally, I think so... it was in a directory hidden to go before without any dependency information stated

@liggitt
Copy link
Member

liggitt commented Jan 26, 2021

With a new dependency order (am not a fan), has the publishing order been updated as well?

the goal of the submodule was to avoid touching dependency ordering for modules we intend people to use

nikhita added a commit to nikhita/publishing-bot that referenced this pull request Jan 26, 2021
Currently while zipping modules using the gomod-zip tool, if a repo
contains submodules, we include them in the zip archive.

However, when we do a go mod download, go skips submodules from the
zip archive. This means that the zip file created using gomod-zip
is no longer accurate.

This also causes the following error when go tries to read the zip file
(during `go mod tidy`, etc) and notices submodules in the zip archive:
kubernetes/kubernetes#56876 (comment)

This specific error occurs because submodules were recently added to
code-generator in kubernetes/kubernetes#98197.

To fix this issue, this commit ignores submodules while creating the
zip archive. The code is derived from https://github.com/golang/go/blob/b373d31c25e58d0b69cff3521b915f0c06fa6ac8/src/cmd/go/internal/modfetch/coderepo.go#L572
but since we already know the package name, this commit removes some
complexity from the original go code.
@thockin thockin deleted the makefile-unused-variables-watning branch August 2, 2021 04:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/build-release area/code-generation area/dependency Issues or PRs related to dependency changes area/release-eng Issues or PRs related to the Release Engineering subproject cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/release Categorizes an issue or PR as relevant to SIG Release. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants