Skip to content

Commit 259993b

Browse files
committed
python-setup: Handle poetry virtualenvs.options.no-pip = true
Fixes #1425
1 parent 2073a69 commit 259993b

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## [UNRELEASED]
44

5-
No user facing changes.
5+
- Python automatic dependency installation will no longer fail for projects using Poetry that specify `virtualenvs.options.no-pip = true` in their `poetry.toml`. [#1431](https://github.com/github/codeql-action/pull/1431).
66

77
## 2.1.38 - 12 Jan 2023
88

python-setup/find_site_packages.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Print the path to the site-packages directory for the current Python environment.
3+
"""
4+
5+
try:
6+
import pip
7+
import os
8+
print(os.path.dirname(os.path.dirname(pip.__file__)))
9+
except ImportError:
10+
import sys
11+
print("could not import pip", file=sys.stderr)
12+
# if you use poetry with `virtualenvs.options.no-pip = true` you might end up with a
13+
# virtualenv without pip, so the above trick doesn't actually work. See
14+
# https://python-poetry.org/docs/configuration/#virtualenvsoptionsno-pip
15+
#
16+
# A possible option is to install `pip` into the virtualenv created by poetry
17+
# (`poetry add pip`), but it turns out that doesn't always work :( for the test
18+
# poetry/requests-3, I was not allowed to install pip! So I did not pursue this
19+
# option further.
20+
#
21+
# Instead, local testing shows that first entry of `site.getsitepackages()` has the
22+
# right path, whereas `site.getusersitepackages()` is about the system python (very
23+
# confusing).
24+
#
25+
# We can't use the environment variable POETRY_VIRTUALENVS_OPTIONS_NO_PIP because it
26+
# does not work, see https://github.com/python-poetry/poetry/issues/5906
27+
import site
28+
print(site.getsitepackages()[0])

python-setup/tests/from_python_exe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ def get_details(path_to_python_exe: str) -> Tuple[str, str]:
99
import_path = subprocess.check_output(
1010
[
1111
path_to_python_exe,
12-
"-c",
13-
"import os; import pip; print(os.path.dirname(os.path.dirname(pip.__file__)))",
12+
os.path.join(os.path.dirname(__file__), "find_site_packages.py")
1413
],
1514
stdin=subprocess.DEVNULL,
1615
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[virtualenvs]
22
in-project = true
3+
4+
[virtualenvs.options]
5+
no-pip = true

src/analyze.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ async function setupPythonExtractor(logger: Logger) {
8888
return;
8989
}
9090

91+
const scriptsFolder = path.resolve(__dirname, "../python-setup");
92+
9193
let output = "";
9294
const options = {
9395
listeners: {
@@ -100,8 +102,7 @@ async function setupPythonExtractor(logger: Logger) {
100102
await new toolrunner.ToolRunner(
101103
codeqlPython,
102104
[
103-
"-c",
104-
"import os; import pip; print(os.path.dirname(os.path.dirname(pip.__file__)))",
105+
path.join(scriptsFolder, "find_site_packages.py"),
105106
],
106107
options
107108
).exec();

0 commit comments

Comments
 (0)