Page MenuHomePhabricator

Output of scap lint.py isn't helpful if it fails the php -l check
Closed, ResolvedPublic

Description

Testing the fixes for T270452: Add more preflight checks to makerelease2

Invalid JSON:

Linting PHP and JSON files for sanity...
Traceback (most recent call last):
  File "/var/www/wiki/mediawiki/tools/release/make-release/scap_lint.py", line 71, in check_valid_json_file
    json.load(json_file)
  File "/usr/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 7 column 2 (char 168)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./makerelease2.py", line 269, in <module>
    main()
  File "./makerelease2.py", line 264, in main
    archive(args.repository, args.tag, args.output_dir, args.previous, sign=args.sign,
  File "./makerelease2.py", line 95, in archive
    scap_lint.check_valid_syntax('.', procs=multiprocessing.cpu_count())
  File "/var/www/wiki/mediawiki/tools/release/make-release/scap_lint.py", line 57, in check_valid_syntax
    check_valid_json_file(abspath)
  File "/var/www/wiki/mediawiki/tools/release/make-release/scap_lint.py", line 73, in check_valid_json_file
    raise ValueError("%s is an invalid JSON file" % path)
ValueError: ./composer.json is an invalid JSON file

That works.

Text before <?php

Linting PHP and JSON files for sanity...
Traceback (most recent call last):
  File "./makerelease2.py", line 269, in <module>
    main()
  File "./makerelease2.py", line 264, in main
    archive(args.repository, args.tag, args.output_dir, args.previous, sign=args.sign,
  File "./makerelease2.py", line 95, in archive
    scap_lint.check_valid_syntax('.', procs=multiprocessing.cpu_count())
  File "/var/www/wiki/mediawiki/tools/release/make-release/scap_lint.py", line 56, in check_valid_syntax
    check_php_opening_tag(abspath)
  File "/var/www/wiki/mediawiki/tools/release/make-release/scap_lint.py", line 115, in check_php_opening_tag
    raise ValueError("%s has content before opening <?php tag" % path)
ValueError: ./index.php has content before opening <?php tag

That works too.

But if we make an invalid php file (index.php again)....

Linting PHP and JSON files for sanity...
Traceback (most recent call last):
  File "./makerelease2.py", line 269, in <module>
    main()
  File "./makerelease2.py", line 264, in main
    archive(args.repository, args.tag, args.output_dir, args.previous, sign=args.sign,
  File "./makerelease2.py", line 95, in archive
    scap_lint.check_valid_syntax('.', procs=multiprocessing.cpu_count())
  File "/var/www/wiki/mediawiki/tools/release/make-release/scap_lint.py", line 50, in check_valid_syntax
    subprocess.check_call(cmd, shell=True)
  File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'find -O2 '.' -not -type d -name '*.php' -not -name 'autoload_static.php'  -or -name '*.inc' | xargs -n1 -P2 -exec php -l >/dev/null 2>&1' returned non-zero exit status 124.

This error isn't very helpful, beyond knowing something is seemingly wrong :(

Event Timeline

The "problem" is >/dev/null 2>&1

If we run it without the redirection, we do get something useful:

No syntax errors detected in ./docs/doxygen_first_page.php
No syntax errors detected in ./img_auth.php
PHP Parse error:  syntax error, unexpected '$mediaWiki' (T_VARIABLE) in ./index.php on line 42
Errors parsing ./index.php
No syntax errors detected in ./tests/phpunit/MediaWikiTestCase.php
xargs: php: exited with status 255; aborting

But obviously we don't care about hundreds of lines with No syntax errors detected

We could capture the output in Python and filter it on error?

might just need to remove the 2>&1 part, could use some experimentation.

Change 657930 had a related patch set uploaded (by Reedy; owner: Reedy):
[mediawiki/tools/release@master] scap_lint.py: Output errors from php -l

https://gerrit.wikimedia.org/r/657930

The above ends up with

PHP Parse error:  syntax error, unexpected end of file in test.php on line 4
xargs: php: exited with status 255; aborting

The xargs line is a bit of extra noise, but we at least get the error...

PS4

reedy@ubuntu64-web-esxi:/var/www/wiki/mediawiki/tools/release/make-release$ cat test.php
<?php

test
reedy@ubuntu64-web-esxi:/var/www/wiki/mediawiki/tools/release/make-release$ python3 test.py 
PHP Parse error:  syntax error, unexpected end of file in test.php on line 4
reedy@ubuntu64-web-esxi:/var/www/wiki/mediawiki/tools/release/make-release$ cat test.php 
<?php

test
reedy@ubuntu64-web-esxi:/var/www/wiki/mediawiki/tools/release/make-release$ python3 test.py 
Linting PHP and JSON files for sanity...
Failed
PHP Parse error:  syntax error, unexpected end of file in test.php on line 4
reedy@ubuntu64-web-esxi:/var/www/wiki/mediawiki/tools/release/make-release$ nano test.php 
reedy@ubuntu64-web-esxi:/var/www/wiki/mediawiki/tools/release/make-release$ cat test.php 
<?php

reedy@ubuntu64-web-esxi:/var/www/wiki/mediawiki/tools/release/make-release$ python3 test.py 
Linting PHP and JSON files for sanity...
Finished linting PHP and JSON files

Change 657832 had a related patch set uploaded (by Reedy; owner: Reedy):
[mediawiki/tools/scap@master] Better handling of scap_lint errors

https://gerrit.wikimedia.org/r/657832

What version of python is scap/scap tests running on? The patch I've done for tools/release is fine... But isn't gonna work for scap..

What version of python is scap/scap tests running on? The patch I've done for tools/release is fine... But isn't gonna work for scap..

python 2.7.13

And you're right: subprocess.run didn't exist in python2 so the solution could use some porting to Popen, I suppose.

I note that subprocess was added in Python 2.4 and that it's certainly in Python 2.7. In fact, I wish Scap used nothing else to run other programs.

I note that subprocess was added in Python 2.4 and that it's certainly in Python 2.7. In fact, I wish Scap used nothing else to run other programs.

Never mind, I misread the previous comment, which is about subprocess.run specifically. That was added on 3.5.

Change 657930 merged by jenkins-bot:

[mediawiki/tools/release@master] scap_lint.py: Output errors from `php -l`

https://gerrit.wikimedia.org/r/657930

Change 738024 had a related patch set uploaded (by Ahmon Dancy; author: Ahmon Dancy):

[mediawiki/tools/scap@master] lint.py: Supply useful output on lint fail

https://gerrit.wikimedia.org/r/738024

I ran into this today while experimenting, so I fixed it.

dancy changed the task status from Open to In Progress.Nov 10 2021, 11:12 PM

I ran into this today while experimenting, so I fixed it.

Thanks!

I was going to say when this is tested/verified/merged, we should sync it back into release tools... But is scap still running on oooold python?

I ran into this today while experimenting, so I fixed it.

Thanks!

I was going to say when this is tested/verified/merged, we should sync it back into release tools... But is scap still running on oooold python?

Scap 4.0.0+ uses Python 3.

I ran into this today while experimenting, so I fixed it.

Thanks!

I was going to say when this is tested/verified/merged, we should sync it back into release tools... But is scap still running on oooold python?

Scap 4.0.0+ uses Python 3.

Cool!

So we should be able to replace my hacky version to bring the files back into sync again

Change 657930 merged by jenkins-bot:

[mediawiki/tools/release@master] scap_lint.py: Output errors from `php -l`

https://gerrit.wikimedia.org/r/657930

Change 657832 abandoned by Reedy:

[mediawiki/tools/scap@master] [WIP] Better handling of scap_lint errors

Reason:

https://gerrit.wikimedia.org/r/657832

Change 738024 merged by jenkins-bot:

[mediawiki/tools/scap@master] lint.py: Supply useful output on lint fail

https://gerrit.wikimedia.org/r/738024

dancy claimed this task.

This change was included in scap 4.1.0 which was recently deployed.