refactor(gazelle): report missing BUILD_WORKSPACE_DIRECTORY key more directly #3240
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.
Replace
os.environ.get("BUILD_WORKSPACE_DIRECTORY")
withos.environ["BUILD_WORKSPACE_DIRECTORY"]
.The former may return None if the environment variable is not set, in which case the code will crash with a TypeError when the line is run since the result is concatenated with a
pathlib.Path
object, and is therefore making it impossible to use rules_python_gazelle_plugin along with rules_mypy:These changes allow rules_mypy users to also use rules_python_gazelle_plugin without having to work around the type error.
Now if the environment variable is not set, the code will still crash, but now with an error that better indicates the failed precondition, namely
KeyError("BUILD_WORKSPACE_DIRECTORY")
rather thanTypeError("unsupported operand type(s) for /: 'PosixPath' and 'NoneType')
.