How to include generated files in "bazel-out" in the source tree?

662 views
Skip to first unread message

John Adams

unread,
Dec 17, 2022, 4:08:23 PM12/17/22
to bazel-discuss
I am trying to use https://github.com/apollographql/apollo-kotlin which generates some *.kt files in the bazel-out and bazel-bin directories.

How can I include these files when building a library?

Trying to do this but it isn't working:

kt_jvm_library(
     name = "generated-stuff", 
     srcs = ["bazel-bin/src/main/java/com/example/Hello.kt"], 
     deps = [ ... ], 
)

Not sure if I need to use a genrule for this? But have not been able to figure out how.

Appreciate any help here. Thanks

John Adams

unread,
Dec 17, 2022, 4:58:43 PM12/17/22
to bazel-discuss
Full context: This is the repo I am trying to test: https://github.com/vgrec/bazel-graphql-poc

Just need to figure out how the generated files can be included in srcs?

John Adams

unread,
Dec 17, 2022, 5:23:32 PM12/17/22
to bazel-discuss
In Gradle I can do this with:

sourceSets {
    main {
        java {
            srcDir ('${buildDir.absolutePath}/generated/...')
        }
        resources {
            srcDir ('config')
        }
    }
}

I wonder can I find a Bazel equivalent for ${buildDir.absolutePath}.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/ffe25a51-e967-4c4f-9cae-b30dd9770f83n%40googlegroups.com.

David Turner

unread,
Jan 2, 2023, 11:46:15 AM1/2/23
to John Adams, bazel-discuss
Two things:

1) It is not possible to copy files to the source tree using `bazel build`, this is by design and enforced by the build sandbox.
On the other hand, you can create a script that will be invoked by `bazel run`, which will copy generated build artifacts in the source tree. This is not considered part of the build, and doesn't need to be hermetic.

2) You cannot use generated file paths directly as sources in your build rules, however, you can list target labels in `srcs`. The effect will be that the paths to the corresponding configure target's "default output files" will be used as inputs, which is probably what you want.

Hope this helps.

Reply all
Reply to author
Forward
0 new messages