Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Support main_module with bootstrap_impl=system_python #3072

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

brandonchinn178
Copy link
Contributor

main_module was added in #2671, but support was only added for bootstrap_impl=script. Despite only supporting bootstrap_impl=script, both the CHANGELOG and the documentation didn't mention that limitation, making it seem like main_module doesn't work out-of-the-box (given that bootstrap_impl=system_python is the default). See #2765 (comment).

This PR adds support for main_module with bootstrap_impl=system_python. This is categorized as feat because it's new functionality, but it can also be conceptualized as a fix, since it's bringing the feature in line with docs.

@brandonchinn178 brandonchinn178 force-pushed the bchinn-main-module branch 2 times, most recently from 9cc661a to ffed3fb Compare July 9, 2025 19:44
Copy link
Collaborator

@rickeylev rickeylev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and test!

aside: if you grant maintainer permssions to edit, i can directly do some of the small things.

@@ -127,11 +128,22 @@ py_reconfig_test(
name = "main_module_test",
srcs = ["main_module.py"],
bootstrap_impl = "script",
imports = ["."],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be kept because it ensures the correct entry is added to sys.path. Without it, it can inadvertently rely on the implicit behavior from the flag that adds repos to sys.path and the PYTHONSAFEPATH setting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can re-add it, but I don't understand the reasoning. IIUC, imports says "add this directory to PYTHONPATH", but we're specifying main_module with the full absolute path, so imports = ["."] isn't doing anything. Would imports = [] have the behavior you want?

@@ -0,0 +1,9 @@
"""
A shim to get `main_module` working with `bootstrap_impl=system_python`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a shim like this, modify the bootstrap template. All it should really need is to populate a %main_module% variable and pass -m when invoking python. That should simplify things quite a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm I could try it again, but it was difficult because then I'd have to update all the locations where %main% is used, since there isn't a main file anymore. It seemed easier to say "we're using the main file logic everywhere still, it just points to a shim"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it seems like the notion of a main file is hardcoded in a lot of places in python_bootstrap_template.txt. It's also not immediately clear how the translation to -m <main_module> would work for the coverage path. The shim seems like the easiest solution, especially given this is a deprecated codepath and it should have a negligible performance impact

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants