We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Support optional 3rd party dependencies in helm
**Is your feature request related to a problem? Please describe.**
I am generating a helm chart that would ordinarily have chart dependencies that are optional based on a condition. The method of defining helm artifacts doesn't seem to allow for this.

**Describe the solution you'd like**
Preferably defining a 3rd party helm artifact would simply allow it to be used manually inside the chart.yaml instead of injecting the chart automatically (or at least have the option) so that you could manually configure the chart yaml as you would like.

**Describe alternatives you've considered**
I think I could make proxy 1st party chart that references the 3rd party chart and then make that optional but that is very awkward.

**Additional context**
Add any other context or screenshots about the feature request here.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/python/pants/backend/helm/dependency_inference/chart.py`
Content:
```
1 # Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2 # Licensed under the Apache License, Version 2.0 (see LICENSE).
3 
4 from __future__ import annotations
5 
6 import logging
7 from dataclasses import dataclass
8 from typing import Iterable
9 
10 from pants.backend.helm.resolve import artifacts
11 from pants.backend.helm.resolve.artifacts import ThirdPartyHelmArtifactMapping
12 from pants.backend.helm.subsystems.helm import HelmSubsystem
13 from pants.backend.helm.target_types import (
14     AllHelmChartTargets,
15     HelmChartDependenciesField,
16     HelmChartMetaSourceField,
17     HelmChartTarget,
18 )
19 from pants.backend.helm.target_types import rules as helm_target_types_rules
20 from pants.backend.helm.util_rules import chart_metadata
21 from pants.backend.helm.util_rules.chart_metadata import HelmChartDependency, HelmChartMetadata
22 from pants.engine.addresses import Address
23 from pants.engine.internals.selectors import Get, MultiGet
24 from pants.engine.rules import collect_rules, rule
25 from pants.engine.target import (
26     DependenciesRequest,
27     ExplicitlyProvidedDependencies,
28     FieldSet,
29     InferDependenciesRequest,
30     InferredDependencies,
31 )
32 from pants.engine.unions import UnionRule
33 from pants.util.frozendict import FrozenDict
34 from pants.util.logging import LogLevel
35 from pants.util.ordered_set import OrderedSet
36 from pants.util.strutil import bullet_list, pluralize
37 
38 logger = logging.getLogger(__name__)
39 
40 
41 class DuplicateHelmChartNamesFound(Exception):
42     def __init__(self, duplicates: Iterable[tuple[str, Address]]) -> None:
43         super().__init__(
44             f"Found more than one `{HelmChartTarget.alias}` target using the same chart name:\n\n"
45             f"{bullet_list([f'{addr} -> {name}' for name, addr in duplicates])}"
46         )
47 
48 
49 class UnknownHelmChartDependency(Exception):
50     def __init__(self, address: Address, dependency: HelmChartDependency) -> None:
51         super().__init__(
52             f"Can not find any declared artifact for dependency '{dependency.name}' "
53             f"declared at `Chart.yaml` in Helm chart at address: {address}"
54         )
55 
56 
57 class FirstPartyHelmChartMapping(FrozenDict[str, Address]):
58     pass
59 
60 
61 @rule
62 async def first_party_helm_chart_mapping(
63     all_helm_chart_tgts: AllHelmChartTargets,
64 ) -> FirstPartyHelmChartMapping:
65     charts_metadata = await MultiGet(
66         Get(HelmChartMetadata, HelmChartMetaSourceField, tgt[HelmChartMetaSourceField])
67         for tgt in all_helm_chart_tgts
68     )
69 
70     name_addr_mapping: dict[str, Address] = {}
71     duplicate_chart_names: OrderedSet[tuple[str, Address]] = OrderedSet()
72 
73     for meta, tgt in zip(charts_metadata, all_helm_chart_tgts):
74         if meta.name in name_addr_mapping:
75             duplicate_chart_names.add((meta.name, name_addr_mapping[meta.name]))
76             duplicate_chart_names.add((meta.name, tgt.address))
77             continue
78 
79         name_addr_mapping[meta.name] = tgt.address
80 
81     if duplicate_chart_names:
82         raise DuplicateHelmChartNamesFound(duplicate_chart_names)
83 
84     return FirstPartyHelmChartMapping(name_addr_mapping)
85 
86 
87 @dataclass(frozen=True)
88 class HelmChartDependenciesInferenceFieldSet(FieldSet):
89     required_fields = (HelmChartMetaSourceField, HelmChartDependenciesField)
90 
91     source: HelmChartMetaSourceField
92     dependencies: HelmChartDependenciesField
93 
94 
95 class InferHelmChartDependenciesRequest(InferDependenciesRequest):
96     infer_from = HelmChartDependenciesInferenceFieldSet
97 
98 
99 @rule(desc="Inferring Helm chart dependencies", level=LogLevel.DEBUG)
100 async def infer_chart_dependencies_via_metadata(
101     request: InferHelmChartDependenciesRequest,
102     first_party_mapping: FirstPartyHelmChartMapping,
103     third_party_mapping: ThirdPartyHelmArtifactMapping,
104     subsystem: HelmSubsystem,
105 ) -> InferredDependencies:
106     address = request.field_set.address
107 
108     # Parse Chart.yaml for explicitly set dependencies.
109     explicitly_provided_deps, metadata = await MultiGet(
110         Get(ExplicitlyProvidedDependencies, DependenciesRequest(request.field_set.dependencies)),
111         Get(HelmChartMetadata, HelmChartMetaSourceField, request.field_set.source),
112     )
113 
114     remotes = subsystem.remotes()
115 
116     def resolve_dependency_url(dependency: HelmChartDependency) -> str | None:
117         if not dependency.repository:
118             registry = remotes.default_registry
119             if registry:
120                 return f"{registry.address}/{dependency.name}"
121             return None
122         else:
123             return f"{dependency.repository}/{dependency.name}"
124 
125     # Associate dependencies in Chart.yaml with addresses.
126     dependencies: OrderedSet[Address] = OrderedSet()
127     for chart_dep in metadata.dependencies:
128         candidate_addrs = []
129 
130         first_party_dep = first_party_mapping.get(chart_dep.name)
131         if first_party_dep:
132             candidate_addrs.append(first_party_dep)
133 
134         dependency_url = resolve_dependency_url(chart_dep)
135         third_party_dep = third_party_mapping.get(dependency_url) if dependency_url else None
136         if third_party_dep:
137             candidate_addrs.append(third_party_dep)
138 
139         if not candidate_addrs:
140             raise UnknownHelmChartDependency(address, chart_dep)
141 
142         matches = frozenset(candidate_addrs).difference(explicitly_provided_deps.includes)
143 
144         explicitly_provided_deps.maybe_warn_of_ambiguous_dependency_inference(
145             matches,
146             address,
147             context=f"The Helm chart {address} declares `{chart_dep.name}` as dependency",
148             import_reference="helm dependency",
149         )
150 
151         maybe_disambiguated = explicitly_provided_deps.disambiguated(matches)
152         if maybe_disambiguated:
153             dependencies.add(maybe_disambiguated)
154 
155     logger.debug(
156         f"Inferred {pluralize(len(dependencies), 'dependency')} for target at address: {address}"
157     )
158     return InferredDependencies(dependencies)
159 
160 
161 def rules():
162     return [
163         *collect_rules(),
164         *artifacts.rules(),
165         *helm_target_types_rules(),
166         *chart_metadata.rules(),
167         UnionRule(InferDependenciesRequest, InferHelmChartDependenciesRequest),
168     ]
169
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/python/pants/backend/helm/dependency_inference/chart.py b/src/python/pants/backend/helm/dependency_inference/chart.py
--- a/src/python/pants/backend/helm/dependency_inference/chart.py
+++ b/src/python/pants/backend/helm/dependency_inference/chart.py
@@ -4,11 +4,13 @@
 from __future__ import annotations
 
 import logging
