Skip to content

path normalization and tempdir fixes for windows #363

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

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
586f5af
path normalization and tempdir fixes for windows
KRRT7 Jun 22, 2025
0b1d5e0
restore original test
KRRT7 Jun 22, 2025
b0dd63b
update code context extractor
KRRT7 Jun 23, 2025
1330fbb
Merge branch 'main' into part-1-windows-fixes
KRRT7 Jun 23, 2025
c9a7ad5
windows too
KRRT7 Jun 23, 2025
a6c631a
Update end-to-end-test-futurehouse.yaml
KRRT7 Jun 23, 2025
096564b
Update end_to_end_test_utilities.py
KRRT7 Jun 23, 2025
0090bea
Update end_to_end_test_utilities.py
KRRT7 Jun 23, 2025
5b4f092
Merge branch 'main' of https://github.com/codeflash-ai/codeflash into…
KRRT7 Jul 3, 2025
5da9df8
Merge branch 'main' of https://github.com/codeflash-ai/codeflash into…
KRRT7 Jul 3, 2025
d3d7286
Update e2e-futurehouse-structure.yaml
KRRT7 Jul 3, 2025
46878c5
test
KRRT7 Jul 3, 2025
401c163
set encoding
KRRT7 Jul 3, 2025
620374f
windows test
KRRT7 Jul 3, 2025
adf4cfc
Update e2e-futurehouse-structure.yaml
KRRT7 Jul 3, 2025
07365bc
Update pyproject.toml
KRRT7 Jul 3, 2025
c0ae41f
Update pyproject.toml
KRRT7 Jul 3, 2025
ed32b36
Update env_utils.py
KRRT7 Jul 3, 2025
4b6f8b0
Update unit-tests.yaml
KRRT7 Jul 3, 2025
274f421
Update unit-tests.yaml
KRRT7 Jul 3, 2025
de84558
Merge branch 'main' of https://github.com/codeflash-ai/codeflash into…
KRRT7 Jul 3, 2025
aa09a1e
Merge branch 'main' into part-1-windows-fixes
KRRT7 Jul 3, 2025
eace1a9
Update unit-tests.yaml
KRRT7 Jul 4, 2025
8e5d03c
Update unit-tests.yaml
KRRT7 Jul 4, 2025
c5d73f8
Merge branch 'main' of https://github.com/codeflash-ai/codeflash into…
KRRT7 Jul 18, 2025
f7d8d6a
conn & windows
KRRT7 Jul 19, 2025
02dc316
fix E2E workflow
KRRT7 Jul 19, 2025
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
14 changes: 11 additions & 3 deletions .github/workflows/e2e-futurehouse-structure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ jobs:
# Dynamically determine if environment is needed only when workflow files change and contributor is external
environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7')) && 'external-trusted-contributors' || '' }}

runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
env:
CODEFLASH_AIS_SERVER: prod
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
Expand All @@ -22,6 +25,7 @@ jobs:
RETRY_DELAY: 5
EXPECTED_IMPROVEMENT_PCT: 10
CODEFLASH_END_TO_END: 1
PYTHONIOENCODING: "utf8"
steps:
- name: 🛎️ Checkout
uses: actions/checkout@v4
Expand All @@ -31,6 +35,7 @@ jobs:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Validate PR
shell: bash
run: |
# Check for any workflow changes
if git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}" | grep -q "^.github/workflows/"; then
Expand All @@ -56,13 +61,16 @@ jobs:
- name: Set up Python 3.11 for CLI
uses: astral-sh/setup-uv@v5
with:
python-version: 3.11.6
python-version: 3.11

- name: Install dependencies (CLI)
run: |
uv sync
uv add ruff

- name: Run Codeflash to optimize code
id: optimize_code
env:
PYTHONUTF8: 1
run: |
uv run python tests/scripts/end_to_end_test_futurehouse.py
uv run tests/scripts/end_to_end_test_futurehouse.py
26 changes: 23 additions & 3 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ jobs:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest]
continue-on-error: true
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -24,10 +25,29 @@ jobs:
uses: astral-sh/setup-uv@v5
with:
python-version: ${{ matrix.python-version }}
version: "0.5.30"

- name: install dependencies
run: uv sync

