Skip to content

fix(cmd): reorder invoke validation to check Initialized before Validate#3849

Open
Elvand-Lie wants to merge 1 commit into
knative:mainfrom
Elvand-Lie:fix/invoke-validation-ordering
Open

fix(cmd): reorder invoke validation to check Initialized before Validate#3849
Elvand-Lie wants to merge 1 commit into
knative:mainfrom
Elvand-Lie:fix/invoke-validation-ordering

Conversation

@Elvand-Lie
Copy link
Copy Markdown
Contributor

Fixes #3848

Changes

In runInvoke(), f.Validate() was called before checking f.Initialized(). Every other command (run, deploy, delete, describe, subscribe) checks Initialized() immediately after NewFunction(). The invoke command was the only one that validated first.

This caused users running func invoke in a non-function directory to see a confusing validation error (e.g., func.yaml contains errors) instead of the helpful "no function found" message.

Additionally, the Validate error was printed via fmt.Printf AND returned to cobra, causing double error output.

What this PR does:

  1. Move !f.Initialized() check before f.Validate() — matching every other command
  2. Remove fmt.Printf double error output on validation failure
  3. Preserve the format mismatch warning in its logical position (after validation)

Testing

The reordering ensures the !f.Initialized() guard runs first, returning the user-friendly error message. The f.Validate() call only runs on initialized functions where it can produce meaningful diagnostics.

/kind bug

@knative-prow knative-prow Bot added the kind/bug Bugs label May 25, 2026
@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 25, 2026 15:46
@knative-prow knative-prow Bot added size/XS 🤖 PR changes 0-9 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 25, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 25, 2026

Hi @Elvand-Lie. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 53.47%. Comparing base (e7d7dcf) to head (d73aa6a).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3849      +/-   ##
==========================================
+ Coverage   53.44%   53.47%   +0.02%     
==========================================
  Files         200      200              
  Lines       23450    23431      -19     
==========================================
- Hits        12533    12529       -4     
+ Misses       9662     9654       -8     
+ Partials     1255     1248       -7     
Flag Coverage Δ
e2e 33.65% <0.00%> (-0.07%) ⬇️
e2e go 29.56% <0.00%> (-0.10%) ⬇️
e2e node 25.86% <0.00%> (-0.10%) ⬇️
e2e python 29.90% <0.00%> (-0.10%) ⬇️
e2e quarkus 25.97% <0.00%> (-0.10%) ⬇️
e2e rust 25.44% <0.00%> (-0.10%) ⬇️
e2e springboot 24.10% <0.00%> (-0.10%) ⬇️
e2e typescript 25.96% <0.00%> (-0.10%) ⬇️
e2e-config-ci 26.80% <0.00%> (+0.02%) ⬆️
integration 15.77% <0.00%> (+0.02%) ⬆️
unit macos-14 42.29% <100.00%> (+0.06%) ⬆️
unit macos-latest 42.29% <100.00%> (+0.06%) ⬆️
unit ubuntu-24.04-arm 42.59% <100.00%> (+0.06%) ⬆️
unit ubuntu-latest 43.15% <100.00%> (+0.06%) ⬆️
unit windows-latest 42.35% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gauron99
Copy link
Copy Markdown
Contributor

/ok-to-test

@knative-prow knative-prow Bot added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels May 26, 2026
@gauron99
Copy link
Copy Markdown
Contributor

Other commands (run, build, deploy, describe, delete, logs) use NewErrNotInitializedFromPath(f.Root, "invoke") from cmd/errors.go instead of a raw fmt.Errorf. This should use the same wrapper for consistency.

@gauron99
Copy link
Copy Markdown
Contributor

/assign

@Elvand-Lie Elvand-Lie force-pushed the fix/invoke-validation-ordering branch from 1ac2196 to 2b1e9d6 Compare May 27, 2026 18:07
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 27, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Elvand-Lie
Once this PR has been reviewed and has the lgtm label, please ask for approval from gauron99. For more information see the Code Review Process.

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

Details 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

@knative-prow knative-prow Bot added size/M 🤖 PR changes 30-99 lines, ignoring generated files. and removed size/XS 🤖 PR changes 0-9 lines, ignoring generated files. labels May 27, 2026
@Elvand-Lie Elvand-Lie force-pushed the fix/invoke-validation-ordering branch from 2b1e9d6 to 5c026dc Compare May 27, 2026 18:34
@Elvand-Lie
Copy link
Copy Markdown
Contributor Author

@gauron99 pushed an update to match the repository's established error and validation patterns:

  1. Reordered runInvoke() to check f.Initialized() before f.Validate(), aligning with build, deploy, and run.
  2. Replaced the raw fmt.Errorf with NewErrNotInitializedFromPath(f.Root, "invoke").
  3. Added the "invoke" case to cmd/errors.go to provide the standard CLI error output.
  4. Added TestInvoke_NotInitialized to verify this exact flow.
  5. Fixed formatting with goimports to pass the CI style check.

Let me know if my solution fits.

@gauron99 gauron99 requested review from Copilot and removed request for dsimansk and jrangelramos May 28, 2026 10:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR makes func invoke report the same not-initialized error style as other commands when run outside a function directory, instead of validating an uninitialized function first.

Changes:

  • Checks f.Initialized() before f.Validate() in runInvoke().
  • Adds an invoke-specific ErrNotInitialized help message.
  • Adds a unit test for invoking from a non-function directory.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
cmd/invoke.go Reorders initialization validation before function validation.
cmd/errors.go Adds the invoke-specific not-initialized error message.
cmd/invoke_test.go Covers the not-initialized invoke error path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/invoke.go Outdated
- Move !f.Initialized() check before f.Validate(), matching every other command (build, deploy, run, delete, describe)
- Replace raw fmt.Errorf with NewErrNotInitializedFromPath(f.Root, invoke) for consistency
- Remove fmt.Printf double error output on validation failure
- Add invoke case to cmd/errors.go for standardized CLI error messaging
- Add TestInvoke_NotInitialized to verify the correct error type and message
@Elvand-Lie Elvand-Lie force-pushed the fix/invoke-validation-ordering branch from 6c68f64 to d73aa6a Compare May 28, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Bugs ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/M 🤖 PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmd: invoke checks f.Validate() before f.Initialized(), producing confusing errors

3 participants