Skip to content

Commit

Permalink
Shade androidx.room.compiler dependencies in Dagger.
Browse files Browse the repository at this point in the history
This CL also adds a python script that will validate each jar deployed through deploy-library.sh to prevent unexpected classes from leaking into our deployed jars in the future.

RELNOTES=N/A
PiperOrigin-RevId: 488388271
  • Loading branch information
bcorso authored and Dagger Team committed Nov 14, 2022
1 parent 633059f commit 94118a2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions util/deploy-dagger.sh
Expand Up @@ -54,7 +54,7 @@ _deploy \
# This artifact uses the shaded classes from dagger-spi, so we use the same
# shading rules so that our references to those classes are shaded the same way.
_deploy \
"com.google.auto.common,dagger.spi.shaded.auto.common;androidx.room.compiler.processing,dagger.spi.shaded.androidx.room.compiler.processing" \
"com.google.auto.common,dagger.spi.shaded.auto.common;androidx.room.compiler,dagger.spi.shaded.androidx.room.compiler" \
java/dagger/internal/codegen/artifact.jar \
java/dagger/internal/codegen/pom.xml \
java/dagger/internal/codegen/artifact-src.jar \
Expand All @@ -70,7 +70,7 @@ _deploy \
""

_deploy \
"com.google.auto.common,dagger.spi.shaded.auto.common;androidx.room.compiler.processing,dagger.spi.shaded.androidx.room.compiler.processing" \
"com.google.auto.common,dagger.spi.shaded.auto.common;androidx.room.compiler,dagger.spi.shaded.androidx.room.compiler" \
java/dagger/spi/artifact.jar \
java/dagger/spi/pom.xml \
java/dagger/spi/artifact-src.jar \
Expand Down
17 changes: 17 additions & 0 deletions util/deploy-library.sh
Expand Up @@ -33,6 +33,9 @@ deploy_library() {
library="${library%.*}-shaded.${library##*.}"
fi

# Validate that the classes in the library jar begin with expected prefixes.
validate_jar $(bazel_output_file $library)

# TODO(bcorso): Consider moving this into the "gen_maven_artifact" macro, this
# requires having the version checked-in for the build system.
add_tracking_version \
Expand Down Expand Up @@ -98,6 +101,20 @@ add_tracking_version() {
fi
}

validate_jar() {
local library=$1
if [[ $library == */gwt/libgwt.jar ]]; then
python $(dirname $0)/validate-jar-entry-prefixes.py \
$library "dagger/,META-INF/,javax/inject/"
elif [[ $library == */java/dagger/hilt/android/artifact.aar ]]; then
python $(dirname $0)/validate-jar-entry-prefixes.py \
$library "dagger/,META-INF/,hilt_aggregated_deps/"
else
python $(dirname $0)/validate-jar-entry-prefixes.py \
$library "dagger/,META-INF/"
fi
}

add_automatic_module_name_manifest_entry() {
local library=$1
local module_name=$2
Expand Down
53 changes: 53 additions & 0 deletions util/validate-jar-entry-prefixes.py
@@ -0,0 +1,53 @@
"""Validates classes in the deployed jar are all within the expected packages.
Usage:
python validate-jar-entry-prefixes.py <jar-file> <comma-separated-prefixes>
"""
import re
import shutil
import sys
import tempfile
import zipfile


def main(argv):
if len(argv) > 3:
raise Exception('Expected only two arguments but got {0}'.format(len(argv)))

jar_file, prefixes = argv[-2:]
prefixes_pattern = re.compile('|'.join(prefixes.split(',')))

invalid_entries = []
if jar_file.endswith('.jar'):
invalid_entries = _invalid_entries(jar_file, prefixes_pattern)
elif jar_file.endswith('.aar'):
dirpath = tempfile.mkdtemp()
with zipfile.ZipFile(jar_file, 'r') as zip_file:
class_file = zip_file.extract('classes.jar', dirpath)
invalid_entries = _invalid_entries(class_file, prefixes_pattern)
shutil.rmtree(dirpath)
else:
raise Exception('Invalid jar file: {0}'.format(jar_file))

if invalid_entries:
raise Exception(
'Found invalid entries in {0} that do not match one of the allowed prefixes ({1}):\n {2}'
.format(
jar_file,
', '.join(['"{0}"'.format(p) for p in prefixes.split(',')]),
'\n '.join(invalid_entries))
)


def _invalid_entries(jar_file, prefixes_pattern):
invalid_entries = []
with zipfile.ZipFile(jar_file, 'r') as zip_file:
for info in zip_file.infolist():
if not info.is_dir():
if not prefixes_pattern.match(info.filename):
invalid_entries.append(info.filename)
return invalid_entries


if __name__ == '__main__':
main(sys.argv)

0 comments on commit 94118a2

Please sign in to comment.