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

Add a vendor make target for updating go module files #108

Merged
merged 3 commits into from
Feb 28, 2023

Conversation

elezar
Copy link
Contributor

@elezar elezar commented Feb 9, 2023

This change adds a vendor make target that runs go mod tidy for the root module as well as each of the cmd submodules.

The vendor target is adapted from the containerd makefile.

This also converts cmd/validate to a submodule.

@elezar elezar force-pushed the add-mod-tidy-tooling branch from bca3201 to e26b6e6 Compare February 9, 2023 13:17
@elezar elezar requested a review from klihub February 9, 2023 14:15
@elezar elezar force-pushed the add-mod-tidy-tooling branch from e26b6e6 to d9288a2 Compare February 9, 2023 14:17
@elezar elezar requested a review from bart0sh February 9, 2023 15:24
Makefile Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
@elezar elezar force-pushed the add-mod-tidy-tooling branch 2 times, most recently from f624832 to 8815bf2 Compare February 10, 2023 13:35
@elezar elezar requested a review from klihub February 10, 2023 13:35
@elezar elezar force-pushed the add-mod-tidy-tooling branch 2 times, most recently from 1eb3ae9 to dbf0a40 Compare February 10, 2023 14:16
Makefile Show resolved Hide resolved
Makefile Outdated
MOD_TIDY_TARGETS := $(patsubst %,mod-tidy-%,$(MOD_TARGETS))
MOD_VERIFY_TARGETS := $(patsubst %,mod-verify-%,$(MOD_TARGETS))

$(MOD_TIDY_TARGETS): mod-tidy-%:
Copy link
Contributor

@klihub klihub Feb 13, 2023

Choose a reason for hiding this comment

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

What is the purpose of mod-tidy-% here in $(MOD_TIDY_TARGETS): mod-tidy-% ? AFAICT, it will evaluate to empty.

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 is how pattern targets are constructed. The % in the rule matches the last part of the target and allows $(*) to be used in the commands.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried checking what, for instance, that mod-tidy-% evaluates to in the $(MOD_TIDY_TARGETS): mod-tidy-% rule with something like this:

$(MOD_TIDY_TARGETS): mod-tidy-%:
	echo "*** prerequisites for '$@' => '$^' ***"
	$(Q)echo "Running $@... in $(abspath ./cmd/$(*))"
	$(Q)(cd $(abspath ./cmd/$(*)) && ${GO} mod tidy)

Copy link
Contributor

Choose a reason for hiding this comment

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

That's not how pattern targets are constructed IMO. In a pattern target there must be a % (exactly one) in the target. Then you can have one or more % in the prerequisites which are then replaced with the same stem. In $(MOD_TIDY_TARGETS) there are 0 %s. That's where my whole confusion is coming from. The way how I originally misread you rule definition definitely does not make any sense. The way how I now more carefully read your rule... I don't understand what it means.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the rule as written:

$(MOD_TIDY_TARGETS): mod-tidy-%: 

there are no prerequisites defined. This is a static pattern rule of the form:

targets ...: target-pattern: dep-patterns ...
        commands

In this case, dep-patterns is empty.

From the docs:

The target-pattern and dep-patterns say how to compute the dependencies of each target. Each target is matched against the target-pattern to extract a part of the target name, called the stem. This stem is substituted into each of the dep-patterns to make the dependency names (one from each dep-pattern).

If we then expand this we have:

mod-tidy-cdi mod-tidy-validate: mod-tidy-%:

Meaning that this will effectively result in two "rules" with the stem matching cdi and validate respectively. This ensures that the commands associated with the rule have $(*) set to cdi and validate repspectively.

As a note, I use these kinds of rules in our Makefiles to cover cases where we are adding new commands, for example. Here, I generate the top-level targets using wildcards and use replacements and static pattern rules to define generic targets. This means that no make changes are required to add new commands.

If we think that this is excessive for this repo, I can expand these rules instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

there are no prerequisites defined. This is a static pattern rule of the form:

Thank you ! That is exactly the snippet I was looking for in the same piece of documentation yesterday, but I failed to find it.

Makefile Outdated
$(Q)echo "Running $@... in $(abspath ./cmd/$(*))"
$(Q)(cd $(abspath ./cmd/$(*)) && ${GO} mod tidy)

