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

incompatible_load_python_rules_from_bzl: Python rules now need to be loaded from @rules_python #9006

Closed
brandjon opened this issue Jul 29, 2019 · 11 comments
Assignees
Labels
incompatible-change Incompatible/breaking change P2 We'll consider working on this in future. (Assignee optional) team-Rules-Python Native rules for Python type: process

Comments

@brandjon
Copy link
Member

brandjon commented Jul 29, 2019

Flag: --incompatible_load_python_rules_from_bzl
Available since: 0.29
Will be flipped in: 2.0
Feature tracking issue: #8893

Motivation

The "core" Python rule logic is currently bundled with Bazel as a combination of native symbols and symbols loaded from @bazel_tools//tools/python. Eventually the native rules will be ported to Starlark, and all symbols will live in bazelbuild/rules_python. In the meantime, we're making it so the user loads these symbols from the rules_python repository, even though they're largely still implemented inside of Bazel itself. This will help make future migration of rule logic to rules_python less painful.

Change

Enabling this flag causes the four native Python rules,

  • py_library
  • py_binary
  • py_test
  • py_runtime

to fail at analysis time when they are used directly instead of via rules_python.

Migration

Any usage of the these rules as a built-in symbol should be replaced with a use of the macros defined in rules_python as follows. (All four macros are defined in defs.bzl.)

# My BUILD file

py_binary(
    ...
)

should become

# My BUILD file

load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
    ...
)

In the case of macros in .bzl files,

# My .bzl file

def my_macro(...):
    native.py_binary(
        ...
    )

should become

# My .bzl file

load("@rules_python//python:defs.bzl", "py_binary")

def my_macro(...):
    py_binary(
        ...
    )

To access the @rules_python repo you'll need to make sure you have the following in your WORKSPACE file:

# My WORKSPACE file

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "rules_python",
    remote = "https://github.com/bazelbuild/rules_python.git",
    commit = "4b84ad270387a7c439ebdccfd530e2339601ef27",  # (2019-08-02 or later)
)

load("@rules_python//python:repositories.bzl", "py_repositories")
py_repositories()

If you're using rules_python for pip support (which is separate from the core Python rules), you'll also need to call the pip_repositories() function.

Note that until recently, @rules_python went by the workspace name @io_bazel_rules_python. If you have a definition for the old name in your WORKSPACE file then you'll need to rename it, and ensure your commit is updated to a recent version.

@brandjon brandjon added incompatible-change Incompatible/breaking change team-Rules-Python Native rules for Python breaking-change-1.0 labels Jul 29, 2019
@brandjon brandjon self-assigned this Jul 29, 2019
brandjon added a commit to brandjon/bazel that referenced this issue Jul 30, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Jul 31, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Jul 31, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Work toward bazelbuild#9006.

RELNOTES: None
bazel-io pushed a commit that referenced this issue Jul 31, 2019
This adds `@bazel_tools//tools/python:private/defs.bzl`, which defines macro wrappers around the four native Python rules (`py_library`, `py_binary`, `py_test`, `py_runtime`). These wrappers simulate the behavior of `@rules_python//python:defs.bzl`, so that they are compatible with the upcoming `--incompatible_load_python_rules_from_bzl` flag.

This change also updates many direct uses of the native Python rules to use the wrappers instead. However, changes to the third_party/ directory have to be in a separate commit.

The new macros should only be used by Bazel itself. All external users should use @rules_python instead.

I'm omitting updating src/test/py/bazel/testdata/runfiles_test/*/BUILD.mock files because they appear to be tests that shouldn't complain until the flag is flipped, and I'm not sure that adding a load won't break them. Also not updating the examples/ dir for similar reasons.

Work toward #9006.

