conan-io / docs

conan.io reStructuredText documentation
http://docs.conan.io
MIT License
104 stars 356 forks source link

Update docs: Referencing user/channel in a recipe throws error #1546

Open radonish opened 4 years ago

radonish commented 4 years ago

Looking at the example here where the requirements() method checks if the user and channel are set: https://docs.conan.io/en/latest/reference/conanfile/attributes.html#user-channel

        if self.user and self.channel:
            # If the recipe is using them, I want to consume my fork.
            self.requires("Say/0.1@%s/%s" % (self.user, self.channel))

For the scenario where the user/channel are not provided in the conan create command I get: conans.errors.ConanException: user not defined, but self.user is used in conanfile

Is the documentation dated or is the behavior python version-related perhaps?

Python 3.7.1 Conan 1.21.1

Thanks

memsharded commented 4 years ago

Hi @radonish

yes, that is true. The self.user and self.channel fields cannot be used in that way if there is no user and channel defined. It is likely that such exception will be removed in 2.0.

In the meantime, you can capture the exception:

def requirements(self):
    try:
          user = self.user
          channel = self.channel
    except ConanException:
          self.requires("Say/0.1")
    else:
          self.requires("Say/0.1@%s/%s" % (user, channel))
radonish commented 4 years ago

@memsharded, that's what I had been doing in the past but came across the documentation example and wondered if I was missing something silly.

Thanks!

memsharded commented 4 years ago

Not really, those docs were before the possibility of removing the user/channel. So not a bug, but I think it deserves being updated in the docs. Lets move it there.