-
-
Notifications
You must be signed in to change notification settings - Fork 636
refactor(gazelle): report missing BUILD_WORKSPACE_DIRECTORY key more directly #3240
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
Conversation
Replace `os.environ.get("BUILD_WORKSPACE_DIRECTORY")`
with `os.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:
INFO: Analyzed target //:gazelle_python_manifest.update (63 packages loaded, 10415 targets configured).
INFO: Found 1 target...
ERROR: /.../BUILD:52:24: mypy //:gazelle_python_manifest.update failed: (Exit 1): mypy failed: ...
external/rules_python_gazelle_plugin+/manifest/copy_to_source.py:23: error: Unsupported operand types for / ("None" and "Path") [operator]
external/rules_python_gazelle_plugin+/manifest/copy_to_source.py:23: note: Left operand is of type "str | None"
Found 1 error in 1 file (checked 1 source file)
Target //:gazelle_python_manifest.update failed to build
Aspect //tools:aspects.bzl%mypy_aspect of //:gazelle_python_manifest.update failed to build
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, i.e. KeyError("BUILD_WORKSPACE_DIRECTORY") rather than TypeError.
I don't follow. An error is still raised, so how does it help rules_mypy work better? |
|
Thanks for the improvement! |
Sure thing!
In case it's still helpful, an error is still raised at runtime if the env var isn't set, but that has nothing to do with what is causing |
|
Hi @rickeylev, even though this was merged a month ago, I just hit this again running with the latest release of rules_python (1.6.3, from 2 weeks ago):
From https://github.com/bazel-contrib/rules_python/commits/1.6.3/gazelle/manifest/copy_to_source.py it looks like this still hasn't made it into a release version. Is there anything else I can do to help get this released? |
|
We'll be doing a 1.7 release soon. Probably within a week. |
|
Oh nice, thanks @rickeylev! One follow-up thought, is it worth adding a ticket for e.g. incorporating rules_mypy into this repo's own CI, to keep new type errors from being introduced? I can create the issue if you're open to it. |
|
I personally am not against doing type checking against all of the python code within the repo, but that is a fair amount of work that would definitely benefit from community also pitching in. I'll create a place-holder ticket that is reasonably wide (and we already have a CI job that is doing type checking on the |
|
Thanks @rickeylev, great you're open to that. I didn't see the placholder ticket get created yet so I just created #3353 -- hope that's helpful. |

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.Pathobject, 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').