Open aschleck opened 1 year ago
create symlinks with the correct names pointing to the real folders
I have not tried, but I have had many problems with symlinks and Windows. So we may not want to go down this path.
Directory symlinks aka junctions have been available on Windows for a long time and are relied upon by Bazel quite heavily. They could work here.
A simple "solution" that is recommended for Bazel-aware Python packages is to move everything into a top-level directory with the desired name. Since repo roots are on the Python path, this should allow existing absolute imports to continue to work.
@rickeylev in case you're not aware of this. We discussed this problem around a year ago I think, and I think your recommendation was to set up a PyPI-esque import dir merging all needed repos (and forgoing repo names altogether). Could that be adapted to help in @aschleck's case? Is this process documented?
cc @meteorcloudy
Not having the repo name in the import name is ideal, yes.
That said, requiring that to switch to bzlmod is a rather big hurdle.
Having the repo mapping available during analysis would open a few more options.
Hmmm. There might be another way out, too. Not 100% sure on this.
Basically, a user creates a directory with the desired import name. In that dir, they create an init.py file. They set the imports attribute of the target to ..
(they can nest an additional directory to avoid additional pollution). Within the init, there's a few options. It could add to __path__
, which will make imports of submodules search those added paths. Another option is to manually load and exec the original higher level file and replace itself in sys.modules. This has the advantage of avoiding a temp dir with symlinks. It is also something a library can do, so doesn't require a binary to do anything special.
Symlinks would also work. They'll require additional changes to the startup script, plus cleanup logic, though a user's main could also be written to do the equiv before running the real main module code.
This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!
I no longer work at the company where this matters but it still is a thing
This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!
Still a thing as far as I know
Another idea: add something to the end of sys.path as a last-chance way to handle these. By adding to the end, we can minimize interfering with other import dirs. Could generate a dir with shim files (using __path__
), or an import hook.
Also, in case it wasn't clear: the main thing preventing progress on this issue is available time. There's several ideas, but someone has to try something to see how well it works or doesn't work.
π bug report
Affected Rule
The issue is caused by the rule: `py_binary`/`py_library` under bzlmod ### Is this a regression? Yes, this works under the WORKSPACE model. It is only bzlmod that is broken. ### Description When two Bazel workspaces (with one depending on the other) both define packages with the same name (`common`, `core`, `helpers`, `private`, `util`, ...) the outer one will shadow the inner one. Without bzlmod this has always been solvable by using absolute imports with the workspace name as the prefix. However, under bzlmod, these absolute imports no longer work: when a module is used as an outer repository the prefix is `_main` but when a module is used as a dependency repository it is `