Skip to content
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

echo $? doesnt give me a '0' in windows. #2478

Open
kairin opened this issue Aug 18, 2024 · 2 comments
Open

echo $? doesnt give me a '0' in windows. #2478

kairin opened this issue Aug 18, 2024 · 2 comments

Comments

@kairin
Copy link

kairin commented Aug 18, 2024

Okay sorry i know there's another closed thread mentioning something similar... but even when i use the 1.12.1 version... i still get the error of non 0 when i key in "$?"

#2212
#2211
(scikit-build/ninja-python-distributions#157 (comment))

what other information should i look at to troubleshoot this?

C:\Users\kairi\gitclones>cd ninja

C:\Users\kairi\gitclones\ninja>dir
 Volume in drive C has no label.

 Directory of C:\Users\kairi\gitclones\ninja

18/08/2024  13:29    <DIR>          .
18/08/2024  13:29    <DIR>          ..
18/08/2024  13:29             1,080 .clang-format
18/08/2024  13:29               643 .clang-tidy
18/08/2024  13:29               163 .editorconfig
18/08/2024  13:29    <DIR>          .github
18/08/2024  13:29               670 .gitignore
18/08/2024  13:29               891 appveyor.yml
18/08/2024  13:29            10,365 CMakeLists.txt
18/08/2024  13:29            28,653 configure.py
18/08/2024  13:29             1,480 CONTRIBUTING.md
18/08/2024  13:29            11,560 COPYING
18/08/2024  13:29    <DIR>          doc
18/08/2024  13:29    <DIR>          misc
18/08/2024  13:29             2,677 README.md
18/08/2024  13:29             1,588 RELEASING.md
18/08/2024  13:29    <DIR>          src
18/08/2024  13:29    <DIR>          windows
              11 File(s)         59,770 bytes
               7 Dir(s)  41,561,018,368 bytes free

C:\Users\kairi\gitclones\ninja>git checkout release
branch 'release' set up to track 'origin/release'.
Switched to a new branch 'release'

C:\Users\kairi\gitclones\ninja>"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.11.0
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Users\kairi\gitclones\ninja>py configure.py --bootstrap
bootstrapping ninja...
warning: A compatible version of re2c (>= 0.15.3) was not found; changes to src/*.in.cc will not affect your build.
depfile_parser.cc
lexer.cc
build.cc
build_log.cc
clean.cc
clparser.cc
debug_flags.cc
deps_log.cc
disk_interface.cc
dyndep.cc
dyndep_parser.cc
edit_distance.cc
eval_env.cc
graph.cc
graphviz.cc
json.cc
line_printer.cc
manifest_parser.cc
metrics.cc
missing_deps.cc
parser.cc
state.cc
status.cc
string_piece_util.cc
util.cc
version.cc
subprocess-win32.cc
includes_normalize-win32.cc
msvc_helper-win32.cc
msvc_helper_main-win32.cc
minidump-win32.cc
getopt.c
ninja.cc
Generating code
Finished generating code
wrote build.ninja.
bootstrap complete.  rebuilding...
[35/35] LINK ninja.exe
Generating code
Finished generating code

C:\Users\kairi\gitclones\ninja>ninja --help
usage: ninja [options] [targets...]

if targets are unspecified, builds the 'default' target (see manual).

options:
  --version      print ninja version ("1.12.1")
  -v, --verbose  show all command lines while building
  --quiet        don't show progress status, just command output

  -C DIR   change to DIR before doing anything else
  -f FILE  specify input build file [default=build.ninja]

  -j N     run N jobs in parallel (0 means infinity) [default=8 on this system]
  -k N     keep going until N jobs fail (0 means infinity) [default=1]
  -l N     do not start new jobs if the load average is greater than N
  -n       dry run (don't run commands but act like they succeeded)

  -d MODE  enable debugging (use '-d list' to list modes)
  -t TOOL  run a subtool (use '-t list' to list subtools)
    terminates toplevel options; further flags are passed to the tool
  -w FLAG  adjust warnings (use '-w list' to list warnings)

C:\Users\kairi\gitclones\ninja>ninja --version
1.12.1

C:\Users\kairi\gitclones\ninja>echo $?
$?

C:\Users\kairi\gitclones\ninja>
@mcprat
Copy link
Contributor

mcprat commented Aug 19, 2024

The answer is simple, google is your friend...

When developing on Windows, you have to always keep in mind that it is posixly-stupid

$? is a POSIX standardized environment variable for exit status that does not exist in Windows, unless you were to create an alias for it on your own (but Windows might limit the type of characters that can be in a variable).

The first clue that you're not actually accessing a variable is that the content after echo is exactly the same as what is printed.
The variables in the command prompt are wrapped in % instead of starting with $

most of the IEEE documentation for $? is here, almost nothing in the following document applies to Windows:
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html

You have to access a variable called %errorlevel% instead:
https://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line

@mcprat
Copy link
Contributor

mcprat commented Aug 19, 2024

2 other things to keep in mind...

exit status is always a number, not just zero or "non-zero", so $? as a result is obviously invalid.

the exit status only applies to what is EXACTLY preceding the print of the exit status, not just the last "major" command or "only if there is a failure". If your example were to actually print the exit status when you intended to, it would only be testing the result of ninja --version and nothing else.

example:

$ something
-bash: something: command not found
$ echo $?
127

$ something; true
-bash: something: command not found
$ echo $?
0

$ true; something
-bash: something: command not found
$ echo $?
127

$ ^C
$ echo $?
130

$ something
-bash: something: command not found
$ ^C
$ echo $?
130

$ true
$ ^C
$ echo $?
130

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

No branches or pull requests

2 participants