Skip to content

Commit 0379832

Browse files
committed
Improve pip config set error when PIP_CONFIG_FILE is non-regular
1 parent 439c091 commit 0379832

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/pip/_internal/configuration.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,18 @@ def _get_parser_to_modify(self) -> tuple[str, RawConfigParser]:
378378
assert self.load_only
379379
parsers = self._parsers[self.load_only]
380380
if not parsers:
381-
# This should not happen if everything works correctly.
381+
# When PIP_CONFIG_FILE points to a non-regular file (e.g. /dev/null),
382+
# we can't load/modify it as an INI file. Surface a clear error instead
383+
# of an internal one.
384+
env_config_file = os.environ.get("PIP_CONFIG_FILE")
385+
if env_config_file and not os.path.isfile(env_config_file):
386+
raise ConfigurationError(
387+
"Cannot modify pip configuration: PIP_CONFIG_FILE points to a "
388+
f"non-regular file: {env_config_file}"
389+
)
390+
382391
raise ConfigurationError(
383-
"Fatal Internal error [id=2]. Please report as a bug."
392+
"Cannot modify pip configuration: no configuration file is available for the selected scope."
384393
)
385394

386395
# Use the highest priority parser.

tests/functional/test_configuration.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,24 @@ def test_config_separated(
216216
result.stdout,
217217
)
218218

219+
220+
def test_config_set_with_pip_config_file_devnull_shows_human_error(
221+
self, script: PipTestEnvironment
222+
) -> None:
223+
script.environ["PIP_CONFIG_FILE"] = "/dev/null"
224+
225+
result = script.pip(
226+
"config",
227+
"set",
228+
"global.index-url",
229+
"https://example.com/",
230+
expect_error=True,
231+
)
232+
assert "Fatal Internal error [id=2]" not in result.stderr
233+
assert "PIP_CONFIG_FILE" in result.stderr
234+
assert "non-regular file" in result.stderr
235+
236+
219237
@pytest.mark.network
220238
def test_editable_mode_default_config(
221239
self, script: PipTestEnvironment, data: TestData

0 commit comments

Comments
 (0)