Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions conan/internal/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from conan.tools.cmake import cmake_layout
from conan.tools.google import bazel_layout
from conan.tools.microsoft import vs_layout
from conan.tools.scm import Version
from conan.internal.errors import conanfile_exception_formatter, NotFoundException
from conan.errors import ConanException
from conan.internal.model.conan_file import ConanFile
Expand Down Expand Up @@ -143,6 +144,16 @@ def load_named(self, conanfile_path, name, version, user, channel, graph_lock=No
with conanfile_exception_formatter("conanfile.py", "set_version"):
conanfile.set_version()

if not conanfile.version:
# Last chance, try to get version from conan_data.
# Only works if only one version is present
conan_data = getattr(conanfile, "conan_data", {})
sources = conan_data.get("sources", {}) if conan_data else {}
sources = sources or {}
sorted_versions = sorted(sources.keys(), key=lambda x: Version(x))
if sorted_versions:
conanfile.version = sorted_versions.pop()

return conanfile

def load_export(self, conanfile_path, name, version, user, channel, graph_lock=None,
Expand Down
59 changes: 59 additions & 0 deletions test/integration/conanfile/conan_data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,62 @@ def generate(self):
'2.0':
x: foo
""")


@pytest.mark.parametrize("command", [
"source",
"graph info",
"create",
"export"
])
def test_commands_auto_pick_version(command):
c = TestClient(light=True)
conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
name = "pkg"

def source(self):
self.output.info("ok")
""")
conandata = textwrap.dedent("""
sources:
1.0:
url: "url1"
2.0:
url: "url2"
""")

c.save({"conanfile.py": conanfile,
"conandata.yml": conandata})
c.run(command)
assert "pkg/2.0" in c.out


@pytest.mark.parametrize("conandata", [
pytest.param(
textwrap.dedent("""
sources:
# Empty, no content
"""), id="empty sources"),
pytest.param(
textwrap.dedent("""
# No sources key
foo: 1
"""), id="no sources"),
])
def test_commands_auto_pick_version_no_version_conandata(conandata):
c = TestClient(light=True)
conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
name = "pkg"

def source(self):
self.output.info("ok")
""")

c.save({"conanfile.py": conanfile,
"conandata.yml": conandata})
c.run("create", assert_error=True)
assert "conanfile didn't specify version" in c.out
Loading