Skip to content

Commit

Permalink
fix(whl_library): remove --no-index and add --no-build-isolation
Browse files Browse the repository at this point in the history
…when build sdist (#2126)

Building sdist results in `Could not find a version that satisfies the
requirement setuptool` this regressed when a fix in parameter handling
got introduced in #2091.

Before this change the building from sdist when using
`experimental_index_url`
would break because `--no-index` is passed to `pip`. This means that
`pip`
would fail to locate build time dependencies needed for the packages and
would
just not work. In `whl_library` we setup `PYTHONPATH` to have some build
dependencies available (like `setuptools`) and we could use them during
building from `sdist` and to do so we need to add `--no-build-isolation`
flag.
However, for some cases we need to also add other build-time
dependencies (e.g.
`flit_core`) so that the building of the wheel in the `repository_rule`
context
is successfuly. Removing `--no-index` allows `pip` to silently fetch the
needed
build dependencies from PyPI if they are missing and continue with the
build.

This is not a perfect solution, but it does unblock users to use the
`sdist`
distributions with the experimental feature enabled by using
`experimental_index_url` (see #260 for tracking of the completion).

Fixes #2118
Fixes #2152

---------

Co-authored-by: aignas <240938+aignas@users.noreply.github.com>
  • Loading branch information
ewianda and aignas committed Aug 24, 2024
1 parent b679a79 commit b99bb61
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ A brief description of the categories of changes:
* (gazelle): Update error messages when unable to resolve a dependency to be more human-friendly.

### Fixed
* (whl_library): Remove `--no-index` and add `--no-build-isolation` to the
`pip install` command when installing a wheel from a local file, which happens
when `experimental_index_url` flag is used.
* (bzlmod) get the path to the host python interpreter in a way that results in
platform non-dependent hashes in the lock file when the requirement markers need
to be evaluated.
Expand Down
4 changes: 2 additions & 2 deletions examples/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions python/private/pypi/whl_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,10 @@ def _whl_library_impl(rctx):
whl_path = rctx.path(rctx.attr.filename)
else:
# It is an sdist and we need to tell PyPI to use a file in this directory
# and not use any indexes.
extra_pip_args.extend(["--no-index", "--find-links", "."])
# and, allow getting build dependencies from PYTHONPATH, which we
# setup in this repository rule, but still download any necessary
# build deps from PyPI (e.g. `flit_core`) if they are missing.
extra_pip_args.extend(["--no-build-isolation", "--find-links", "."])

args = _parse_optional_attrs(rctx, args, extra_pip_args)

Expand Down
7 changes: 0 additions & 7 deletions tests/pypi/evaluate_markers/BUILD.bazel

This file was deleted.

20 changes: 20 additions & 0 deletions tests/pypi/integration/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@dev_pip//:requirements.bzl", "all_requirements")
load(":transitions.bzl", "transition_rule")

build_test(
name = "all_requirements_build_test",
targets = all_requirements,
)

# Rule that transitions dependencies to be built from sdist
transition_rule(
name = "all_requirements_from_sdist",
testonly = True,
deps = all_requirements,
)

build_test(
name = "all_requirements_from_sdist_build_test",
targets = ["all_requirements_from_sdist"],
)
24 changes: 24 additions & 0 deletions tests/pypi/integration/transitions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
""" Define a custom transition that sets the pip_whl flag to no """

def _flag_transition_impl(_settings, _ctx):
return {"//python/config_settings:pip_whl": "no"}

flag_transition = transition(
implementation = _flag_transition_impl,
inputs = [],
outputs = ["//python/config_settings:pip_whl"],
)

# Define a rule that applies the transition to dependencies
def _transition_rule_impl(_ctx):
return [DefaultInfo()]

transition_rule = rule(
implementation = _transition_rule_impl,
attrs = {
"deps": attr.label_list(cfg = flag_transition),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
)

0 comments on commit b99bb61

Please sign in to comment.