When there are syntax errors in build files the na...
# general
a
When there are syntax errors in build files the name of the file that Pants is unable to parse is not printed. E.g:
Copy code
10:12:27.69 [ERROR] 1 Exception encountered:

Engine traceback:
  in `test` goal

SyntaxError: invalid syntax (BUILD, line 11)
Even with log level debug set, this is the full exception message. Is there a way to identify the filename easily?
1
g
Does
--print-stacktrace
add anything useful? Or
RUST_BACKTRACE=1
?
❤️ 1
(Or just
-ldebug
, maybe)
a
Oh, nice!
--print-stacktrace
is exactly what I'm looking for.
RUST_BACKTRACE=1 and ldebug had no effect on this, sadly.
g
Ack. If you can figure out ~where the error is found in Pants code, it'd be really nice to file an issue so we can maybe add more info, full path, etc.
(I guess you can file an issue no matter if we know where the error is found :D)
a
That's a good idea, I'll do that! 🙂
g
I did a quick grep but couldn't find us raising the error, so I figure it's a rethrow/uncaught exception somewhere.
a
Yeah, I guess it can happen for a multitude of reasons, depending on where in the BUILD file it encounters an error.
Copy code
_extract_env_vars(
  File "/home/mha/.cache/nce/3d6643e46b53e4cc0b2a0d5c768866226ddce3de1f57f80c4a02d8d39800fa8e/bindings/venvs/2.17.0/lib/python3.9/site-packages/pants/engine/internals/build_files.py", line 311, in _extract_env_vars
    env_vars = (*BUILDFileEnvVarExtractor.get_env_vars(file_content), *extra_env)
  File "/home/mha/.cache/nce/3d6643e46b53e4cc0b2a0d5c768866226ddce3de1f57f80c4a02d8d39800fa8e/bindings/venvs/2.17.0/lib/python3.9/site-packages/pants/engine/internals/build_files.py", line 207, in get_env_vars
    obj.visit(ast.parse(file_content.content, file_content.path))
  File "/home/mha/.cache/nce/f3ff38b1ccae7dcebd8bbf2e533c9a984fac881de0ffd1636fbb61842bd924de/cpython-3.9.18+20231002-x86_64-unknown-linux-gnu-install_only.tar.gz/python/lib/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flag
This is the final part of the stacktrace which ends with printing out exactly which file and line number it encountered an error on.
g
I meant more about where and which exact BUILD file, as a user-facing improvement
a
Yeah, no I got that, I was talking about how to intercept the error itself
g
Ah, sorry, yeah. I figure somewhere in build_files.py we could catch it and add some more info then. Probably should catch and abort with a custom error, not just bubble the syntax error.
a
Agreed, I remember this biting me in the ass early on as well, but thankfully at that point it was just like 5 build files to scan through.
Usually this happens when I get too ambitious with regex replacing across multiple BUILD files.
c
huh, so this is not an error from the BUILD file in the project root?
a
In this case it's an error from a deeply nested BUILD file when running
pants test ::
c
we've made a ton of improvements wrgt error messages and where they are defined.. guess this case slipped through...
a
Could be that I'm just behind on versions? I'm still running 2.17 here.
👀 1
g
Same on 2.19..
c
thanks Tom, yea the latest improvements are from 2.18.. but this is another case to fix as well then 🙂
g
Just looking at the compile call it says filename, so I'm imagining we only pass that. And since we seemingly just bubble the error there's no more info. I figure passing full path there is a 10-second fix with at least some UX improvements. For another 10-second fix, I think re-raising it as a custom error which includes cursor position would be awesome. Or just extracting the span into the error like regular Python does.
👍 2
c
file path info is definitely available, as we capture that for targets defined in that BUILD file... so just a matter of dressing the error properly.
g
For some reason the native impl of SyntaxError only shows basename so had to subclass it...