Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of ctx.info_file and ctx.version_file in rule implementations #13360

Open
mvukov opened this issue Apr 15, 2021 · 8 comments
Open

Use of ctx.info_file and ctx.version_file in rule implementations #13360

mvukov opened this issue Apr 15, 2021 · 8 comments
Assignees
Labels
P2 We'll consider working on this in future. (Assignee optional) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: feature request

Comments

@mvukov
Copy link

mvukov commented Apr 15, 2021

Description of the problem / feature request:

At the moment information in ctx.info_file and ctx.version_file cannot be used for rule implementation.

Feature requests: what underlying problem are you trying to solve with this feature?

See for instance bazelbuild/rules_python#451. Therein, I would like to use git info, routed via workspace status script, to make an output file name of a Python wheel. Since I cannot read a file from context, at the moment I process ctx.info_file and ctx.version_file in a separate Python script and then dump a wheel version to a .txt file. The new py_wheel_push rule in that PR consumes my .txt file and then assembles the correct file name. However, it is desirable that py_wheel rule already assembles the correct wheel name, which at the moment is not possible if I want to use info from workspace status -- e.g. a wheel version in py_wheel rule as version = "{FOO}", where I expect FOO to be picked up from the workspace status.

Are there any plans to enable use of the workspace status in rule implementations?

What operating system are you running Bazel on?

Ubuntu 18.04

What's the output of bazel info release?

release 2.0.0

Have you found anything relevant by searching the web?

The only thing I could find is that Bazel context cannot read files, and that's for good reasons as far as I understand.

@gregestren
Copy link
Contributor

Where are ctx.info_file and ctx.version_file in the Bazel docs?

@mvukov
Copy link
Author

mvukov commented Apr 15, 2021

Unfortunately, they are not documented. See #9363 and #11422. FWIW, I had to dig into rules_docker repo to learn how to access the workspace status.

@aiuto
Copy link
Contributor

aiuto commented May 3, 2021

+1 to documenting these.

@tetromino
Copy link
Contributor

Clarification for future readers: the problem is that the content of ctx.info_file and version_file cannot be used in a rule implementation.

@brandjon brandjon added P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) type: feature request and removed untriaged labels Jun 3, 2021
@brandjon
Copy link
Member

brandjon commented Jun 3, 2021

We can't read file content prior to execution. But perhaps workspace status information can be (or is currently?) exposed in some alternate API.

@tetromino
Copy link
Contributor

See also #13300 requesting better documentation for reading files in Starlark.

@brandjon brandjon added untriaged team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts and removed team-Build-Language labels Nov 4, 2022
@comius comius added P2 We'll consider working on this in future. (Assignee optional) and removed P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) untriaged labels Aug 22, 2023
@comius
Copy link
Contributor

comius commented Aug 22, 2023

Assigning to @buildbreaker2021, who's currently working on this functionality, to review if there's need to update documentation and/or if this issue can be resolved.

@buildbreaker2021
Copy link
Contributor

buildbreaker2021 commented Aug 22, 2023

There is an API already implemented(although private still) which will support use-cases like these.

@StarlarkMethod(
name = "transform_version_file",
documented = false,
doc =
"Similar to <a href=\"#transform_info_file\">transform_info_file</a>, but transforms <a"
+ " href='https://bazel.build/rules/lib/builtins/ctx#version_file'>ctx.version_file</a>.",
parameters = {
@Param(
name = "transform_func",
doc = TRANSFORM_VERSION_FUNC_DOC,
allowedTypes = {@ParamType(type = StarlarkFunction.class)},
positional = false,
named = true),
@Param(
name = "template",
doc = TEMPLATE_DOC,
allowedTypes = {@ParamType(type = FileApi.class)},
positional = false,
named = true),
@Param(
name = "output_file_name",
doc = VERSION_OUTPUT_DOC,
allowedTypes = {@ParamType(type = String.class)},
positional = false,
named = true),
},
useStarlarkThread = true)
FileApi transformVersionFile(
Object transformFuncObject,
Object templateObject,
String outputFileName,
StarlarkThread thread)
throws InterruptedException, EvalException;
@StarlarkMethod(
name = "transform_info_file",
documented = false,
doc =
"Transforms <a"
+ " href='https://bazel.build/rules/lib/builtins/ctx#info_file'>ctx.info_file</a> to"
+ " a language-consumable file and writes its contents to <code>output</code>. Keys"
+ " and values are transformed by calling the <code>transform_func</code> Starlark"
+ " method, and the output file format is generated according to the"
+ " <code>template</code>. Use this call to create an action in an auxiliary rule."
+ " Create a single target for an auxiliary rule which is then used as an implicit"
+ " dependency for the main rule. Main rule is the one which needs the transformed"
+ " <code>info_file</code>. The auxiliary rule should also declare"
+ " <code>output</code> file to which transformed content is written. The"
+ " <code>output</code> file is then provided to the dependant main rule. This will"
+ " avoid action conflicts and duplicated file generations.",
parameters = {
@Param(
name = "transform_func",
doc = TRANSFORM_INFO_FUNC_DOC,
allowedTypes = {@ParamType(type = StarlarkFunction.class)},
positional = false,
named = true),
@Param(
name = "template",
doc = TEMPLATE_DOC,
allowedTypes = {@ParamType(type = FileApi.class)},
positional = false,
named = true),
@Param(
name = "output_file_name",
doc = INFO_OUTPUT_DOC,
allowedTypes = {@ParamType(type = String.class)},
positional = false,
named = true),
},
useStarlarkThread = true)
FileApi transformInfoFile(
Object transformFuncObject,
Object templateObject,
String outputFileName,
StarlarkThread thread)
throws InterruptedException, EvalException;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 We'll consider working on this in future. (Assignee optional) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: feature request
Projects
None yet
Development

No branches or pull requests

7 participants