✨ Quality: shutil.which() return value not checked before os.path.dirname() - #5413
Open
Manhhoangvp95 wants to merge 2 commits into
Open
Conversation
…ame()
On non-Windows platforms, `shutil.which("kubectl")` returns `None` if the
executable is not found. The code then calls `os.path.dirname(full_path)`
which will raise `TypeError: expected str, bytes or os.PathLike object, not NoneType`.
This will crash at runtime when kubectl is not installed.
Affected files: file_utils.py
Signed-off-by: Manhhoangvp95 <87350762+Manhhoangvp95@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Improves robustness of the installer’s kubectl path discovery by handling the case where shutil.which("kubectl") returns None on non-Windows platforms, preventing an immediate TypeError from os.path.dirname(None).
Changes:
- Add a
Noneguard aroundshutil.which("kubectl")inget_kubectl_directory()with a fallback directory.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+76
to
+78
| full_path = shutil.which("kubectl") | ||
| if full_path is None: | ||
| return "/usr/local/bin" |
There was a problem hiding this comment.
Please add a unit test covering the shutil.which("kubectl") is None branch (and the expected behavior you choose—exception vs fallback). This logic is easy to regress and the repo already has unit test coverage for other installer/common helpers.
louiseschmidtgen
requested changes
May 5, 2026
louiseschmidtgen
left a comment
Contributor
There was a problem hiding this comment.
Hi @Manhhoangvp95,
Thank you for contributing to MicroK8s.
- 1 on copilots comment: Let's not try to guess where the
kubectlinstallation lives ("/usr/local/bin") and instead fail with a sensible error.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✨ Code Quality
Problem
On non-Windows platforms,
shutil.which("kubectl")returnsNoneif theexecutable is not found. The code then calls
os.path.dirname(full_path)which will raise
TypeError: expected str, bytes or os.PathLike object, not NoneType.This will crash at runtime when kubectl is not installed.
Severity:
highFile:
installer/common/file_utils.pySolution
Add a check for None:
if full_path is None: raise RuntimeError("kubectl not found")or return a sensible default.
Changes
installer/common/file_utils.py(modified)Summary
Changes
Testing
Possible Regressions
Checklist
Notes
Closes #5412