question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] Define aliases that depends on settings and options

See original GitHub issue

It possible to define alias packages that use the settings and options to define the target packages?

My first approach, would be, for being able to get access to the settings and options when Conan is loading the recipe, to re-define the attribute alias as a property using python decorator @property. By doing this, the self context is accessible. Here the recipe for the alias.

from conans import ConanFile
from conans.errors import ConanInvalidConfiguration

class BoostAliasConan(ConanFile):
    name = 'boost'
    version = 'latest'   
    revision_mode = 'hash'

    settings = 'os', 'compiler'

    options = {
        'shared': [True, False],
    }
    default_options = {
        'shared': True,
    }

    @property
    def alias(self):
        if self.settings.os == 'Windows':
            if self.options.shared:
                boost_version = '1.57.0'
            else:
                boost_version = '1.63.0'
        elif self.settings.os == 'Linux':
            boost_version = '1.72.0'
        else:
            raise ConanInvalidConfiguration('This configuration is not supported')

        return f'boost/{boost_version}@user/stable'

When using the alias package boost/latest@user/stable without specifying any option, the alias resolution is fine. But specifying option shared=False on install, then the alias recipe still will have the default value set. this results in getting boost version 1.57.0 instead of 1.63.0.

What would be the right way to define such aliases?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
memshardedcommented, Sep 26, 2021

To follow up: it is not possible to use settings and options in alias to decide dynamically, and it would be quite complicated to implement, adding too much extra complexity when we are trying to simplify the alias logic.

In any case, it is possible to define a “proxy” recipe that implements the logic, (and can remove the variance of packages removing settings and options in package_id().) That would be a regular recipe, not an alias, and will be represented in the dependency graph, but I don’t see a reason this won’t work.

I’d say the question is responded and can be closed, if that approach doesn’t work, we could discuss why and what are the alternatives, but at the moment I am closing this because because it doesn’t seem something possible to implement reasonably.

1reaction
memshardedcommented, Sep 13, 2021

I am afraid that such is some bad behavior, possibly because some internal caching in the legacy requires = "foo/latest" syntax. The new explicit alias syntax approved by the Tribe for 2.0 (and already available at least since 1.39), will fail exactly in this step, because the settings and options are not initialized, this test proves it:

def test_alias_settings():
    c = TestClient()
    c.save({"conanfile.py": GenConanfile()})
    c.run("create . foo/1.0@")
    c.save({"conanfile.py": textwrap.dedent("""
        from conans import ConanFile

        class FooConan(ConanFile):
            settings = "os"
            options = { "shared": [True, False] }
            default_options = { "shared": False }

            @property
            def alias(self):
                self.output.info("os: {}".format(self.settings.os))
                print("shared: {}".format(self.options.shared))
                return 'foo/1.0@'
        """)})
    c.run("export . foo/latest@")

    # Legacy syntax, might load settings/options, incorrectly
    c.save({"conanfile.py": GenConanfile().with_requires("foo/latest")})
    c.run("install .")
    # New syntax will raise
    c.save({"conanfile.py": GenConanfile().with_requires("foo/(latest)")})
    c.run("install .", assert_error=True)
    assert "Alias definition error in foo/latest" in c.out
Read more comments on GitHub >

github_iconTop Results From Across the Web

An Introduction to Useful Bash Aliases and Functions
Let's create a common bash alias now. One idiomatic command phrase that many people use frequently is ls -lha or ls -lhA (the...
Read more >
Is it a good practice to use aliases? [closed]
First, you need to set the same alias on each computer. They behave differently on different computers, which can confuse others. Unless you ......
Read more >
Aliases in Windows command prompt - Stack Overflow
To define a console alias, use Doskey.exe to create a macro, or use the AddConsoleAlias function. doskey. doskey test=cd \a_very_long_path\test. To also pass ......
Read more >
Bash aliases you can't live without - Opensource.com
A Bash alias is a method of supplementing or overriding Bash commands with new ones. Bash aliases make it easy for users to...
Read more >
Linux alias Command: How to Use It With Examples
In this tutorial, learn how to create, view, and remove temporary and permanent command shortcuts in Linux using the alias command.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found