- name: Unit tests
run: uv run pytest tests/ --benchmark-skip -m "not ci_skip"
run: uv run pytest tests/ --benchmark-skip -m "not ci_skip"

# unit-tests-windows:
# runs-on: windows-latest
# continue-on-error: true
# steps:
# - uses: actions/checkout@v4
# with:
# fetch-depth: 0
# token: ${{ secrets.GITHUB_TOKEN }}

# - name: Install uv
# uses: astral-sh/setup-uv@v5
# with:
# python-version: "3.11"

# - name: install dependencies
# run: uv sync

# - name: Unit tests
# run: uv run pytest tests/ --benchmark-skip -m "not ci_skip"
8 changes: 4 additions & 4 deletions codeflash/benchmarking/replay_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def create_trace_replay_test_code(
benchmark_function_name=benchmark_function_name,
orig_function_name=function_name,
function_name=alias,
file_path=file_path,
file_path=Path(file_path).as_posix(),
max_run_count=max_run_count,
)
else:
Expand All @@ -176,7 +176,7 @@ def create_trace_replay_test_code(
test_body = test_class_method_body.format(
benchmark_function_name=benchmark_function_name,
orig_function_name=function_name,
file_path=file_path,
file_path=Path(file_path).as_posix(),
class_name_alias=class_name_alias,
class_name=class_name,
method_name=method_name,
Expand All @@ -187,7 +187,7 @@ def create_trace_replay_test_code(
test_body = test_static_method_body.format(
benchmark_function_name=benchmark_function_name,
orig_function_name=function_name,
file_path=file_path,
file_path=Path(file_path).as_posix(),
class_name_alias=class_name_alias,
class_name=class_name,
method_name=method_name,
Expand All @@ -198,7 +198,7 @@ def create_trace_replay_test_code(
test_body = test_method_body.format(
benchmark_function_name=benchmark_function_name,
orig_function_name=function_name,
file_path=file_path,
file_path=Path(file_path).as_posix(),
class_name_alias=class_name_alias,
class_name=class_name,
method_name=method_name,
Expand Down
3 changes: 2 additions & 1 deletion codeflash/code_utils/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ def get_run_tmp_file(file_path: Path) -> Path:


def path_belongs_to_site_packages(file_path: Path) -> bool:
file_path_resolved = file_path.resolve()
site_packages = [Path(p) for p in site.getsitepackages()]
return any(file_path.resolve().is_relative_to(site_package_path) for site_package_path in site_packages)
return any(file_path_resolved.is_relative_to(site_package_path) for site_package_path in site_packages)


def is_class_defined_in_file(class_name: str, file_path: Path) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion codeflash/code_utils/coverage_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def generate_candidates(source_code_path: Path) -> list[str]:
current_path = source_code_path.parent

while current_path != current_path.parent:
candidate_path = str(Path(current_path.name) / candidates[-1])
candidate_path = (Path(current_path.name) / candidates[-1]).as_posix()
candidates.append(candidate_path)
current_path = current_path.parent

Expand Down
9 changes: 4 additions & 5 deletions codeflash/code_utils/env_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
if formatter_cmds[0] == "disabled":
return return_code
tmp_code = """print("hello world")"""
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", suffix=".py") as f:
f.write(tmp_code)
f.flush()
tmp_file = Path(f.name)
with tempfile.TemporaryDirectory() as tmpdir:
tmp_file = Path(tmpdir) / "test_codeflash_formatter.py"
tmp_file.write_text(tmp_code, encoding="utf-8")
try:
format_code(formatter_cmds, tmp_file, print_status=False, exit_on_failure=exit_on_failure)
except Exception:
exit_with_message(
"⚠️ Codeflash requires a code formatter to be installed in your environment, but none was found. Please install a supported formatter, verify the formatter-cmds in your codeflash pyproject.toml config and try again.",
error_on_exit=True,
)
return return_code
return return_code


@lru_cache(maxsize=1)
Expand Down
4 changes: 3 additions & 1 deletion codeflash/code_utils/instrument_existing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ def visit_FunctionDef(self, node: ast.FunctionDef, test_class_name: str | None =
args=[
ast.JoinedStr(
values=[
ast.Constant(value=f"{get_run_tmp_file(Path('test_return_values_'))}"),
ast.Constant(
value=f"{get_run_tmp_file(Path('test_return_values_')).as_posix()}"
),
ast.FormattedValue(
value=ast.Name(id="codeflash_iteration", ctx=ast.Load()),
conversion=-1,
Expand Down
2 changes: 1 addition & 1 deletion codeflash/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def markdown(self) -> str:
"""Returns the markdown representation of the code, including the file path where possible."""
return "\n".join(
[
f"```python{':' + str(code_string.file_path) if code_string.file_path else ''}\n{code_string.code.strip()}\n```"
f"```python{':' + code_string.file_path.as_posix() if code_string.file_path else ''}\n{code_string.code.strip()}\n```"
for code_string in self.code_strings
]
)
Expand Down
4 changes: 2 additions & 2 deletions codeflash/result/create_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def existing_tests_source_for(
if greater:
rows.append(
[
f"`{print_filename}::{qualified_name}`",
f"`{print_filename.as_posix()}::{qualified_name}`",
f"{print_original_runtime}",
f"{print_optimized_runtime}",
f"⚠️{perf_gain}%",
Expand All @@ -110,7 +110,7 @@ def existing_tests_source_for(
else:
rows.append(
[
f"`{print_filename}::{qualified_name}`",
f"`{print_filename.as_posix()}::{qualified_name}`",
f"{print_original_runtime}",
f"{print_optimized_runtime}",
f"✅{perf_gain}%",
Expand Down
6 changes: 3 additions & 3 deletions codeflash/verification/instrument_codeflash_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def instrument_codeflash_capture(
modified_code = add_codeflash_capture_to_init(
target_classes={class_parent.name},
fto_name=function_to_optimize.function_name,
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
code=original_code,
tests_root=tests_root,
is_fto=True,
Expand All @@ -46,7 +46,7 @@ def instrument_codeflash_capture(
modified_code = add_codeflash_capture_to_init(
target_classes=helper_classes,
fto_name=function_to_optimize.function_name,
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
code=original_code,
tests_root=tests_root,
is_fto=False,
Expand Down Expand Up @@ -124,7 +124,7 @@ def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
keywords=[
ast.keyword(arg="function_name", value=ast.Constant(value=f"{node.name}.__init__")),
ast.keyword(arg="tmp_dir_path", value=ast.Constant(value=self.tmp_dir_path)),
ast.keyword(arg="tests_root", value=ast.Constant(value=str(self.tests_root))),
ast.keyword(arg="tests_root", value=ast.Constant(value=self.tests_root.as_posix())),
ast.keyword(arg="is_fto", value=ast.Constant(value=self.is_fto)),
],
)
Expand Down
6 changes: 4 additions & 2 deletions tests/scripts/end_to_end_test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def run_codeflash_command(

output = []
for line in process.stdout:
line = line.strip().encode("utf-8").decode("utf-8")
logging.info(line.strip())
output.append(line)

Expand Down Expand Up @@ -122,7 +123,7 @@ def build_command(
) -> list[str]:
python_path = "../../../codeflash/main.py" if "code_directories" in str(cwd) else "../codeflash/main.py"

base_command = ["python", python_path, "--file", config.file_path, "--no-pr"]
base_command = ["uv", "run", "--no-project", python_path, "--file", config.file_path, "--no-pr"]

if config.function_name:
base_command.extend(["--function", config.function_name])
Expand Down Expand Up @@ -187,13 +188,14 @@ def validate_stdout_in_candidate(stdout: str, expected_in_stdout: list[str]) ->
def run_trace_test(cwd: pathlib.Path, config: TestConfig, expected_improvement_pct: int) -> bool:
test_root = cwd / "tests" / (config.test_framework or "")
clear_directory(test_root)
command = ["python", "-m", "codeflash.main", "optimize", "workload.py"]
command = ["uv", "run", "--no-project", "-m", "codeflash.main", "optimize", "workload.py"]
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, cwd=str(cwd), env=os.environ.copy()
)

output = []
for line in process.stdout:
line = line.strip().encode("utf-8").decode("utf-8")
logging.info(line.strip())
output.append(line)

Expand Down
Loading
Loading