$(MOD_VERIFY_TARGETS): mod-verify-%: mod-tidy-%
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of mod-verify-% mod-tidy-% here ? I think they will evaluate to empty. Shouldn't this be instead

$(MOD_VERIFY_TARGETS): $(MOD_TIDY_TARGETS)

Copy link
Contributor

Choose a reason for hiding this comment

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

That said, I think strictly in the make sense something like mod-verify-%: mod-tidy-% is more correct: "whenever verifying a module, tidy it up first"...

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 means that each mod-verify-% target depends on its specific mod-tidy-% target. This means that for each one tidy is run before verify.

I don't think that we need to enforce the global dependencies here i.e: before ANY verify target is run, run ALL tidy targets.

Copy link
Contributor

Choose a reason for hiding this comment

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

But a simple mod-verify-%: mod-tidy-% would already cover that dependency. There is no need for the $(MOD_VERIFY_TARGETS): there in the beginning.

And I actually misread that whole line before posting my original comment. I thought it said $(MOD_VERIFY_TARGETS): mod-verify-% mod-tidy-%...

But even now with that more careful look, I have to say that I still don't understand what does that extra $(MOD_VERIFY_TARGETS): accomplishes there... Actually and I wasn't even aware you could write a rule like that... or if I ever did I've long forgotten since.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the initial part the pattern rules are not matched. Afk at the moment, so let me find a docs link tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makefile Outdated Show resolved Hide resolved
@elezar elezar force-pushed the add-mod-tidy-tooling branch from dbf0a40 to 5829772 Compare February 13, 2023 09:38
@elezar elezar requested a review from klihub February 13, 2023 09:48
Copy link
Contributor

@klihub klihub left a comment

Choose a reason for hiding this comment

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

I'm still confused about whether this makes sense and what does the extra variable-target in the beginning means:

$(CMD_MOD_TIDY_TARGETS): mod-tidy-%: mod-tidy-root

But if others (know and) are fine with that then I'm fine with it too.
@bart0sh ?

.gitignore Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
Makefile Show resolved Hide resolved
@elezar elezar force-pushed the add-mod-tidy-tooling branch from 5829772 to 0139ef8 Compare February 14, 2023 08:40
@bart0sh
Copy link
Contributor

bart0sh commented Feb 14, 2023

looks good to me overall.

One thing though. Do we need this level of granularity? Would it be enough and simpler to have only 2 targets mod-tidy and mod-verify that would just walk through all modules dirs (something like $(git ls-files '*go.mod' | xargs dirname)) and run either go mod tidy or go mod verify for all of them?

Signed-off-by: Evan Lezar <[email protected]>
…s are up to date

This change adds mod-tidy and mod-verify make targets that mirror what is done for
containerd. This should ensure that the module files in the root as well as in
subfolders are up to date.

Signed-off-by: Evan Lezar <[email protected]>
@elezar
Copy link
Contributor Author

elezar commented Feb 20, 2023

@elezar

Does this guarantee ordering? We would need the commands to be run in the root before the sub-folders. Like I said, I'm > happy to change the way we are doing things here if we feel strongly about it.

It does guarantee it if directories are sorted:

$ git ls-files '*go.mod' | xargs dirname | sort
.
cmd/cdi
cmd/validate

I don't feel strongly about it though. I think this could be simpler and clearer this way, but I'm generally ok with your version.

@elezar elezar force-pushed the add-mod-tidy-tooling branch from 0139ef8 to b86c208 Compare February 20, 2023 08:49
@elezar
Copy link
Contributor Author

elezar commented Feb 28, 2023

@bart0sh @klihub anything that we feel still needs to be done here?

I can re-implement this if we feel strongly about it, otherwise I'd like to get this merged.

@klihub
Copy link
Contributor

klihub commented Feb 28, 2023

@bart0sh @klihub anything that we feel still needs to be done here?

I can re-implement this if we feel strongly about it, otherwise I'd like to get this merged.

No too strong feelings. I'm fine with getting it merged.

@klihub klihub merged commit af32687 into cncf-tags:main Feb 28, 2023
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.

3 participants