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 Validation check to see if func is already initialized #2574

Merged
merged 2 commits into from
Nov 15, 2024

Conversation

tarunsunny3
Copy link
Contributor

@tarunsunny3 tarunsunny3 commented Nov 6, 2024

Changes

This PR addresses an issue where calling func build without prior function initialization resulted in an irrelevant error message. Now, if func build is invoked before initialization, an appropriate error is shown, guiding users to initialize the function first.

/kind bug

Fixes #2280

Release Note

fix: wrong error message when running func commands if the function is not initialized

Docs


@knative-prow knative-prow bot added the kind/bug Categorizes issue or PR as related to a bug. label Nov 6, 2024
@knative-prow knative-prow bot requested review from nainaz and vyasgun November 6, 2024 03:52
Copy link

knative-prow bot commented Nov 6, 2024

Welcome @tarunsunny3! It looks like this is your first PR to knative/func 🎉

Copy link

knative-prow bot commented Nov 6, 2024

Hi @tarunsunny3. 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. Regular contributors should join the org to skip this step.

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.

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.

@knative-prow knative-prow bot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 6, 2024
@tarunsunny3 tarunsunny3 marked this pull request as draft November 6, 2024 03:55
@knative-prow knative-prow bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 6, 2024
@tarunsunny3 tarunsunny3 marked this pull request as ready for review November 6, 2024 03:56
@knative-prow knative-prow bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 6, 2024
@matejvasek matejvasek requested review from Leo6Leo, gauron99 and lkingland and removed request for nainaz and vyasgun November 6, 2024 07:49
@matejvasek
Copy link
Contributor

/ok-to-test

@knative-prow knative-prow bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Nov 6, 2024
Copy link

codecov bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 65.38%. Comparing base (53428b1) to head (d9a9159).
Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
cmd/build.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2574      +/-   ##
==========================================
+ Coverage   58.78%   65.38%   +6.59%     
==========================================
  Files         130      130              
  Lines       15479    15485       +6     
==========================================
+ Hits         9100    10125    +1025     
+ Misses       5514     4385    -1129     
- Partials      865      975     +110     
Flag Coverage Δ
e2e-test 35.89% <0.00%> (?)
e2e-test-oncluster 32.80% <0.00%> (?)
e2e-test-oncluster-runtime 28.64% <0.00%> (?)
e2e-test-runtime-go 26.44% <0.00%> (?)
e2e-test-runtime-node 25.85% <0.00%> (?)
e2e-test-runtime-python 25.85% <0.00%> (?)
e2e-test-runtime-quarkus 26.00% <0.00%> (?)
e2e-test-runtime-rust 25.01% <0.00%> (?)
e2e-test-runtime-springboot 25.05% <0.00%> (?)
e2e-test-runtime-typescript 25.95% <0.00%> (?)
integration-tests 51.89% <0.00%> (+2.24%) ⬆️
unit-tests 50.86% <0.00%> (?)
unit-tests-macos-latest ?
unit-tests-ubuntu-latest ?
unit-tests-windows-latest ?

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.

cmd/build.go Outdated
}
if !hasFunc {
return c, fmt.Errorf("no function has been initialized in the current directory. Please initialize a function by running either:\n- func init --language <your language>\n- func create <function name> --language <your_language>")
}
f, err := fn.NewFunction(c.Path)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should not this already return ENOENT?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could simply check f.Initialized() instead of introducing new IsFunctionInitialized() function.

Copy link
Contributor

Choose a reason for hiding this comment

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

TBH I am surprised that the function returns err==nil when there is no function. Why it does that @lkingland ?

// If no func.yaml in directory, return the default function which will
// have f.Initialized() == false
var filename = filepath.Join(root, FunctionFile)
if _, err = os.Stat(filename); err != nil {
if os.IsNotExist(err) {
err = nil
}
return
}

Copy link
Contributor

Choose a reason for hiding this comment

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

@lkingland also if you mean zero-value by "default function" , then you also need to zero the path of the f.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, good question. A Function is first a structure in memory, which can be serialized to disk. The path to where the Function is, or will be when initialized, is the only value provided in the constructor, and thus is the only field that is populated with user data when creating a new function from scratch.

Like you say. Just check if f.Initialized().

Copy link
Contributor

Choose a reason for hiding this comment

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

I still do not like, but can live with it. Just the function docstring says:

NewFunction from a given path. Invalid paths, or no function at path are errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

I bet that if you asked 10 programmers what NewFunction(pathWithNoFunction) returns at least 9 would tell it's an error.

cmd/build.go Outdated Show resolved Hide resolved
Copy link
Member

@lkingland lkingland left a comment

Choose a reason for hiding this comment

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

Thanks @tarunsunny3 for finding out where this check needs to be!

I made a couple of suggestions to 1) use the pre-existing f.Initialzied() method and 2) print the path of the function in the error message (it's not always in the current directory)

Thanks for the contribution.

cmd/build.go Outdated Show resolved Hide resolved
cmd/build.go Outdated Show resolved Hide resolved
pkg/functions/client.go Outdated Show resolved Hide resolved
@lkingland
Copy link
Member

Gentle ping @tarunsunny3 . We would love to merge your PR whenever you're ready to make those minor changes. 👍🏻

@tarunsunny3
Copy link
Contributor Author

tarunsunny3 commented Nov 15, 2024

@lkingland
Sorry I have been busy with my coursework lately
I just tried it with just f.Initialized() but for some reason c.Path is giving empty output as supposed to give the current directory's path if I am correct

Is it fine if it gives an empty output?
image

We could just remove c.Path from the error message altogether or debug why c.Path is empty?

@knative-prow knative-prow bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 15, 2024
@matejvasek
Copy link
Contributor

/ok-to-test
/approve
/lgtm

@knative-prow knative-prow bot added the lgtm Indicates that a PR is ready to be merged. label Nov 15, 2024
Copy link

knative-prow bot commented Nov 15, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: matejvasek, tarunsunny3

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

@knative-prow knative-prow bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 15, 2024
@knative-prow knative-prow bot merged commit bf9a70a into knative:main Nov 15, 2024
39 checks passed
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. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Wrong error message when running func commands if the function is not initialized
3 participants