-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Use python3 instead of python #2241
base: master
Are you sure you want to change the base?
Conversation
c818397
to
8a02798
Compare
8580e79
to
62d33d7
Compare
62d33d7
to
f769f65
Compare
@@ -240,6 +241,10 @@ if(BUILD_TESTING) | |||
manifest_parser_perftest | |||
) | |||
add_executable(${perftest} src/${perftest}.cc) | |||
set_source_files_properties(src/${perftest}.cc | |||
PROPERTIES | |||
COMPILE_DEFINITIONS NINJA_PYTHON="${NINJA_PYTHON}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if NINJA_PYTHON
is empty, because it not found by find_package(Python3 COMPONENTS Interpreter)
(and therefore Python3_EXECUTABLE
is empty)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea i think it would fail similarly to today, I can make it error in cmake if you'd like?
I just tried this PR on top of upstream, and this change hard-codes A better change would simply to change |
@digit-google it sounds like maybe you didn't read the review comment that explained why hardcoding python3 is broken on Windows and needs to be fixed by using find_package()?
It will certainly run in any environment that matches the one you built ninja on, which means if you build ninja on a CI bot and test it on the same bot, everything works. Who is "we" here, by the way? This PR passed the ninja project's CI. ;) |
Sorry, "we" in that specific case if the Fuchsia project, we have CI bots who run in an nsjail / container that doesn't have /usr/bin/python3 available (but have a vendored Python version in a different location, reachable from PATH, and this is done very intentionally to ensure the system version is never used by mistake). Still, you make the produced Ninja binary less portable for no good reason. Just changing Besides, I recommend you document the exact issue that require this change, since "a few stragglers" doesn't seem an accurate justification. |
As mentioned in the above review comments, changing it to python3 fixes some platforms where there is no "python" binary but in turn breaks the case of Windows with the Microsoft Store version of python, where there is only a "python" binary and no "python3" binary. Regardless, the name can be controlled by passing an explicit option to the build system so one could override it at compilation time with any choice of:
The question here is really just about what should be autodetected. Essentially this PR makes two changes:
|
Also note that the production binary (as opposed to say the perf testing binary) IIRC doesn't really use python for anything other than the webserver embedded tool, which I suspect isn't relevant to fuchsia anyway. So embedding paths from a build machine that aren't relevant to the final runtime machine may not matter in practice. |
I believe that hard-coding paths that are specific to the build machine in binaries that will be distributed and run on a different machine is nearly always a bad idea. This is the kind of stuff that will bite someone six months later, and will require hours or days of lost debugging time for developers who do not know enough about Ninja to solve this quickly (been there, done that, got the t-shirt :-)). At a minimum there should be a way to override that value at runtime. Using However, on systems that still link I suggest instead something a little bit more helpful:
This would be maximally portable, and will avoid wasting unknown amounts of developer time. |
A simpler alternative would be to detect python2 at runtime in the script, and print a user-friendly error message directly from it. |
That's a very load-bearing "that are specific to the build machine". On Linux and *BSD systems it is a very good idea -- such a good idea in fact that it may be mandated via policy. If you depend on something you should ensure you use the thing you depend on -- not something that unpredictably gets found first because e.g. the user added anaconda to their shell login profile. Certainly, Google may have problems that are atypical to the wider world of software development.
That's totally backwards IMO. The stated objection is that the command isn't guaranteed to exist. This is a trivially detectable issue -- it doesn't get easier to debug than "error: /usr/bin/python3: no such file or directory". There is nothing to debug, the only time spent will be time determining the right fix, whether that is trying to install the relevant interpreter or rebuilding ninja. In comparison I can attest to people spending weeks fruitlessly debugging the case where the answer in the end turned out to be "the wrong python3 was found on PATH, and as a result the program tried to run but mysteriously bombed out 50 miles away with a nonsensical error message that provided zero clues as to why it happened and was even actively obfuscating the real cause in 7 different ways". Bugs where your software fails to tell you there was a problem and instead runs for a bit and then spews copious stack traces unrelated to the root cause are the absolute worst. |
The current error message is more likely to look like: There is no opportunity to execute python code capable of detecting which version of python is being run. |
If you have such a policy, you can already implement it for the Ninja binaries that you produce by setting NINJA_PYTHON=/usr/bin/python3 in your CMake configuration step. However, there is no need to impose such a policy in the official Ninja binaries released through Github, to other people who do not need it, or would be affected negatively by it. And on Windows, where there is no standard installation location for the Python interpreter, the value embedded into the binary is essentially random (it depends too much on how the developer setup their system).
No, a missing interpreter is already detected and will print an error message (see browse.cc). The only plausable issue is that invoking Looking at the content of browse.py, it looks like only the shebang line has been changed to use So in the end, it looks like there is simply no use for this PR at all, as it doesn't fix anything, and likely regresses the tool on Windows. |
This fixes a few stragglers after 6a17e84
f769f65
to
a3e7e9e
Compare
Updated to swap |
@keith , thanks for updating the PR. This looks good to me, but https://peps.python.org/pep-0394/#exclusion-of-ms-windows seems to state that We may want to cleanup the browse.py from Python2-specific code paths, but I'll let @jhasse comment on that. And by the way, I just tried the previous version on my machine, with my PATH pointing to a non-standard location for the Python interpreter, and the build did embed that non-system location into the final executable. Hence the problem I described for Windows also existed for Unix anyway. In conclusion, embedding auto-detected host paths into binaries is still a terrible idea. Having a policy of enforcing standard locations like |
The plausible issue is exactly that it will print an error message, in a circumstance where it is trivially proven that the default is bad and, as you say, user-hostile -- and the correct answer is to use python3.
It fixes the default error message in browse.cc on macOS, as has been repeatedly pointed out to you. In exchange, it adds a default error message in fuchsia, and
in the version that hardcodes "python3" instead of "python" also regresses the tool on Windows, where Microsoft provides "python" and doesn't provide "python3". Yes, Microsoft provides a standard installation location for python.exe! It is part of the operating system. It chainloads into the Microsoft Store installable redistributable. Honestly I get the feeling that your stance is anything that works on fuchsia is the one true way and people who have issues in other environments are imagining it. I understand that embedding paths is occasionally awkward. My belief is that although your usual environment has this as the norm, it's rarely the norm elsewhere, since Linux, *BSD, macOS, and Windows all have canonical locations where if python3 is installed using the OS mechanism, the binary will exist in that precise path. If the purpose of a default is to make the most users happy, it makes sense to optimize for the default system locations and expect non-default users to configure it to suit themselves. Another approach which you haven't mentioned in your eagerness to say nothing should be done at all -- detect whether Python is available as "python3" or as "python" and set that as the default. I'm not sure how to do that in cmake. It is already what configure.py does, though. I'm very loudly refraining from making any comment about cmake users and the build system scripts they write. |
It looks like changing
Seems to fix |
what about checking for python3 on the PATH and if it isn't fallback to python - during runtime? |
Because the PATH at build time may not be the same when the produced Ninja binary is actually run. |
Ah sorry, I misread your message. Doing runtime checks should be ok in theory. But doing that properly on all platforms is non trivial through, iirc. Adding an option |
For example on Windows, you would have to check the registry to do things correctly: https://learn.microsoft.com/en-us/windows/win32/shell/app-registration?redirectedfrom=MSDN#finding-an-application-executable :-( |
Windows is also additionally interesting because you can run But the "py" tool isn't installed by default, whereas "python" is. If you're going to search anything on Windows, you'd probably want to reimplement |
This fixes a few stragglers after 6a17e84