-
Notifications
You must be signed in to change notification settings - Fork 4k
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
BUILD- and WORKSPACE-loaded bzl files should have the same predeclared environment #11954
Comments
Agreed. One benefit is that the set of predeclared names is an input to the Starlark resolver (and thus compiler), so different names mean the .bzl file must be compiled differently. |
This is to help protect the authors of exports.bzl from shooting themselves in the foot. Main changes: - Add validation of the injected builtins to ensure we don't inadvertently create new symbols, or override important symbols like `rule()`. - Don't allow access to overridable symbols (e.g., `CcInfo`) from within @builtins bzls, since their meaning would be different than in regular bzl files. Also: - Spin off a new test suite for PackageFactory's management of the environment. - Add a test that BUILD and WORKSPACE bzl files have minimal differences in their environments. They should eventually converge (#11954). - Update comments/TODOs to reference #11954. - Eliminate unneeded method PackageFactory#getNativeRules. It was originally intended for when injection logic was going to live inside StarlarkBuiltinsFunction. - Add error handling and test for case where StarlarkBuiltinsFunction fails due to disallowed injections. Work toward #11437 and #11954. RELNOTES: None PiperOrigin-RevId: 329207541
Previously, ConfiguredRuleClassProvider#getEnvironment returned an env that contained a binding for "native", mapping to a StarlarkNativeModule instance. This got overwritten in PackageFactory to instead map to a struct containing the same fields (e.g. glob()) plus all the registered rules (e.g. cc_library). Since ASTFileLookupFunction used the getEnvironment() method, it relied on the bogus StarlarkNativeModule object being there for static name validation to work. As of unknown commit, ASTFileLookupFunction now queries PackageFactory for the final environment, so we are free to no longer add this incomplete dummy object. Except for BzlEnv, StarlarkNativeModule is now only used in conjunction with Starlark.addMethods(), not Starlark.addModule(), so the object itself is almost inaccessible. (Its class is still the home of the user documentation for the special native object fields.) Verified that ApiExporter produces the same result after this change. (In fact, that target doesn't even depend on this CL.) Cleanup along the way to #11437 and #11954. RELNOTES: None PiperOrigin-RevId: 336211639
Note that the set of names is currently the same (and there's a test to ensure it remains the same). Deprioritizing this issue because the benefit is mostly aesthetic, and will become irrelevant as we migrate from the current repository mechanism to bzlmod. |
Discussed with @brandjon yesterday:
|
@brandjon Is it possible that bazel/src/test/java/com/google/devtools/build/lib/packages/BazelStarlarkEnvironmentTest.java Line 59 in 968422d
|
We discussed this again more recently. We confirm that there's conceptually only one This is basically the same as what happens today if you try to
No, I don't think so. I believe all non- |
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale. |
This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please post |
A .bzl file should define the same set of top-level symbols regardless of whether it was loaded on behalf of a BUILD file or WORKSPACE file. Today, the set of symbol names is the same, but the
native
modules of the two bzl types are different.We want to avoid spurious differences between types of bzls files, both for the sake of simplicity and to be friendlier to tooling. Clearly there are features that do not apply to both contexts --
native.glob()
makes no sense during WORKSPACE evaluation, andnative.register_toolchains
makes no sense during BUILD evaluation. But we can address this by failing at evaluation time if called from the wrong context. We already do that, for instance, ifnative.glob()
is called at the top level in a bzl file rather than inside a macro.Arguably we can also have BUILD and WORKSPACE files themselves share the same environment. But if we do that, let's track it in a separate bug.
@alandonovan
The text was updated successfully, but these errors were encountered: