Skip to content

Commit

Permalink
[GR-62639] Make jacaco exclusion work properly.
Browse files Browse the repository at this point in the history
PullRequest: mx/1878
  • Loading branch information
dougxc committed Feb 28, 2025
2 parents fb4e48e + a8141c6 commit 95a8845
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
16 changes: 8 additions & 8 deletions common.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"COMMENT.jdks": "When adding or removing JDKs keep in sync with JDKs in ci/common.jsonnet",
"jdks": {
"galahad-jdk": {"name": "jpg-jdk", "version": "25", "build_id": "jdk-25+8-767", "platformspecific": true, "extrabundles": ["static-libs"]},
"galahad-jdk": {"name": "jpg-jdk", "version": "25", "build_id": "jdk-25+10-1066", "platformspecific": true, "extrabundles": ["static-libs"]},

"oraclejdk17": {"name": "jpg-jdk", "version": "17.0.7", "build_id": "jdk-17.0.7+8", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-17": {"name": "labsjdk", "version": "ce-17.0.7+4-jvmci-23.1-b02", "platformspecific": true },
Expand Down Expand Up @@ -45,13 +45,13 @@

"oraclejdk23": {"name": "jpg-jdk", "version": "23", "build_id": "jdk-23+37", "platformspecific": true, "extrabundles": ["static-libs"]},

"oraclejdk-latest": {"name": "jpg-jdk", "version": "25", "build_id": "jdk-25+8", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-latest": {"name": "labsjdk", "version": "ce-25+8-jvmci-b01", "platformspecific": true },
"labsjdk-ce-latestDebug": {"name": "labsjdk", "version": "ce-25+8-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ce-latest-llvm": {"name": "labsjdk", "version": "ce-25+8-jvmci-b01-sulong", "platformspecific": true },
"labsjdk-ee-latest": {"name": "labsjdk", "version": "ee-25+8-jvmci-b01", "platformspecific": true },
"labsjdk-ee-latestDebug": {"name": "labsjdk", "version": "ee-25+8-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ee-latest-llvm": {"name": "labsjdk", "version": "ee-25+8-jvmci-b01-sulong", "platformspecific": true }
"oraclejdk-latest": {"name": "jpg-jdk", "version": "25", "build_id": "jdk-25+11", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-latest": {"name": "labsjdk", "version": "ce-25+11-jvmci-b01", "platformspecific": true },
"labsjdk-ce-latestDebug": {"name": "labsjdk", "version": "ce-25+11-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ce-latest-llvm": {"name": "labsjdk", "version": "ce-25+11-jvmci-b01-sulong", "platformspecific": true },
"labsjdk-ee-latest": {"name": "labsjdk", "version": "ee-25+11-jvmci-b01", "platformspecific": true },
"labsjdk-ee-latestDebug": {"name": "labsjdk", "version": "ee-25+11-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ee-latest-llvm": {"name": "labsjdk", "version": "ee-25+11-jvmci-b01-sulong", "platformspecific": true }
},

"eclipse": {
Expand Down
2 changes: 1 addition & 1 deletion src/mx/_impl/mx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18368,7 +18368,7 @@ def alarm_handler(signum, frame):
_CACHE_DIR = get_env('MX_CACHE_DIR', join(dot_mx_dir(), 'cache'))

# The version must be updated for every PR (checked in CI) and the comment should reflect the PR's issue
version = VersionSpec("7.40.1") # [GR-60424] Improve traceability in code owners
version = VersionSpec("7.40.2") # [GR-62639] jacaco exclusion does not work correctly

_mx_start_datetime = datetime.utcnow()

Expand Down
43 changes: 24 additions & 19 deletions src/mx/_impl/mx_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from argparse import ArgumentParser
from collections import OrderedDict
from datetime import datetime, timezone, timedelta
from pathlib import Path

from . import mx
from . import mx_util
Expand Down Expand Up @@ -888,42 +889,40 @@ def _jacoco_excludes_includes():
See https://www.eclemma.org/jacoco/trunk/doc/agent.html for details on the "includes" and "excludes" agent options.
"""
includes = list(_jacoco_includes)
baseExcludes = list(_jacoco_excludes)
baseExcludes = set(_jacoco_excludes)
excludes = []
excluded_projects = set()
aps = mx.annotation_processors()
for p in mx.projects():
if p.isJavaProject():
projsetting = getattr(p, 'jacoco', '')
assert isinstance(projsetting, str), f'jacoco must be a string, not a {type(projsetting)}'
if not _jacoco_is_package_whitelisted(p.name):
pass
elif projsetting == 'exclude':
baseExcludes.append(p.name)
elif p in aps:
# Exclude all annotation processors from JaCoco analysis
baseExcludes.append(p.name)
elif projsetting == 'exclude' or p.name in baseExcludes or p in aps:
excluded_projects.add(p.name)
excludes.extend((package + '.*' for package in p.defined_java_packages()))
elif projsetting == 'include':
includes.append(p.name + '.*')
includes.extend((package + '.*' for package in p.defined_java_packages()))
packagelist = getattr(p, 'jacocoExcludePackages', [])
assert isinstance(packagelist, list), f'jacocoExcludePackages must be a list, not a {type(packagelist)}'
for packagename in packagelist:
baseExcludes.append(packagename)
excludes.append(packagename + ".*")
if _jacoco_whitelisted_packages:
includes.extend((x + '.*' for x in _jacoco_whitelisted_packages))

def _filter(l):
# filter out specific classes which are already covered by a baseExclude package
# filter out specific classes which are already covered by a baseExclude
return [clazz for clazz in l if not any([clazz.startswith(package) for package in baseExcludes])]

excludes = []
for p in mx.projects():
if p.isJavaProject() and p.name not in baseExcludes and _jacoco_is_package_whitelisted(p.name):
if p.isJavaProject() and p.name not in excluded_projects and _jacoco_is_package_whitelisted(p.name):
excludes += _filter(
p.find_classes_with_annotations(None, _jacoco_excluded_annotations, includeInnerClasses=True,
includeGenSrc=True).keys())
excludes += _filter(p.find_classes_with_matching_source_line(None, lambda line: 'JaCoCo Exclude' in line,
includeInnerClasses=True,
includeGenSrc=True).keys())
excludes += [package + '.*' for package in baseExcludes]
return excludes, includes

def get_jacoco_dest_file():
Expand Down Expand Up @@ -959,13 +958,19 @@ def get_jacoco_agent_args(jacoco=None, agent_option_prefix=''):

agent_path = get_jacoco_agent_path(True)
agent_args = f'{agent_option_prefix}-javaagent:{agent_path}={agent_options}'
# Use java arg file to handle long command lines
with tempfile.NamedTemporaryFile(prefix="jacoco_agent", mode="w", delete=False) as args_file:
# Make sure to remove temporary file when program exits
atexit.register(os.remove, args_file.name)
args_file.write(agent_args)
args_file.flush()
return [f'@{args_file.name}']

# Use java args file to handle long command lines
mxbuild_dir = Path("mxbuild")
argsfile_dir = mxbuild_dir.joinpath("jacoco") if mxbuild_dir.exists() else Path.cwd()
now = datetime.now().isoformat().replace(':', '_')
argsfile_dir.mkdir(exist_ok=True)
argsfile = argsfile_dir.joinpath(f"agent-{now}.argsfile").absolute()
mx.log(f"JaCoCo agent config: '{argsfile}'")
if not mxbuild_dir.exists() and not mx.get_opts().verbose:
# Remove argsfile at exit if not in a mxbuild dir and not verbose
atexit.register(os.remove, str(argsfile))
argsfile.write_text(agent_args)
return [f'@{argsfile}']
return None

def jacocoreport(args, exec_files=None):
Expand Down

0 comments on commit 95a8845

Please sign in to comment.