DAOS-18933 ci: fix PermissionError in pre-commit clang-format hook#18183
DAOS-18933 ci: fix PermissionError in pre-commit clang-format hook#18183
Conversation
When extra.py is invoked standalone by the pre-commit hook, SCons replaces WhereIs() with a stub that returns '' instead of None. The previous guard (if clang_exe is None) let the empty string through, causing subprocess.check_output(['', '-version']) to raise: PermissionError: [Errno 13] Permission denied: '' Fix this by falling back to shutil.which() when WhereIs() returns a falsy value, and replacing the None check with a falsy guard (if not clang_exe) to cover both None and empty-string cases. Signed-off-by: Cedric Koch-Hofer <cedric.koch-hofer@hpe.com>
|
Ticket title is 'Pre-commit clang-format hook crashes with PermissionError' |
| def _get_version_string(): | ||
| clang_exe = WhereIs('clang-format') | ||
| if clang_exe is None: | ||
| clang_exe = WhereIs('clang-format') or shutil.which('clang-format') |
There was a problem hiding this comment.
Why you decided to use both? Why not just replace the unreliable one with a reliable one?
There was a problem hiding this comment.
SCons.Script ships a no-op stub for WhereIs that is active whenever the module is imported outside a full SCons build (i.e. the pre-commit hook context).
On my machine:
>>> from SCons.Script import WhereIs; WhereIs('clang-format')
'' # stub always returns ''
>>> import shutil; shutil.which('clang-format')
'/usr/bin/clang-format' # stdlib finds it correctlyReplacing WhereIs entirely would fix the crash but lose the SCons-environment-aware lookup during actual builds. The or keeps WhereIs when it works and falls back to shutil.which when the stub is in effect.
There was a problem hiding this comment.
It is a little messy. 😅
Another thought. Why not use shutil.which('clang-format') in the WhereIs stub?
There was a problem hiding this comment.
It is a little messy. 😅
Another thought. Why not use
shutil.which('clang-format')in theWhereIsstub?
Did not wanted to touch to the fake_scons stubs as I am not sure of their purpose.
I am going to investigate as I agree that it should be a better solution: I have to check that I am not breaking anything (test, etc.).
Description
Fixes DAOS-18933 — the pre-commit clang-format hook crashes with a
PermissionErroron any machine where the SConsWhereIsstub is active.Problem
extra.pyis used in two contexts:SCons.Script.WhereIsis the real implementation that searchesPATH.WhereIswith a no-op stub:The previous guard only checked for
None:Because
''is notNone, the guard was bypassed andsubprocess.check_outputwas called with an empty string as the executable, raising:
This hit every developer running
git commitwhen the stubWhereIswas in effect.The clang-format check was silently skipped, but the alarming traceback caused confusion.
Fix
shutil.which('clang-format')(Python stdlib, no SCons dependency)when
WhereIsreturns a falsy value.is Noneguard with a falsy guard (if not clang_exe) to cover bothNoneand empty-string cases.shutil.whichcorrectly searchesos.environ['PATH']in both contexts.Changed files
site_scons/site_tools/extra/extra.pyshutilimport; useor shutil.which()fallback; replaceis Nonewith falsy guardSteps for the author:
After all prior steps are complete: