Skip to content

Commit a39c489

Browse files
committed
Merge branch 'main' into feat/musl-linux-toolchain
2 parents 243b4ef + 68d1b41 commit a39c489

File tree

14 files changed

+191
-107
lines changed

14 files changed

+191
-107
lines changed

.bazelci/presubmit.yml

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ buildifier:
3434
build_flags:
3535
- "--keep_going"
3636
- "--build_tag_filters=-integration-test"
37+
- "--config=bazel7.x"
3738
test_targets:
3839
- "--"
3940
- "..."
@@ -84,16 +85,6 @@ buildifier:
8485
- //tests:version_3_8_test
8586
- //tests:version_3_9_test
8687
- //tests:version_default_test
87-
.pystar_base: &pystar_base
88-
bazel: "7.x"
89-
environment:
90-
RULES_PYTHON_ENABLE_PYSTAR: "1"
91-
build_flags:
92-
- "--config=bazel7.x"
93-
test_flags:
94-
# The doc check tests fail because the Starlark implementation makes the
95-
# PyInfo and PyRuntimeInfo symbols become documented.
96-
- "--test_tag_filters=-integration-test,-doc_check_test"
9788
tasks:
9889
gazelle_extension_min:
9990
<<: *common_workspace_flags_min_bazel
@@ -139,26 +130,18 @@ tasks:
139130
name: "Default: Ubuntu, upcoming Bazel"
140131
platform: ubuntu2004
141132
bazel: last_rc
142-
pystar_ubuntu_workspace:
143-
<<: *reusable_config
144-
<<: *pystar_base
145-
name: "Default test: Ubuntu, Pystar, workspace"
146-
platform: ubuntu2004
147-
pystar_ubuntu_bzlmod:
133+
ubuntu_workspace:
148134
<<: *reusable_config
149-
<<: *pystar_base
150-
name: "Default test: Ubuntu, Pystar, bzlmod"
135+
name: "Default: Ubuntu, workspace"
151136
platform: ubuntu2004
152-
pystar_mac_workspace:
137+
mac_workspace:
153138
<<: *reusable_config
154139
<<: *common_workspace_flags
155-
<<: *pystar_base
156-
name: "Default test: Mac, Pystar, workspace"
140+
name: "Default: Mac, workspace"
157141
platform: macos
158-
pystar_windows_workspace:
142+
windows_workspace:
159143
<<: *reusable_config
160-
<<: *pystar_base
161-
name: "Default test: Windows, Pystar, workspace"
144+
name: "Default: Windows, workspace"
162145
platform: windows
163146

164147
debian:
@@ -250,6 +233,13 @@ tasks:
250233
working_directory: examples/bzlmod
251234
platform: ubuntu2004
252235
bazel: 7.x
236+
integration_test_bzlmod_ubuntu_upcoming:
237+
<<: *reusable_build_test_all
238+
<<: *coverage_targets_example_bzlmod
239+
name: "examples/bzlmod: Ubuntu, upcoming Bazel"
240+
working_directory: examples/bzlmod
241+
platform: ubuntu2004
242+
bazel: last_rc
253243
integration_test_bzlmod_debian:
254244
<<: *reusable_build_test_all
255245
<<: *coverage_targets_example_bzlmod
@@ -264,12 +254,27 @@ tasks:
264254
working_directory: examples/bzlmod
265255
platform: macos
266256
bazel: 7.x
257+
integration_test_bzlmod_macos_upcoming:
258+
<<: *reusable_build_test_all
259+
<<: *coverage_targets_example_bzlmod
260+
name: "examples/bzlmod: macOS, upcoming Bazel"
261+
working_directory: examples/bzlmod
262+
platform: macos
263+
bazel: last_rc
267264
integration_test_bzlmod_windows:
268265
<<: *reusable_build_test_all
269266
# coverage is not supported on Windows
270267
name: "examples/bzlmod: Windows"
271268
working_directory: examples/bzlmod
272269
platform: windows
270+
bazel: 7.x
271+
integration_test_bzlmod_windows_upcoming:
272+
<<: *reusable_build_test_all
273+
# coverage is not supported on Windows
274+
name: "examples/bzlmod: Windows, upcoming Bazel"
275+
working_directory: examples/bzlmod
276+
platform: windows
277+
bazel: last_rc
273278
integration_test_bzlmod_ubuntu_lockfile:
274279
<<: *reusable_build_test_all
275280
<<: *coverage_targets_example_bzlmod

