Who's using code generation through Pants right no...
# general
f
Who's using code generation through Pants right now? And how to do you get it to play nice with IDEs that expect those files to exist on disk, rather than just on-demand in a Pants execution sandbox?
p
I cheated. I wrote a fmt plugin that actually generates code because fmt can write to files in the workspace unlike codegen which is only for files that are "ephemeral" and should not be committed to version control. You can only get the codegen files in a packaged artifact or by exporting it.
🤯 1
l
In VSCode, you can use
python.analysis.extraPaths
. That will give you code completion and go to the definition. Example:
Copy code
"python.analysis.extraPaths": [
        "./dist/codegen/src"
    ]
d
there is also the
export-codegen
goal as it turns out, been around since 2.16 at least.
as per @proud-dentist-22844’s comment re: "or by exporting it."
p
The big gotcha with
export-codegen
is that it will only export under a subfolder like
dist/
, so it does not help for files you want to generate and store WITH the rest of your sources.
d
Yes, but I'd argue you rarely want to store/commit generated code in your source tree. If you do, then sure, fair cop, your workaround is a good one. 👍
p
go-generate
is another goal that provides a way for codegen to end up with the sources, as versioning generated files is standard practice with go development. Unlike standard pants codegen, you actually have to run
go-generate
to get the codegen to run.
👍 1
What if there were a
persist-codegen
goal similar to
export-codegen
, but it “persists” the generated files in the workspace (not under dist). For that to work well, I think the targets that trigger the codegen would probably need a
persist
parameter
target_abcd(…, persist=True)
so that they can opt into a persisted mode instead of the standard ephemeral mode.
Then there could be a linter that warns if the persisted files need to be regenerated.
d
Sounds reasonable to me. It does feel like it would be better as something you need to opt-into. Potentially if the
persist-codegen
command would have made a change, but can't because the target hasn't opted in, spit out a warning/info/whatever is appropriate indicating that the
persist=True
option is required? Otherwise you might get a bunch of people saying "hey, I used
persist-codegen
, why isn't it showing up in my workspace?"
it's one of those, damned if you do, damned if you don't, unfortunately. If you don't do opt-in, the tool is potentially making destructive changes to the source tree. If you do the opt-in approach, the other crowd will be like "why isn't it doing what I just told it to?" 🤷
1
l
Not ideal but that does the job:
Copy code
pants export-codegen :: && cd dist/codegen && cp --parents **/*.{py,pyi} <path_to_your_repo>
I am using that in VSCode Tasks with
<path_to_your_repo>
set to
${workspaceFolder}
.
f
I could see this being a feature though, especially if there's some guardrails around what files it can touch, and those file patterns are integrated into the fs watcher ignores like gitignore and pants_ignore patterns
👍 3