+import os
 from dataclasses import dataclass
 from typing import Iterable
 
 from pants.backend.helm.resolve import artifacts
 from pants.backend.helm.resolve.artifacts import ThirdPartyHelmArtifactMapping
+from pants.backend.helm.resolve.remotes import HelmRemotes
 from pants.backend.helm.subsystems.helm import HelmSubsystem
 from pants.backend.helm.target_types import (
     AllHelmChartTargets,
@@ -96,6 +98,16 @@
     infer_from = HelmChartDependenciesInferenceFieldSet
 
 
+def resolve_dependency_url(remotes: HelmRemotes, dependency: HelmChartDependency) -> str | None:
+    if not dependency.repository:
+        registry = remotes.default_registry
+        if registry:
+            return os.path.join(registry.address, dependency.name)
+        return None
+    else:
+        return os.path.join(dependency.repository, dependency.name)
+
+
 @rule(desc="Inferring Helm chart dependencies", level=LogLevel.DEBUG)
 async def infer_chart_dependencies_via_metadata(
     request: InferHelmChartDependenciesRequest,
@@ -113,15 +125,6 @@
 
     remotes = subsystem.remotes()
 
-    def resolve_dependency_url(dependency: HelmChartDependency) -> str | None:
-        if not dependency.repository:
-            registry = remotes.default_registry
-            if registry:
-                return f"{registry.address}/{dependency.name}"
-            return None
-        else:
-            return f"{dependency.repository}/{dependency.name}"
-
     # Associate dependencies in Chart.yaml with addresses.
     dependencies: OrderedSet[Address] = OrderedSet()
     for chart_dep in metadata.dependencies:
@@ -131,7 +134,7 @@
         if first_party_dep:
             candidate_addrs.append(first_party_dep)
 
-        dependency_url = resolve_dependency_url(chart_dep)
+        dependency_url = resolve_dependency_url(remotes, chart_dep)
         third_party_dep = third_party_mapping.get(dependency_url) if dependency_url else None
         if third_party_dep:
             candidate_addrs.append(third_party_dep)

{"golden_diff": "diff --git a/src/python/pants/backend/helm/dependency_inference/chart.py b/src/python/pants/backend/helm/dependency_inference/chart.py\n--- a/src/python/pants/backend/helm/dependency_inference/chart.py\n+++ b/src/python/pants/backend/helm/dependency_inference/chart.py\n@@ -4,11 +4,13 @@\n from __future__ import annotations\n \n import logging\n+import os\n from dataclasses import dataclass\n from typing import Iterable\n \n from pants.backend.helm.resolve import artifacts\n from pants.backend.helm.resolve.artifacts import ThirdPartyHelmArtifactMapping\n+from pants.backend.helm.resolve.remotes import HelmRemotes\n from pants.backend.helm.subsystems.helm import HelmSubsystem\n from pants.backend.helm.target_types import (\n     AllHelmChartTargets,\n@@ -96,6 +98,16 @@\n     infer_from = HelmChartDependenciesInferenceFieldSet\n \n \n+def resolve_dependency_url(remotes: HelmRemotes, dependency: HelmChartDependency) -> str | None:\n+    if not dependency.repository:\n+        registry = remotes.default_registry\n+        if registry:\n+            return os.path.join(registry.address, dependency.name)\n+        return None\n+    else:\n+        return os.path.join(dependency.repository, dependency.name)\n+\n+\n @rule(desc=\"Inferring Helm chart dependencies\", level=LogLevel.DEBUG)\n async def infer_chart_dependencies_via_metadata(\n     request: InferHelmChartDependenciesRequest,\n@@ -113,15 +125,6 @@\n \n     remotes = subsystem.remotes()\n \n-    def resolve_dependency_url(dependency: HelmChartDependency) -> str | None:\n-        if not dependency.repository:\n-            registry = remotes.default_registry\n-            if registry:\n-                return f\"{registry.address}/{dependency.name}\"\n-            return None\n-        else:\n-            return f\"{dependency.repository}/{dependency.name}\"\n-\n     # Associate dependencies in Chart.yaml with addresses.\n     dependencies: OrderedSet[Address] = OrderedSet()\n     for chart_dep in metadata.dependencies:\n@@ -131,7 +134,7 @@\n         if first_party_dep:\n             candidate_addrs.append(first_party_dep)\n \n-        dependency_url = resolve_dependency_url(chart_dep)\n+        dependency_url = resolve_dependency_url(remotes, chart_dep)\n         third_party_dep = third_party_mapping.get(dependency_url) if dependency_url else None\n         if third_party_dep:\n             candidate_addrs.append(third_party_dep)\n", "issue": "Support optional 3rd party dependencies in helm\n**Is your feature request related to a problem? Please describe.**\r\nI am generating a helm chart that would ordinarily have chart dependencies that are optional based on a condition. The method of defining helm artifacts doesn't seem to allow for this.\r\n\r\n**Describe the solution you'd like**\r\nPreferably defining a 3rd party helm artifact would simply allow it to be used manually inside the chart.yaml instead of injecting the chart automatically (or at least have the option) so that you could manually configure the chart yaml as you would like.\r\n\r\n**Describe alternatives you've considered**\r\nI think I could make proxy 1st party chart that references the 3rd party chart and then make that optional but that is very awkward.\r\n\r\n**Additional context**\r\nAdd any other context or screenshots about the feature request here.\r\n\n", "before_files": [{"content": "# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\n\nfrom __future__ import annotations\n\nimport logging\nfrom dataclasses import dataclass\nfrom typing import Iterable\n\nfrom pants.backend.helm.resolve import artifacts\nfrom pants.backend.helm.resolve.artifacts import ThirdPartyHelmArtifactMapping\nfrom pants.backend.helm.subsystems.helm import HelmSubsystem\nfrom pants.backend.helm.target_types import (\n    AllHelmChartTargets,\n    HelmChartDependenciesField,\n    HelmChartMetaSourceField,\n    HelmChartTarget,\n)\nfrom pants.backend.helm.target_types import rules as helm_target_types_rules\nfrom pants.backend.helm.util_rules import chart_metadata\nfrom pants.backend.helm.util_rules.chart_metadata import HelmChartDependency, HelmChartMetadata\nfrom pants.engine.addresses import Address\nfrom pants.engine.internals.selectors import Get, MultiGet\nfrom pants.engine.rules import collect_rules, rule\nfrom pants.engine.target import (\n    DependenciesRequest,\n    ExplicitlyProvidedDependencies,\n    FieldSet,\n    InferDependenciesRequest,\n    InferredDependencies,\n)\nfrom pants.engine.unions import UnionRule\nfrom pants.util.frozendict import FrozenDict\nfrom pants.util.logging import LogLevel\nfrom pants.util.ordered_set import OrderedSet\nfrom pants.util.strutil import bullet_list, pluralize\n\nlogger = logging.getLogger(__name__)\n\n\nclass DuplicateHelmChartNamesFound(Exception):\n    def __init__(self, duplicates: Iterable[tuple[str, Address]]) -> None:\n        super().__init__(\n            f\"Found more than one `{HelmChartTarget.alias}` target using the same chart name:\\n\\n\"\n            f\"{bullet_list([f'{addr} -> {name}' for name, addr in duplicates])}\"\n        )\n\n\nclass UnknownHelmChartDependency(Exception):\n    def __init__(self, address: Address, dependency: HelmChartDependency) -> None:\n        super().__init__(\n            f\"Can not find any declared artifact for dependency '{dependency.name}' \"\n            f\"declared at `Chart.yaml` in Helm chart at address: {address}\"\n        )\n\n\nclass FirstPartyHelmChartMapping(FrozenDict[str, Address]):\n    pass\n\n\n@rule\nasync def first_party_helm_chart_mapping(\n    all_helm_chart_tgts: AllHelmChartTargets,\n) -> FirstPartyHelmChartMapping:\n    charts_metadata = await MultiGet(\n        Get(HelmChartMetadata, HelmChartMetaSourceField, tgt[HelmChartMetaSourceField])\n        for tgt in all_helm_chart_tgts\n    )\n\n    name_addr_mapping: dict[str, Address] = {}\n    duplicate_chart_names: OrderedSet[tuple[str, Address]] = OrderedSet()\n\n    for meta, tgt in zip(charts_metadata, all_helm_chart_tgts):\n        if meta.name in name_addr_mapping:\n            duplicate_chart_names.add((meta.name, name_addr_mapping[meta.name]))\n            duplicate_chart_names.add((meta.name, tgt.address))\n            continue\n\n        name_addr_mapping[meta.name] = tgt.address\n\n    if duplicate_chart_names:\n        raise DuplicateHelmChartNamesFound(duplicate_chart_names)\n\n    return FirstPartyHelmChartMapping(name_addr_mapping)\n\n\n@dataclass(frozen=True)\nclass HelmChartDependenciesInferenceFieldSet(FieldSet):\n    required_fields = (HelmChartMetaSourceField, HelmChartDependenciesField)\n\n    source: HelmChartMetaSourceField\n    dependencies: HelmChartDependenciesField\n\n\nclass InferHelmChartDependenciesRequest(InferDependenciesRequest):\n    infer_from = HelmChartDependenciesInferenceFieldSet\n\n\n@rule(desc=\"Inferring Helm chart dependencies\", level=LogLevel.DEBUG)\nasync def infer_chart_dependencies_via_metadata(\n    request: InferHelmChartDependenciesRequest,\n    first_party_mapping: FirstPartyHelmChartMapping,\n    third_party_mapping: ThirdPartyHelmArtifactMapping,\n    subsystem: HelmSubsystem,\n) -> InferredDependencies:\n    address = request.field_set.address\n\n    # Parse Chart.yaml for explicitly set dependencies.\n    explicitly_provided_deps, metadata = await MultiGet(\n        Get(ExplicitlyProvidedDependencies, DependenciesRequest(request.field_set.dependencies)),\n        Get(HelmChartMetadata, HelmChartMetaSourceField, request.field_set.source),\n    )\n\n    remotes = subsystem.remotes()\n\n    def resolve_dependency_url(dependency: HelmChartDependency) -> str | None:\n        if not dependency.repository:\n            registry = remotes.default_registry\n            if registry:\n                return f\"{registry.address}/{dependency.name}\"\n            return None\n        else:\n            return f\"{dependency.repository}/{dependency.name}\"\n\n    # Associate dependencies in Chart.yaml with addresses.\n    dependencies: OrderedSet[Address] = OrderedSet()\n    for chart_dep in metadata.dependencies:\n        candidate_addrs = []\n\n        first_party_dep = first_party_mapping.get(chart_dep.name)\n        if first_party_dep:\n            candidate_addrs.append(first_party_dep)\n\n        dependency_url = resolve_dependency_url(chart_dep)\n        third_party_dep = third_party_mapping.get(dependency_url) if dependency_url else None\n        if third_party_dep:\n            candidate_addrs.append(third_party_dep)\n\n        if not candidate_addrs:\n            raise UnknownHelmChartDependency(address, chart_dep)\n\n        matches = frozenset(candidate_addrs).difference(explicitly_provided_deps.includes)\n\n        explicitly_provided_deps.maybe_warn_of_ambiguous_dependency_inference(\n            matches,\n            address,\n            context=f\"The Helm chart {address} declares `{chart_dep.name}` as dependency\",\n            import_reference=\"helm dependency\",\n        )\n\n        maybe_disambiguated = explicitly_provided_deps.disambiguated(matches)\n        if maybe_disambiguated:\n            dependencies.add(maybe_disambiguated)\n\n    logger.debug(\n        f\"Inferred {pluralize(len(dependencies), 'dependency')} for target at address: {address}\"\n    )\n    return InferredDependencies(dependencies)\n\n\ndef rules():\n    return [\n        *collect_rules(),\n        *artifacts.rules(),\n        *helm_target_types_rules(),\n        *chart_metadata.rules(),\n        UnionRule(InferDependenciesRequest, InferHelmChartDependenciesRequest),\n    ]\n", "path": "src/python/pants/backend/helm/dependency_inference/chart.py"}], "after_files": [{"content": "# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).\n# Licensed under the Apache License, Version 2.0 (see LICENSE).\n\nfrom __future__ import annotations\n\nimport logging\nimport os\nfrom dataclasses import dataclass\nfrom typing import Iterable\n\nfrom pants.backend.helm.resolve import artifacts\nfrom pants.backend.helm.resolve.artifacts import ThirdPartyHelmArtifactMapping\nfrom pants.backend.helm.resolve.remotes import HelmRemotes\nfrom pants.backend.helm.subsystems.helm import HelmSubsystem\nfrom pants.backend.helm.target_types import (\n    AllHelmChartTargets,\n    HelmChartDependenciesField,\n    HelmChartMetaSourceField,\n    HelmChartTarget,\n)\nfrom pants.backend.helm.target_types import rules as helm_target_types_rules\nfrom pants.backend.helm.util_rules import chart_metadata\nfrom pants.backend.helm.util_rules.chart_metadata import HelmChartDependency, HelmChartMetadata\nfrom pants.engine.addresses import Address\nfrom pants.engine.internals.selectors import Get, MultiGet\nfrom pants.engine.rules import collect_rules, rule\nfrom pants.engine.target import (\n    DependenciesRequest,\n    ExplicitlyProvidedDependencies,\n    FieldSet,\n    InferDependenciesRequest,\n    InferredDependencies,\n)\nfrom pants.engine.unions import UnionRule\nfrom pants.util.frozendict import FrozenDict\nfrom pants.util.logging import LogLevel\nfrom pants.util.ordered_set import OrderedSet\nfrom pants.util.strutil import bullet_list, pluralize\n\nlogger = logging.getLogger(__name__)\n\n\nclass DuplicateHelmChartNamesFound(Exception):\n    def __init__(self, duplicates: Iterable[tuple[str, Address]]) -> None:\n        super().__init__(\n            f\"Found more than one `{HelmChartTarget.alias}` target using the same chart name:\\n\\n\"\n            f\"{bullet_list([f'{addr} -> {name}' for name, addr in duplicates])}\"\n        )\n\n\nclass UnknownHelmChartDependency(Exception):\n    def __init__(self, address: Address, dependency: HelmChartDependency) -> None:\n        super().__init__(\n            f\"Can not find any declared artifact for dependency '{dependency.name}' \"\n            f\"declared at `Chart.yaml` in Helm chart at address: {address}\"\n        )\n\n\nclass FirstPartyHelmChartMapping(FrozenDict[str, Address]):\n    pass\n\n\n@rule\nasync def first_party_helm_chart_mapping(\n    all_helm_chart_tgts: AllHelmChartTargets,\n) -> FirstPartyHelmChartMapping:\n    charts_metadata = await MultiGet(\n        Get(HelmChartMetadata, HelmChartMetaSourceField, tgt[HelmChartMetaSourceField])\n        for tgt in all_helm_chart_tgts\n    )\n\n    name_addr_mapping: dict[str, Address] = {}\n    duplicate_chart_names: OrderedSet[tuple[str, Address]] = OrderedSet()\n\n    for meta, tgt in zip(charts_metadata, all_helm_chart_tgts):\n        if meta.name in name_addr_mapping:\n            duplicate_chart_names.add((meta.name, name_addr_mapping[meta.name]))\n            duplicate_chart_names.add((meta.name, tgt.address))\n            continue\n\n        name_addr_mapping[meta.name] = tgt.address\n\n    if duplicate_chart_names:\n        raise DuplicateHelmChartNamesFound(duplicate_chart_names)\n\n    return FirstPartyHelmChartMapping(name_addr_mapping)\n\n\n@dataclass(frozen=True)\nclass HelmChartDependenciesInferenceFieldSet(FieldSet):\n    required_fields = (HelmChartMetaSourceField, HelmChartDependenciesField)\n\n    source: HelmChartMetaSourceField\n    dependencies: HelmChartDependenciesField\n\n\nclass InferHelmChartDependenciesRequest(InferDependenciesRequest):\n    infer_from = HelmChartDependenciesInferenceFieldSet\n\n\ndef resolve_dependency_url(remotes: HelmRemotes, dependency: HelmChartDependency) -> str | None:\n    if not dependency.repository:\n        registry = remotes.default_registry\n        if registry:\n            return os.path.join(registry.address, dependency.name)\n        return None\n    else:\n        return os.path.join(dependency.repository, dependency.name)\n\n\n@rule(desc=\"Inferring Helm chart dependencies\", level=LogLevel.DEBUG)\nasync def infer_chart_dependencies_via_metadata(\n    request: InferHelmChartDependenciesRequest,\n    first_party_mapping: FirstPartyHelmChartMapping,\n    third_party_mapping: ThirdPartyHelmArtifactMapping,\n    subsystem: HelmSubsystem,\n) -> InferredDependencies:\n    address = request.field_set.address\n\n    # Parse Chart.yaml for explicitly set dependencies.\n    explicitly_provided_deps, metadata = await MultiGet(\n        Get(ExplicitlyProvidedDependencies, DependenciesRequest(request.field_set.dependencies)),\n        Get(HelmChartMetadata, HelmChartMetaSourceField, request.field_set.source),\n    )\n\n    remotes = subsystem.remotes()\n\n    # Associate dependencies in Chart.yaml with addresses.\n    dependencies: OrderedSet[Address] = OrderedSet()\n    for chart_dep in metadata.dependencies:\n        candidate_addrs = []\n\n        first_party_dep = first_party_mapping.get(chart_dep.name)\n        if first_party_dep:\n            candidate_addrs.append(first_party_dep)\n\n        dependency_url = resolve_dependency_url(remotes, chart_dep)\n        third_party_dep = third_party_mapping.get(dependency_url) if dependency_url else None\n        if third_party_dep:\n            candidate_addrs.append(third_party_dep)\n\n        if not candidate_addrs:\n            raise UnknownHelmChartDependency(address, chart_dep)\n\n        matches = frozenset(candidate_addrs).difference(explicitly_provided_deps.includes)\n\n        explicitly_provided_deps.maybe_warn_of_ambiguous_dependency_inference(\n            matches,\n            address,\n            context=f\"The Helm chart {address} declares `{chart_dep.name}` as dependency\",\n            import_reference=\"helm dependency\",\n        )\n\n        maybe_disambiguated = explicitly_provided_deps.disambiguated(matches)\n        if maybe_disambiguated:\n            dependencies.add(maybe_disambiguated)\n\n    logger.debug(\n        f\"Inferred {pluralize(len(dependencies), 'dependency')} for target at address: {address}\"\n    )\n    return InferredDependencies(dependencies)\n\n\ndef rules():\n    return [\n        *collect_rules(),\n        *artifacts.rules(),\n        *helm_target_types_rules(),\n        *chart_metadata.rules(),\n        UnionRule(InferDependenciesRequest, InferHelmChartDependenciesRequest),\n    ]\n", "path": "src/python/pants/backend/helm/dependency_inference/chart.py"}]}