• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 22740642519

05 Mar 2026 11:00PM UTC coverage: 52.677% (-40.3%) from 92.931%
22740642519

Pull #23157

github

web-flow
Merge 2aa18e6d4 into f0030f5e7
Pull Request #23157: [pants ng] Partition source files by config.

31678 of 60136 relevant lines covered (52.68%)

0.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.19
/src/python/pants/backend/docker/util_rules/dockerfile.py
1
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
1✔
5

6
import os
1✔
7

8
from pants.backend.docker.target_types import DockerImageInstructionsField, DockerImageSourceField
1✔
9
from pants.engine.fs import CreateDigest, FileContent
1✔
10
from pants.engine.intrinsics import digest_to_snapshot
1✔
11
from pants.engine.rules import collect_rules, implicitly, rule
1✔
12
from pants.engine.target import GeneratedSources, GenerateSourcesRequest, InvalidFieldException
1✔
13
from pants.engine.unions import UnionRule
1✔
14

15

16
class GenerateDockerfileRequest(GenerateSourcesRequest):
1✔
17
    # This will always run codegen when hydrating `docker_image`s, performing source validations but
18
    # does not generate anything if there are no `instructions` defined on the target.
19
    input = DockerImageSourceField
1✔
20
    output = DockerImageSourceField
1✔
21

22

23
@rule
1✔
24
async def hydrate_dockerfile(request: GenerateDockerfileRequest) -> GeneratedSources:
1✔
25
    target = request.protocol_target
1✔
26
    address = target.address
1✔
27
    instructions = target[DockerImageInstructionsField].value
1✔
28

29
    if instructions and request.protocol_sources.files:
1✔
30
        raise InvalidFieldException(
×
31
            f"The `{target.alias}` {address} provides both a Dockerfile with the `source` field, "
32
            "and Dockerfile contents with the `instructions` field, which is not supported.\n\n"
33
            "To fix, please either set `source=None` or `instructions=None`."
34
        )
35

36
    if not (instructions or request.protocol_sources.files):
1✔
37
        raise InvalidFieldException(
×
38
            f"The `{target.alias}` {address} does not specify any Dockerfile.\n\n"
39
            "Provide either the filename to a Dockerfile in your workspace as the `source` field "
40
            "value, or the Dockerfile content to the `instructions` field."
41
        )
42

43
    def dockerfile_path():
1✔
44
        name_parts = ["Dockerfile", address.target_name, address.generated_name]
×
45
        return os.path.join(address.spec_path, ".".join(filter(bool, name_parts)))
×
46

47
    output = (
1✔
48
        await digest_to_snapshot(
49
            **implicitly(
50
                CreateDigest(
51
                    (FileContent(dockerfile_path(), "\n".join([*instructions, ""]).encode()),)
52
                ),
53
            )
54
        )
55
        if instructions
56
        else request.protocol_sources
57
    )
58
    return GeneratedSources(output)
1✔
59

60

61
def rules():
1✔
62
    return (
1✔
63
        *collect_rules(),
64
        UnionRule(GenerateSourcesRequest, GenerateDockerfileRequest),
65
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc