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

Add content_hashing feature flag #107

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,9 @@ DefaultValue = //tools:wheel_resolver
Optional = true
Inherit = true

[featureflags]
PythonWheelHashing = true
[PluginConfig "feature_flags"]
DefaultValue = ""
Repeatable = true
Optional = true
Inherit = true
Help = Flags to enable in-development features, or toggle breaking changes
37 changes: 23 additions & 14 deletions build_defs/python.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,11 @@ def python_binary(name:str, main:str, srcs:list=[], resources:list=[], out:str=N
determine something appropriate for the given interpreter.
labels (list): Labels to apply to this rule.
"""
shebang = shebang or _interpreter_cmd(interpreter)
zipsafe_flag = '' if zip_safe is False else '--zip_safe'
cmd = '$TOOLS_PEX -s "%s" -m "%s" %s --interpreter_options="%s" --stamp="$RULE_HASH"' % (
shebang, CONFIG.PYTHON.MODULE_DIR, zipsafe_flag, CONFIG.PYTHON.INTERPRETER_OPTIONS)
if site:
cmd += ' -S'

if CONFIG.BUILD_CONFIG == 'dbg':
# Both `pdb` and `debugpy` require the pex to be exploded to get debugging to work.
cmd = f'{cmd} -d={CONFIG.PYTHON.DEBUGGER}'.replace(' --zip_safe', '')

assert main not in srcs, "main file should not be included in srcs"

lib_rule_name = '_%s#lib' % name
lib_rule = python_library(
name='_%s#lib' % name,
name=lib_rule_name,
srcs=[main] + srcs,
resources=resources,
interpreter=interpreter,
Expand All @@ -152,6 +142,26 @@ def python_binary(name:str, main:str, srcs:list=[], resources:list=[], out:str=N
test_only=test_only,
)

shebang = shebang or _interpreter_cmd(interpreter)
zipsafe_flag = '' if zip_safe is False else '--zip_safe'
cmd = '$TOOLS_PEX -s "%s" -m "%s" %s --interpreter_options="%s"' % (
shebang, CONFIG.PYTHON.MODULE_DIR, zipsafe_flag, CONFIG.PYTHON.INTERPRETER_OPTIONS)

# If content hashing feature flag is enabled, we use the hash of the built
# dependencies instead of the RULE_HASH as the base of the pex extraction
# folder
if "content_hashing" in CONFIG.PYTHON.FEATURE_FLAGS:
cmd += f' --stamp="$(hash :{lib_rule_name})"'
else:
cmd += ' --stamp="$RULE_HASH"'

if site:
cmd += ' -S'

if CONFIG.BUILD_CONFIG == 'dbg':
# Both `pdb` and `debugpy` require the pex to be exploded to get debugging to work.
cmd = f'{cmd} -d={CONFIG.PYTHON.DEBUGGER}'.replace(' --zip_safe', '')

# Use the pex tool to compress the entry point & add all the bootstrap helpers etc.
pex_rule = build_rule(
name = name,
Expand All @@ -161,7 +171,7 @@ def python_binary(name:str, main:str, srcs:list=[], resources:list=[], out:str=N
cmd=cmd,
requires=['py', 'pex'],
pre_build=_handle_zip_safe if zip_safe is None else None,
deps=deps,
deps=deps + [f":{lib_rule_name}"],
needs_transitive_deps=True, # Needed so we can find anything with zip_safe=False on it.
output_is_complete=True,
tools={
Expand Down Expand Up @@ -729,4 +739,3 @@ if CONFIG.BAZEL_COMPATIBILITY:
py_library = python_library
py_binary = python_binary
py_test = python_test