Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on misplaced requirement function calls #15888

Merged
merged 3 commits into from
Mar 18, 2024
Merged
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
5 changes: 5 additions & 0 deletions conans/client/conanfile/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
def run_configure_method(conanfile, down_options, profile_options, ref):
""" Run all the config-related functions for the given conanfile object """

initial_requires_count = len(conanfile.requires)

if hasattr(conanfile, "config_options"):
with conanfile_exception_formatter(conanfile, "config_options"):
conanfile.config_options()
Expand All @@ -23,6 +25,9 @@ def run_configure_method(conanfile, down_options, profile_options, ref):
elif "auto_shared_fpic" in conanfile.implements:
auto_shared_fpic_configure(conanfile)

if initial_requires_count != len(conanfile.requires):
conanfile.output.warning("Requirements should only be added in the requirements()/build_requirements() methods, not configure()/config_options(), which might raise errors in the future.", warn_tag="deprecated")

result = conanfile.options.get_upstream_options(down_options, ref, is_consumer)
self_options, up_options, private_up_options = result
# self_options are the minimum to reproduce state, as defined from downstream (not profile)
Expand Down
3 changes: 3 additions & 0 deletions conans/model/requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,6 @@ def __repr__(self):

def serialize(self):
return [v.serialize() for v in self._requires.values()]

def __len__(self):
return len(self._requires)
14 changes: 14 additions & 0 deletions conans/test/integration/build_requires/build_requires_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,17 @@ def test_build_missing_build_requires():
c.run("install app --build=missing")
assert "- Build" not in c.out
assert re.search(r"Skipped binaries(\s*)tool/0.1, tooldep/0.1", c.out)


def test_requirement_in_wrong_method():
tc = TestClient(light=True)
tc.save({"conanfile.py": textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
name = "pkg"
version = "0.1"
def configure(self):
self.requires("foo/1.0")
""")})
tc.run('create . -cc="core:warnings_as_errors=[\'*\']"', assert_error=True)
assert "ERROR: deprecated: Requirements should only be added in the requirements()/build_requirements() methods, not configure()/config_options(), which might raise errors in the future." in tc.out