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

fix: Fail more gracefully when pip --dry-run doesn't work #2476

Merged
merged 15 commits into from
Jan 18, 2023

Conversation

metabiswadeep
Copy link
Contributor

Fixes #2463

@metabiswadeep metabiswadeep marked this pull request as draft December 30, 2022 06:18
@metabiswadeep
Copy link
Contributor Author

@BreadGenie @terriko could anyone review this please?

yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")
except subprocess.CalledProcessError as e:
self.logger.error(f"Status : FAIL {e.returncode} {e.output}")
Copy link
Contributor

Choose a reason for hiding this comment

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

You're definitely going to want a more user-friendly error message here. Probably something like
"$filename not scanned: pip --dry-run was unable to get package versions"
"pip version >= ${whatever version dry-run was added in} is required to scan python requirements files"

That said, it's giving a warning that report may change, so we may need to plan around that as much as we can. I'm not sure if that's why your tests are failing, but you might need to take a look at what report is giving you and see if it changed in the past month.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you please elaborate on what you mean by report and how can I access it?

Copy link
Contributor

@terriko terriko Jan 5, 2023

Choose a reason for hiding this comment

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

If you look up a few lines in the code, you can see how pip is being run:

                    "pip3",
                    "install",
                    "-r",
                    self.filename,
                    "--dry-run",
                    "--ignore-installed",
                    "--report",
                    "-",
                    "--quiet",

When you run that same command manually, you'll get the "report" in question.

When I run it, it generates a warning about report not yet producing a stable API. It's possible my version of pip is a few weeks old and this has been fixed. But basically if they change pip's output, we may need to change our code, so if you happened to get a version that was a bit different it could explain some test breakages. Or it could be unrelated. I just wanted to make sure you knew that we can't currently assume that what we get from pip won't change.

Copy link
Contributor

Choose a reason for hiding this comment

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

And if it is fixed and pip says they've got some sort of stable report since I last checked in December, we probably want to explicitly make that version-or-later of pip a requirement.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's possible my version of pip is a few weeks old and this has been fixed.

It's still experimental as of pip 22.3.1. Docs say this:

The report is a JSON object with the following properties:
version: the string 0, denoting that the installation report is an experimental feature. This value will change to 1, when the feature is deemed stable after gathering user feedback (likely in pip 22.3 or 23.0).

Still experimental in 22.3 and I don't see any mention of it in 23.0 milestone. At least one change is expected before it becomes stable.

Copy link
Contributor Author

@metabiswadeep metabiswadeep Jan 5, 2023

Choose a reason for hiding this comment

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

Yes, for me as well, report is giving that warning --report is currently an experimental option. The output format may change in a future release without prior warning .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps we can make pip upgrade to the latest version if the error is thrown?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And later if report is removed in a future version of pip, we could make pip to the last pip version which supported report.

Copy link
Contributor

@Molkree Molkree Jan 7, 2023

Choose a reason for hiding this comment

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

Perhaps we can make pip upgrade to the latest version if the error is thrown?

I don't think it's a good idea to update anything during execution, it's an unusual and undesirable side effect.

And later if report is removed in a future version of pip, we could make pip to the last pip version which supported report.

I doubt it will be removed anytime soon, it's not even stable yet.

pip is a very important part of the Python ecosystem, doing any manipulations with it at runtime and even just limiting its version as a cve-bin-tool dependency is highly undesirable IMO. Requiring at least some version that has --report added/stabilized is different.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @Molkree - we MUST not auto-update any of the installed software. Given the fact that the --report option is still experimental, I wonder if we should be (a) checking that the installed version of pip supports the option (although this could be simply done by specifyng the minimum version of pip in the requirements.txt file) (b) marking this option as 'EXPERIMENTAL' (there is an environment variable for this that I added on another PR) and only invoke if requested and not by default.

I noticed the warning from pip. We should possibly be catching the output from the subprocess call (stderr, stdout) before passing this to the json loader.

@codecov-commenter
Copy link

Codecov Report

Merging #2476 (dda8039) into main (fb9cca7) will increase coverage by 0.01%.
The diff coverage is 76.92%.

@@            Coverage Diff             @@
##             main    #2476      +/-   ##
==========================================
+ Coverage   76.92%   76.93%   +0.01%     
==========================================
  Files         591      591              
  Lines        9736     9741       +5     
  Branches     1323     1323              
==========================================
+ Hits         7489     7494       +5     
  Misses       1947     1947              
  Partials      300      300              
Flag Coverage Δ
longtests 76.93% <76.92%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cve_bin_tool/parsers/python.py 89.58% <76.92%> (-5.77%) ⬇️
cve_bin_tool/cli.py 63.98% <0.00%> (+0.96%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@Molkree Molkree left a comment

Choose a reason for hiding this comment

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

Not the purpose of this PR but urgh, who uses pip3 in 2023, it should be python -m pip.

f"{filename} not scanned: pip --dry-run was unable to get package versions."
)
self.logger.error(
"pip version >= 22.2 is required to scan python requirements files"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"pip version >= 22.2 is required to scan python requirements files"
"pip version >= 22.2 is required to scan Python requirements files."

We should not log this error every time, what if the scan wasn't successful for some other reason? After all, we can check the pip version and log only if pip is not new enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should still log the underlying error in addition to the user-friendly message? Unless we know for sure pip is too old, then there's no point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This error always arises due to an older version of pip I think because in newer versions, no such error arises.

cve_bin_tool/parsers/python.py Outdated Show resolved Hide resolved
@Molkree
Copy link
Contributor

Molkree commented Jan 7, 2023

I think tests on Windows are failing due to fb9cca7.

@metabiswadeep metabiswadeep marked this pull request as ready for review January 7, 2023 10:01
@metabiswadeep
Copy link
Contributor Author

Not the purpose of this PR but urgh, who uses pip3 in 2023, it should be python -m pip.

@Molkree Don't know what's up with this, but CI gives FileNotFoundError: [Errno 2] No such file or directory: 'python -m pip'.

@terriko
Copy link
Contributor

terriko commented Jan 11, 2023

Not the purpose of this PR but urgh, who uses pip3 in 2023, it should be python -m pip.

Hah, I have the same feeling every time I read ... I think it's the packaging docs? But the fact that they're written like that even though they've been updated recently make me think that someone does indeed use pip3 still.

@metabiswadeep
Copy link
Contributor Author

@terriko @anthonyharrison @Molkree Is this okay now?

Copy link
Contributor

@terriko terriko left a comment

Choose a reason for hiding this comment

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

Looks good, test failures appear to be unrelated. (type should be fixed in main, windows will still be broken)

@terriko terriko merged commit b21f142 into intel:main Jan 18, 2023
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.

Fail more gracefully when pip --dry-run doesn't work
5 participants