Skip to content

Conversation

@mashraf-222
Copy link
Contributor

Fix API key detection and saving for PowerShell on Windows

Problem

On Windows PowerShell, codeflash init saved the API key to codeflash_env.bat using CMD syntax (set CODEFLASH_API_KEY=...). PowerShell doesn't execute .bat files automatically, so the key wasn't detected on subsequent runs. Users had to manually set $env:CODEFLASH_API_KEY each session.

Solution

  • Detect PowerShell vs CMD and handle each appropriately
  • Save to codeflash_env.ps1 with PowerShell syntax ($env:CODEFLASH_API_KEY = "...") when in PowerShell
  • Keep saving to codeflash_env.bat with CMD syntax when in CMD
  • Read from both .ps1 and .bat for compatibility
  • Show the correct reload command (. codeflash_env.ps1 for PowerShell, call codeflash_env.bat for CMD)

Changes

  • Added is_powershell() detection using PSModulePath, COMSPEC, and TERM_PROGRAM
  • Updated save_api_key_to_rc() to use the correct file format based on shell type
  • Updated read_api_key_from_shell_config() to check both PowerShell and CMD files
  • Updated reload command in cmd_init.py to use PowerShell syntax when appropriate
  • Added debug logging for troubleshooting

The API key is now saved and detected correctly in PowerShell sessions.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@KRRT7
Copy link
Contributor

KRRT7 commented Nov 24, 2025

have you tested this yourself locally on a windows machine?

@mashraf-222
Copy link
Contributor Author

mashraf-222 commented Nov 24, 2025

have you tested this yourself locally on a windows machine?

yes I did

CLI.API.key.demo.mp4

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Nov 24, 2025

⚡️ Codeflash found optimizations for this PR

📄 474% (4.74x) speedup for save_api_key_to_rc in codeflash/code_utils/shell_utils.py

⏱️ Runtime : 9.20 milliseconds 1.60 milliseconds (best of 1 runs)

A new Optimization Review has been created.

🔗 Review here

Static Badge

@KRRT7 KRRT7 enabled auto-merge (squash) November 26, 2025 05:10
@KRRT7 KRRT7 merged commit 63b6e77 into main Nov 26, 2025
20 of 22 checks passed
@KRRT7 KRRT7 deleted the fix/save/apikey/in/windows branch November 26, 2025 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants