Skip to content

RuntimeError thrown when executing poetry publish -r <name> with environment variable POETRY_REPOSITORIES_<NAME> set #2204

@esciara

Description

@esciara

Issue

According to the doc on using environment variables for configuration, I should be able to declare an alternative package repository to publish to by setting an environment variable of format POETRY_REPOSITORIES_<NAME> (like POETRY_REPOSITORIES_MYREPO).

However, when running the following commands:

$ export POETRY_REPOSITORIES_MYREPO=http://myrepo.url
$ poetry publish -r myrepo

The following error is thrown:

[RuntimeError]
Repository myrepo is not defined

This is due to the fact that the following code for storing the config

config.config_source.add_property(
"repositories.{}.url".format(m.group(1)), url
)

transforms "repositories.<name>" into "repositories.<name>.url", and the following code for publishing
# Retrieving config information
url = self._poetry.config.get("repositories.{}.url".format(repository_name))
if url is None:
raise RuntimeError(
"Repository {} is not defined".format(repository_name)
)

looks for "repositories.<name>.url".

However, the code handling environment variables (in the poetry.config.config.Config.get method) does not treat the POETRY_REPOSITORIES_<NAME> environment variables in any particular way, and hence presents with an equivalent to "repositories.<name>", not "repositories.<name>.url":

if self._use_environment:
env = "POETRY_{}".format(
"_".join(k.upper().replace("-", "_") for k in keys)
)
value = os.getenv(env, _NOT_SET)
if value is not _NOT_SET:
return self.process(self._get_normalizer(setting_name)(value))

Workaround

A workaround to this is to add _URL as a suffix to the variables (here POETRY_REPOSITORIES_MYREPO_URL), but this does not follow the simple and elegant rule for setting poetry though environment variables as defined in the doc.

Corrective options

There are several way this could be corrected:

Option 1: change the config setting repositories.<name> to repositories.<name>.url

  • Pros:
    • does not change current way the settings are stored
    • backwards compatibility for existing scripts (ci/cd and other automation) could be kept by allowing the usage of repositories.<name>
  • Cons:
    • the addition of .url does not seem to make much sens - appart perhaps for config file readability, which IMHO is questionable - since there is no other setting required to repositories than their url
    • backward compatibility fix feels a bit like a dirty hack

Option 2: change the config setting storage from repositories.<name>.url to repositories.<name>

  • Pros:
    • removes the use of .url
    • backwards compatibility for existing configuration files could be kept by looking also for repositories.<name>.url when searching for repositories.<name>
  • Cons:
    • backward compatibility fix feels a bit like a dirty hack, but is probably necessary to not break existing systems (unless an upgrade process is created)

Option 3: apply to the environment variables the same transformation that that to the comfig setting (POETRY_REPOSITORIES_<NAME> transformed on the fly to the equivalent of POETRY_REPOSITORIES_<NAME>_URL)

  • Pros:
    • keeps much of the code the same
    • no need for backward compatibility
  • Cons:
    • does not remove the use of .url

Any preferred option? My personal vote goes as first best for option 2 with an upgrade process and as second best to option 3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/publishingRelated to PyPI/PEP 503 publishingkind/bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions