Skip to content

Conversation

ctcjab
Copy link
Contributor

@ctcjab ctcjab commented Sep 4, 2025

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:

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 than TypeError("unsupported operand type(s) for /: 'PosixPath' and 'NoneType').

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.
@rickeylev rickeylev changed the title type(fix): type error in copy_to_source.py fix(gazelle): report missing BUILD_WORKSPACE_DIRECTORY key more directly Sep 4, 2025
@rickeylev rickeylev changed the title fix(gazelle): report missing BUILD_WORKSPACE_DIRECTORY key more directly refactor(gazelle): report missing BUILD_WORKSPACE_DIRECTORY key more directly Sep 4, 2025
@rickeylev
Copy link
Collaborator

These changes allow rules_mypy users to also use rules_python_gazelle_plugin without having to work around the type error.

I don't follow. An error is still raised, so how does it help rules_mypy work better?

@rickeylev
Copy link
Collaborator

Thanks for the improvement!

@rickeylev rickeylev added this pull request to the merge queue Sep 4, 2025
Merged via the queue into bazel-contrib:main with commit 4977221 Sep 4, 2025
2 of 3 checks passed
@ctcjab
Copy link
Contributor Author

ctcjab commented Sep 5, 2025

Thanks for the improvement!

Sure thing!

I don't follow. An error is still raised, so how does it help rules_mypy work better?

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 bazel build to fail here. What causes bazel build to fail is that it runs mypy on this code, and the code had a type error (it tried to concatenate a Path object with a str | None object). By rewriting the code to avoid the possible None value, it caused mypy to consider this code type safe, which in turn resolved the bazel build error. Make sense?

@ctcjab ctcjab deleted the patch-1 branch September 6, 2025 20:52
@ctcjab
Copy link
Contributor Author

ctcjab commented Oct 7, 2025

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):

Screenshot 2025-10-07 at 4 00 36 PM

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?

@rickeylev
Copy link
Collaborator

We'll be doing a 1.7 release soon. Probably within a week.

@ctcjab
Copy link
Contributor Author

ctcjab commented Oct 8, 2025

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.

@aignas
Copy link
Collaborator

aignas commented Oct 9, 2025

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 runfiles library), so initially we could use something similar in our initial approach.

@ctcjab
Copy link
Contributor Author

ctcjab commented Oct 14, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants