chore: clean up premature download logic, add MVN_CENTRAL_TODO markers

Remove download_jar_to_cache() and related constants (MAVEN_CENTRAL_RUNTIME_URL,
CODEFLASH_JAR_CACHE_DIR) — the JAR isn't published to Maven Central yet so this
code always fails silently. The working path today is the bundled JAR in resources/.

Simplify _find_runtime_jar() from 5 steps to 3: ~/.m2 → bundled → dev build.

Add searchable MVN_CENTRAL_TODO markers at all 4 locations that need to change
once the JAR is published to Maven Central:
  1. build_tools.py — uncomment resolve_from_maven_central()
  2. test_runner.py _find_runtime_jar() — adjust resolution chain
  3. test_runner.py _ensure_codeflash_runtime() — uncomment Maven Central call
  4. pyproject.toml — exclude runtime JAR from wheel (~15MB savings)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Mohamed Ashraf 2026-03-06 00:58:09 +00:00
parent 236da846ec
commit b6acd6b7ce
3 changed files with 23 additions and 103 deletions

View file

@ -21,19 +21,6 @@ logger = logging.getLogger(__name__)
CODEFLASH_RUNTIME_VERSION = "1.0.0"
CODEFLASH_RUNTIME_JAR_NAME = f"codeflash-runtime-{CODEFLASH_RUNTIME_VERSION}.jar"
# Maven Central URL for downloading the runtime JAR on first run.
# This is the standard Maven Central repository path pattern: /groupId/artifactId/version/artifactId-version.jar
# where groupId dots are replaced with slashes (com.codeflash → com/codeflash).
MAVEN_CENTRAL_RUNTIME_URL = (
f"https://repo1.maven.org/maven2/com/codeflash/codeflash-runtime"
f"/{CODEFLASH_RUNTIME_VERSION}/{CODEFLASH_RUNTIME_JAR_NAME}"
)
# Local cache directory for JARs downloaded on first run.
# This avoids re-downloading on every optimization. Located in ~/.codeflash/java/
# so it persists across projects but doesn't pollute the user's Maven repository.
CODEFLASH_JAR_CACHE_DIR = Path.home() / ".codeflash" / "java"
JACOCO_PLUGIN_VERSION = "0.8.13"
JACOCO_AGENT_JAR = f"org.jacoco.agent-{JACOCO_PLUGIN_VERSION}-runtime.jar"
JACOCO_CLI_JAR = f"org.jacoco.cli-{JACOCO_PLUGIN_VERSION}-nodeps.jar"
@ -101,62 +88,17 @@ def find_jacoco_cli_jar() -> Path | None:
return None
def download_jar_to_cache(url: str, jar_name: str) -> Path | None:
"""Download a JAR from a URL to the local cache directory (~/.codeflash/java/).
This is used for "download on first run" when the JAR isn't bundled in the
Python package, we fetch it from Maven Central and cache it locally so subsequent
runs don't need internet access.
Returns the path to the cached JAR, or None if download failed.
"""
import urllib.error
import urllib.request
from urllib.parse import urlparse
# S310: validate URL scheme to prevent file:// or custom scheme abuse
parsed = urlparse(url)
if parsed.scheme not in ("https", "http"):
logger.warning("Refusing to download from non-HTTP URL: %s", url)
return None
cache_dir = CODEFLASH_JAR_CACHE_DIR
cached_jar = cache_dir / jar_name
if cached_jar.exists():
return cached_jar
try:
cache_dir.mkdir(parents=True, exist_ok=True)
logger.info("Downloading %s to %s", jar_name, cache_dir)
urllib.request.urlretrieve(url, cached_jar) # noqa: S310
if cached_jar.exists() and cached_jar.stat().st_size > 0:
logger.info("Successfully downloaded %s (%d bytes)", jar_name, cached_jar.stat().st_size)
return cached_jar
logger.warning("Downloaded file is empty or missing: %s", cached_jar)
cached_jar.unlink(missing_ok=True)
return None
except urllib.error.URLError as e:
logger.warning("Failed to download %s: %s", url, e)
cached_jar.unlink(missing_ok=True)
return None
except Exception as e:
logger.warning("Unexpected error downloading %s: %s", url, e)
cached_jar.unlink(missing_ok=True)
return None
# --------------------------------------------------------------------------
# Maven Central resolution (ready to use once codeflash-runtime is published)
# MVN_CENTRAL_TODO: Uncomment once codeflash-runtime is published to Maven Central.
# Steps:
# 1. Uncomment resolve_from_maven_central() below
# 2. Uncomment the call in test_runner.py _ensure_codeflash_runtime()
# 3. Exclude the JAR from the PyPI wheel (see MVN_CENTRAL_TODO in pyproject.toml)
# 4. The install:install-file fallback in _ensure_codeflash_runtime() can be removed
# 5. The bundled JAR in resources/ can be removed from the repo
#
# Once codeflash-runtime is published to Maven Central, uncomment the function
# below and use it in _ensure_codeflash_runtime() to let Maven resolve the JAR
# automatically — no manual install:install-file needed.
#
# When this is enabled:
# 1. The download_jar_to_cache() approach (Option B) becomes deprecated
# because Maven handles downloading and caching in ~/.m2 natively.
# 2. The install_codeflash_runtime() call can be skipped when this succeeds.
# 3. The pom.xml <dependency> addition is still needed (Maven Compiler needs it).
# Alternative: Instead of Maven resolution, you can download the JAR from GitHub
# Releases using a download_jar_to_cache() helper — see git history for the
# implementation that was removed in this cleanup.
#
# def resolve_from_maven_central(maven_root: Path) -> bool:
# """Ask Maven to resolve codeflash-runtime from Maven Central.

View file

@ -24,13 +24,10 @@ from typing import Any
from codeflash.code_utils.code_utils import get_run_tmp_file
from codeflash.languages.base import TestResult
from codeflash.languages.java.build_tools import (
CODEFLASH_JAR_CACHE_DIR,
CODEFLASH_RUNTIME_JAR_NAME,
CODEFLASH_RUNTIME_VERSION,
MAVEN_CENTRAL_RUNTIME_URL,
add_codeflash_dependency_to_pom,
backup_pom,
download_jar_to_cache,
find_jacoco_agent_jar,
find_jacoco_cli_jar,
find_maven_executable,
@ -232,18 +229,16 @@ def generate_jacoco_report(exec_file: Path, classfiles_dir: Path, sourcefiles_di
return False
# MVN_CENTRAL_TODO: Once codeflash-runtime is published to Maven Central, Maven will
# resolve the JAR automatically via the pom.xml <dependency>. At that point step 2
# (bundled resources/) can be removed and the JAR excluded from the PyPI wheel.
def _find_runtime_jar() -> Path | None:
"""Find the codeflash-runtime JAR file.
Resolution order:
1. Local Maven cache (~/.m2) fastest, already resolved by Maven or previous install
2. Bundled in Python package (resources/) available after pip install codeflash
3. Local download cache (~/.codeflash/java/) downloaded on a previous first run
4. Download from Maven Central first-run download, cached for next time
5. Development build directory only when running from source checkout
Once codeflash-runtime is published to Maven Central, step 4 will succeed
and steps 2-3 can be removed (Maven Central becomes the primary source).
3. Development build directory only when running from source checkout
"""
# 1. Check local Maven repository (fastest — already installed by Maven or install:install-file)
m2_jar = (
@ -264,20 +259,7 @@ def _find_runtime_jar() -> Path | None:
if resources_jar.exists():
return resources_jar
# 3. Check local download cache (downloaded on a previous first run)
cached_jar = CODEFLASH_JAR_CACHE_DIR / CODEFLASH_RUNTIME_JAR_NAME
if cached_jar.exists():
return cached_jar
# 4. Download from Maven Central to local cache (~/.codeflash/java/).
# This enables "pip install codeflash" without bundling the 15MB JAR — the JAR
# is fetched on first Java optimization and cached locally for subsequent runs.
# Will only succeed once codeflash-runtime is published to Maven Central.
downloaded = download_jar_to_cache(MAVEN_CENTRAL_RUNTIME_URL, CODEFLASH_RUNTIME_JAR_NAME)
if downloaded is not None:
return downloaded
# 5. Check development build directory (only when running from source checkout)
# 3. Check development build directory (only when running from source checkout)
dev_jar = (
Path(__file__).parent.parent.parent.parent / "codeflash-java-runtime" / "target" / CODEFLASH_RUNTIME_JAR_NAME
)
@ -313,25 +295,17 @@ def _ensure_codeflash_runtime(maven_root: Path, test_module: str | None) -> bool
# Install to local Maven repo if not already there.
#
# --------------------------------------------------------------------------
# Maven Central resolution (ready to use once codeflash-runtime is published)
#
# Once published, uncomment the block below and remove the install:install-file
# fallback. Maven will download the JAR from Central automatically when it sees
# the <dependency> in pom.xml, so we only need to verify it's resolvable:
# MVN_CENTRAL_TODO: Once codeflash-runtime is published to Maven Central, uncomment
# the block below and remove the install:install-file fallback beneath it. Maven will
# download the JAR from Central automatically when it sees the <dependency> in pom.xml.
#
# from codeflash.languages.java.build_tools import resolve_from_maven_central
# if resolve_from_maven_central(maven_root):
# # Maven resolved the JAR from Central to ~/.m2 — no manual install needed.
# # The download_jar_to_cache() path in _find_runtime_jar() also becomes
# # deprecated at this point since Maven handles caching natively in ~/.m2.
# pass
# pass # Maven resolved to ~/.m2 — no manual install needed
# else:
# # Fallback: manually install the JAR we found (bundled/downloaded/dev build)
# if not install_codeflash_runtime(maven_root, runtime_jar):
# logger.error("Failed to install codeflash-runtime to local Maven repository")
# return False
# --------------------------------------------------------------------------
m2_jar = (
Path.home()
/ ".m2"

View file

@ -196,6 +196,10 @@ exclude = [
"Thumbs.db",
"venv",
"env",
# MVN_CENTRAL_TODO: Once codeflash-runtime is available on Maven Central (or GitHub
# Releases), exclude it from the wheel to save ~15MB. Users will get it via Maven
# resolution or download-on-first-run. Uncomment the line below:
# "codeflash/languages/java/resources/codeflash-runtime-*.jar",
]
[tool.mypy]