CHANGELOG.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ Unreleased changes template.
3030
3131
{#v0-0-0-changed}
3232
### Changed
33-
* Nothing yet.
33+
* Nothing changed.
3434
3535
{#v0-0-0-fixed}
3636
### Fixed
37-
* Nothing yet.
37+
* Nothing fixed.
3838
3939
{#v0-0-0-added}
4040
### Added
41-
* Nothing yet.
41+
* Nothing added.
4242
4343
{#v0-0-0-removed}
4444
### Removed
45-
* Nothing yet.
45+
* Nothing removed.
4646
-->
4747

4848
{#v0-0-0}
@@ -70,14 +70,16 @@ Unreleased changes template.
7070
bzlmod extension.
7171

7272
Other changes:
73-
* Nothing yet
73+
* (python_repository) Start honoring the `strip_prefix` field for `zstd` archives.
7474

7575
{#v0-0-0-fixed}
7676
### Fixed
7777
* (toolchains) stop depending on `uname` to get the value of the host platform.
7878

7979
{#v0-0-0-added}
8080
### Added
81+
* (gazelle): Parser failures will now be logged to the terminal. Additional
82+
details can be logged by setting `GAZELLE_VERBOSE=1`.
8183
* (toolchains) allow users to select which variant of the support host toolchain
8284
they would like to use through
8385
`RULES_PYTHON_REPO_TOOLCHAIN_{VERSION}_{OS}_{ARCH}` env variable setting. For
@@ -90,8 +92,35 @@ Other changes:
9092

9193
{#v0-0-0-removed}
9294
### Removed
95+
* Nothing removed.
96+
97+
{#v0-40-0}
98+
## [0.40.0] - 2024-11-17
99+
100+
[0.40.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.40.0
101+
102+
{#v0-40-changed}
103+
### Changed
104+
* Nothing changed.
105+
106+
{#v0-40-fixed}
107+
### Fixed
108+
* (rules) Don't drop custom import paths if Bazel-builtin PyInfo is removed.
109+
([2414](https://github.com/bazelbuild/rules_python/issues/2414)).
110+
111+
{#v0-40-added}
112+
### Added
113+
* Nothing added.
114+
115+
{#v0-40-removed}
116+
### Removed
93117
* (publish) Remove deprecated `requirements.txt` for the `twine` dependencies.
94118
Please use `requirements_linux.txt` instead.
119+
* (python_repository) Use bazel's built in `zstd` support and remove attributes
120+
for customizing the `zstd` binary to be used for `zstd` archives in the
121+
{bzl:obj}`python_repository` repository_rule. This affects the
122+
{bzl:obj}`python_register_toolchains` and
123+
{bzl:obj}`python_register_multi_toolchains` callers in the `WORKSPACE`.
95124

96125
{#v0-39-0}
97126
## [0.39.0] - 2024-11-13

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ bazel_dep(name = "rules_cc", version = "0.0.14")
1010
bazel_dep(name = "platforms", version = "0.0.4")
1111

1212
# Those are loaded only when using py_proto_library
13-
bazel_dep(name = "rules_proto", version = "6.0.2")
13+
bazel_dep(name = "rules_proto", version = "7.0.2")
1414
bazel_dep(name = "protobuf", version = "29.0-rc2", repo_name = "com_google_protobuf")
1515

1616
internal_deps = use_extension("//python/private:internal_deps.bzl", "internal_deps")

docs/environment-variables.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ Determines the verbosity of logging output for repo rules. Valid values:
1515
* `TRACE`
1616
:::
1717

18+
:::{envvar} RULES_PYTHON_REPO_TOOLCHAIN_VERSION_OS_ARCH
19+
20+
Determines the python interpreter platform to be used for a particular
21+
interpreter `(version, os, arch)` triple to be used in repository rules.
22+
Replace the `VERSION_OS_ARCH` part with actual values when using, e.g.
23+
`3_13_0_linux_x86_64`. The version values must have `_` instead of `.` and the
24+
os, arch values are the same as the ones mentioned in the
25+
`//python:versions.bzl` file.
26+
:::
27+
1828
:::{envvar} RULES_PYTHON_PIP_ISOLATED
1929

2030
Determines if `--isolated` is used with pip.

docs/precompiling.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ can use an opt-in or opt-out approach by setting its value:
3939
* targets must opt-out: `--@rules_python//python/config_settings:precompile=enabled`
4040
* targets must opt-in: `--@rules_python//python/config_settings:precompile=disabled`
4141

42+
## Pyc-only builds
43+
44+
A pyc-only build (aka "source less" builds) is when only `.pyc` files are
45+
included; the source `.py` files are not included.
46+
47+
To enable this, set
48+
{bzl:obj}`--@rules_python//python/config_settings:precompile_source_retention=omit_source`
49+
flag on the command line or the {bzl:attr}`precompile_source_retention=omit_source`
50+
attribute on specific targets.
51+
52+
The advantage of pyc-only builds are:
53+
* Fewer total files in a binary.
54+
* Imports _may_ be _slightly_ faster.
55+
56+
The disadvantages are:
57+
* Error messages will be less precise because the precise line and offset
58+
information isn't in an pyc file.
59+
* pyc files are Python major-version specific.
60+
61+
:::{note}
62+
pyc files are not a form of hiding source code. They are trivial to uncompile,
63+
and uncompiling them can recover almost the original source.
64+
:::
65+
4266
## Advanced precompiler customization
4367

4468
The default implementation of the precompiler is a persistent, multiplexed,

examples/bzlmod/MODULE.bazel.lock

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gazelle/python/file_parser.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package python
1717
import (
1818
"context"
1919
"fmt"
20+
"log"
2021
"os"
2122
"path/filepath"
2223
"strings"
@@ -55,7 +56,10 @@ func NewFileParser() *FileParser {
5556
return &FileParser{}
5657
}
5758

58-
func ParseCode(code []byte) (*sitter.Node, error) {
59+
// ParseCode instantiates a new tree-sitter Parser and parses the python code, returning
60+
// the tree-sitter RootNode.
61+
// It prints a warning if parsing fails.
62+
func ParseCode(code []byte, path string) (*sitter.Node, error) {
5963
parser := sitter.NewParser()
6064
parser.SetLanguage(python.GetLanguage())
6165

@@ -64,9 +68,27 @@ func ParseCode(code []byte) (*sitter.Node, error) {
6468
return nil, err
6569
}
6670

67-
return tree.RootNode(), nil
71+
root := tree.RootNode()
72+
if root.HasError() {
73+
log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path)
74+
75+
verbose, envExists := os.LookupEnv("GAZELLE_VERBOSE")
76+
if envExists && verbose == "1" {
77+
for i := 0; i < int(root.ChildCount()); i++ {
78+
child := root.Child(i)
79+
if child.IsError() {
80+
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code))
81+
log.Printf("The above was parsed as: %v", child.String())
82+
}
83+
}
84+
}
85+
}
86+
87+
return root, nil
6888
}
6989

90+
// parseMain returns true if the python file has an `if __name__ == "__main__":` block,
91+
// which is a common idiom for python scripts/binaries.
7092
func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
7193
for i := 0; i < int(node.ChildCount()); i++ {
7294
if err := ctx.Err(); err != nil {
@@ -94,6 +116,8 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
94116
return false
95117
}
96118

119+
// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
120+
// representing if the parse was OK or not.
97121
func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
98122
switch node.Type() {
99123
case sitterNodeTypeDottedName:
@@ -112,6 +136,9 @@ func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
112136
return module{}, false
113137
}
114138

139+
// parseImportStatements parses a node for import statements, returning true if the node is
140+
// an import statement. It updates FileParser.output.Modules with the `module` that the
141+
// import represents.
115142
func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
116143
if node.Type() == sitterNodeTypeImportStatement {
117144
for j := 1; j < int(node.ChildCount()); j++ {
@@ -146,6 +173,8 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
146173
return true
147174
}
148175

176+
// parseComments parses a node for comments, returning true if the node is a comment.
177+
// It updates FileParser.output.Comments with the parsed comment.
149178
func (p *FileParser) parseComments(node *sitter.Node) bool {
150179
if node.Type() == sitterNodeTypeComment {
151180
p.output.Comments = append(p.output.Comments, comment(node.Content(p.code)))
@@ -180,7 +209,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
180209
}
181210

182211
func (p *FileParser) Parse(ctx context.Context) (*ParserOutput, error) {
183-
rootNode, err := ParseCode(p.code)
212+
rootNode, err := ParseCode(p.code, p.relFilepath)
184213
if err != nil {
185214
return nil, err
186215
}

python/private/common.bzl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,22 @@ def filter_to_py_srcs(srcs):
263263
return [f for f in srcs if f.extension == "py"]
264264

265265
def collect_imports(ctx, semantics):
266-
return depset(direct = semantics.get_imports(ctx), transitive = [
267-
dep[PyInfo].imports
268-
for dep in ctx.attr.deps
269-
if PyInfo in dep
270-
] + [
271-
dep[BuiltinPyInfo].imports
272-
for dep in ctx.attr.deps
273-
if BuiltinPyInfo in dep
274-
] if BuiltinPyInfo != None else [])
266+
"""Collect the direct and transitive `imports` strings.
267+
268+
Args:
269+
ctx: {type}`ctx` the current target ctx
270+
semantics: semantics object for fetching direct imports.
271+
272+
Returns:
273+
{type}`depset[str]` of import paths
274+
"""
275+
transitive = []
276+
for dep in ctx.attr.deps:
277+
if PyInfo in dep:
278+
transitive.append(dep[PyInfo].imports)
279+
if BuiltinPyInfo != None and BuiltinPyInfo in dep:
280+
transitive.append(dep[BuiltinPyInfo].imports)
281+
return depset(direct = semantics.get_imports(ctx), transitive = transitive)
275282

276283
def collect_runfiles(ctx, files = depset()):
277284
"""Collects the necessary files from the rule's context.

0 commit comments

Comments
 (0)