RELNOTES: None
PiperOrigin-RevId: 260858287
brandjon added a commit to brandjon/bazel that referenced this issue Jul 31, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Jul 31, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit that referenced this issue Jul 31, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Work toward #9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Some of the uses are replaced by the file under the tools/ dir, which is
already used elsewhere in Bazel. A few uses have to use a new @rules_python
repo (see also bazelbuild#9029).

Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Some of the uses are replaced by the file under the tools/ dir, which is
already used elsewhere in Bazel. A few uses have to use a new @rules_python
repo (see also bazelbuild#9029).

Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Some of the uses are replaced by the file under the tools/ dir, which is
already used elsewhere in Bazel. A few uses have to use a new @rules_python
repo (see also bazelbuild#9029).

Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Some of the uses are replaced by the file under the tools/ dir, which is
already used elsewhere in Bazel. A few uses have to use a new @rules_python
repo (see also #9029).

Work toward #9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
This stub contains only one relevant file, @rules_python//python:defs.bzl,
which mimics the file at the same path in bazelbuild/rules_python. Having this
repo gives us a way to inject this defs.bzl file into our protobuf dependency,
which is loaded as a separate (local) external repo and therefore cannot access
the existing //tools/python:private/defs.bzl in Bazel's own workspace. It also
means we'll be compatible with the future upstream migration to fix protobuf
for bazelbuild#9006.

A separate PR will add this to the Bazel root WORKSPACE file (since
third_party/ must be updated in a separate commit).

Break-out of bazelbuild#9019. Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
These will be used by third_party/protobuf in a follow-up.

Also update a test that uses protobuf and that will break when protobuf is
updated (since the test can't be changed in the same PR as third_party/).

Break-out of bazelbuild#9019. Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Some of the uses are replaced by the file under the tools/ dir, which is
already used elsewhere in Bazel. A few uses have to use a new @rules_python
repo (see also #9029).

Work toward #9006.

RELNOTES: None
bazel-io pushed a commit that referenced this issue Aug 1, 2019
This stub contains only one relevant file, @rules_python//python:defs.bzl,
which mimics the file at the same path in bazelbuild/rules_python. Having this
repo gives us a way to inject this defs.bzl file into our protobuf dependency,
which is loaded as a separate (local) external repo and therefore cannot access
the existing //tools/python:private/defs.bzl in Bazel's own workspace. It also
means we'll be compatible with the future upstream migration to fix protobuf
for #9006.

A separate PR will add this to the Bazel root WORKSPACE file (since
third_party/ must be updated in a separate commit).

Break-out of #9019. Work toward #9006.

Closes #9044.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
These will be used by third_party/protobuf in a follow-up.

Also update a test that uses protobuf and that will break when protobuf is
updated (since the test can't be changed in the same PR as third_party/).

Break-out of bazelbuild#9019. Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
These will be used by third_party/protobuf in a follow-up.

Also update a test that uses protobuf and that will break when protobuf is
updated (since the test can't be changed in the same PR as third_party/).

Break-out of bazelbuild#9019. Work toward bazelbuild#9006.

RELNOTES: None
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Some of the uses are replaced by the file under the tools/ dir, which is
already used elsewhere in Bazel. A few uses have to use a new @rules_python
repo (see also bazelbuild#9029).

Work toward bazelbuild#9006.

RELNOTES: None
bazel-io pushed a commit that referenced this issue Aug 1, 2019
These will be used by third_party/protobuf in a follow-up.

Also update a test that uses protobuf and that will break when protobuf is
updated (since the test can't be changed in the same PR as third_party/).

Break-out of #9019. Closes #9048. Work toward #9006.

RELNOTES: None
PiperOrigin-RevId: 261176215
brandjon added a commit to brandjon/bazel that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Some of the uses are replaced by the file under the tools/ dir, which is
already used elsewhere in Bazel. A few uses have to use a new @rules_python
repo (see also bazelbuild#9029).

Work toward bazelbuild#9006.

RELNOTES: None
bazel-io pushed a commit that referenced this issue Aug 1, 2019
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Some of the uses are replaced by the file under the tools/ dir, which is
already used elsewhere in Bazel. A few uses have to use a new @rules_python
repo (see also #9029).

Work toward #9006.

Closes #9019.

RELNOTES: None
@Yannic
Copy link
Contributor

Yannic commented Mar 6, 2020

What's the status of this flag? Is it going to be flipped eventually or has it been abandoned? If it's the former, can we add migration-X labels for 2.x and 3.x so it'll be detected by Bazelisk and tested on Buildkite?

@wchargin
Copy link
Contributor

wchargin commented Nov 4, 2020

+1 to @Yannic’s question: this flag was supposed to be flipped in Bazel
2.0, but still isn’t flipped in Bazel 3.7, which released almost a year
later. Is it still realistically planned? This matters for downstream
rules_* maintainers; we need to know whether we have to go to the
trouble of pulling in a rules_python dependency for helper scripts.

@lberki lberki assigned lberki and unassigned brandjon Nov 18, 2020
@lberki lberki added the P2 We'll consider working on this in future. (Assignee optional) label Nov 18, 2020
@lberki
Copy link
Contributor

lberki commented Apr 13, 2021

Ow, this bug is still open? After careful consideration, we decided that this would be change that causes way too much breakage, so we decided to take another approach instead: we'll teach Bazel to be able to carry Starlark rules with itself just like it does ones implemented in Java now. We'll have to figure out how to decouple these rules from Bazel, of course, but that's a next step. (My crystal ball says that it will involve some magic connection between BUILD files and the WORKSPACE file, for example, by adding implicit load() statements to the beginning of every BUILD file, but that's just a guess)

@cpsauer
Copy link
Contributor

cpsauer commented Jan 25, 2022

If the plan is to reverse course, could it be a good idea to delete this flag?
(Genuinely not sure; just wanted to float.)

[Context: I, like others it seems, got here because someone using a tool I'd build for bazel had it flipped, breaking things. Probably via --all_incompatible_changes, so it's removal will certainly help when it ships. Maybe there's a need for this to still be around; not sure.]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
incompatible-change Incompatible/breaking change P2 We'll consider working on this in future. (Assignee optional) team-Rules-Python Native rules for Python type: process
Projects
None yet
Development

No branches or pull requests