I am still having trouble with relocated file targ...
# plugins
a
I am still having trouble with relocated file targets - specifically getting a snapshot with the moved files: I would expect this to work (I have removed all my plugin code from this function so it's just running the one
Get
and then exiting)
Copy code
@rule(level=LogLevel.DEBUG)
async def package_into_image(
    field_set: DockerPackageFieldSet,
    union_membership: UnionMembership,
) -> BuiltPackage:
    target_name = field_set.address.target_name
    transitive_targets = await Get(
        TransitiveTargets, TransitiveTargetsRequest([field_set.address])
    )
    for t in transitive_targets.dependencies:
        if type(t) is RelocatedFiles:
            s = await Get(
                SourceFiles,
                SourceFilesRequest,
                SourceFilesRequest(
                    sources_fields=[t.get(Sources)],
                    for_sources_types=[RelocatedFilesSources],
                )
            )
            <http://logger.info|logger.info>('relocated files: %s', s.snapshot.files)
    return BuiltPackage(
        digest=s.snapshot.digest,
        artifacts=([BuiltPackageArtifact(f, ()) for f in s.snapshot.files]),
    )
has output:
Copy code
[nate@ragin-cajun pants-docker]$ ./pants package test_docker:dockerized_flask_app
12:04:18.11 [INFO] relocated files: ()
f
so the rule in pants looks like this:
Copy code
class RelocateFilesViaCodegenRequest(GenerateSourcesRequest):
    input = RelocatedFilesSources
    output = FilesSources


@rule(desc="Relocating loose files for `relocated_files` targets", level=LogLevel.DEBUG)
async def relocate_files(request: RelocateFilesViaCodegenRequest) -> GeneratedSources:
which suggests the codegen rules are involved (even if this is not “traditional” codegen)
which means you need to set
enable_codegen=True
on your
SourceFilesRequest
👍 1
also, instead of
type(t) is RelocatedFiles
, I believe the preferred solution is to check the target for specific fields. So `t.has_field(RelocatedFilesSources)`is probably preferred.
(assuming that the check is even necessary given
for_sources_types=[RelocatedFilesSources],
👍 1
a
hi @fast-nail-55400 thank you! So adding the
enable_codegen=True
did not change the output (and the
is
check is just there to illustrate what i'm trying to do, i'm not using it in my actual plugin)
here is what the BUILD file looks like in case that is helpful:
Copy code
files(name="files",
      sources=["static/style.css"])

relocated_files(name="relocated",
                files_targets=[":files"],
                src="static/",
                dest="style/")

python_library(
    name="test_docker",
    dependencies=[":files",
                  ":resources",
                  ":relocated",]
)

# this doesn't matter I think none of this is being used before the function exits, we just look up the relocated files target and attempt to get the new sources
docker(
        name="dockerized_flask_app",
        image_setup_commands = ["apt-get update && apt-get upgrade --yes",
                                "apt-get -y install gcc libpq-dev"],
        base_image="python:3.8.8-slim-buster",
        tags=["version1", "version2"],
        #command=["/.virtual_env/bin/python",  "-m", "gunicorn", "<http://test_docker.app:app|test_docker.app:app>"],
        command=["/.virtual_env/bin/gunicorn", "--bind=127.0.0.1:8000", "<http://test_docker.app:app|test_docker.app:app>"],
        workdir="root",
        dependencies=["test_docker", "//:gunicorn", "//:gevent",],
)
i.e. with the following request
Copy code
s = await Get(
                SourceFiles,
                SourceFilesRequest,
                SourceFilesRequest(
                    sources_fields=[t.get(Sources)],
                    for_sources_types=[RelocatedFilesSources],
                    enable_codegen=True
                )
            )
I still get
Copy code
[nate@ragin-cajun pants-docker]$ ./pants package test_docker:dockerized_flask_app
12:28:12.95 [INFO] Initializing scheduler...
12:28:13.11 [INFO] Scheduler initialized.
12:28:13.99 [INFO] relocated files: ()
f
maybe try
for_sources_types=[FilesSources]
the codegen is for
RelocatedFilesSources
->
FilesSources
the class docs for the related
HydrateSourcesRequest
state:
“”"Convert raw sources globs into an instance of HydratedSources.
If you only want to handle certain Sources fields, such as only PythonSources, set
for_sources_types
. Any invalid sources will return a
HydratedSources
instance with an
empty snapshot and
sources_type = None
.
If
enable_codegen
is set to
True
, any codegen sources will try to be converted to one
of the
for_sources_types
.
the last paragraph suggests that the destination type is what is needed there
a
oh my gosh thats what did it!
f
I was just guessing by the way. I don’t have any more specific knowledge of the plugin system. These docs could be better.
a
it was quite confusing to me what I needed to do! great! thank you!
👍 1
w
@average-australia-85137: also, fwiw: @happy-kitchen-89482 and @curved-television-6568 are currently collaborating on a docker integration in main: https://github.com/pantsbuild/pants/pull/12363: i’m sure they’d love to join forces, or gather feedback
a
happy to contribute in any method ya'll are interested in
👍 1
❤️ 1
c
@witty-crayon-22786 Yes, we’ve made contact :)
👍 1