bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.22k stars 4.07k forks source link

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

Open mvukov opened 3 years ago

mvukov commented 3 years ago

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 https://github.com/bazelbuild/rules_python/pull/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 commented 3 years ago

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

mvukov commented 3 years ago

Unfortunately, they are not documented. See https://github.com/bazelbuild/bazel/issues/9363 and https://github.com/bazelbuild/bazel/issues/11422. FWIW, I had to dig into rules_docker repo to learn how to access the workspace status.

aiuto commented 3 years ago

+1 to documenting these.

tetromino commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

comius commented 1 year ago

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 commented 1 year ago

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

https://github.com/bazelbuild/bazel/blob/8828015bb6d07f7b962b05c54c65c0c9ed8aa528/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/StarlarkActionFactoryApi.java#L849-L926