Fix Windows FileNotFoundError when invoking sf CLI#121
Merged
Conversation
On Windows, subprocess.run(["sf", ...]) raises FileNotFoundError because sf is a .cmd wrapper, not a plain executable. Passing shell=True (only on win32) lets cmd.exe resolve the .cmd extension. No-op on macOS/Linux.
|
Thanks for the contribution! Before we can merge this, we need @ro-mo-do to sign the Salesforce Inc. Contributor License Agreement. |
rkang-sf
reviewed
Jul 2, 2026
| text=True, | ||
| check=True, | ||
| timeout=30, | ||
| shell=sys.platform == "win32", |
Collaborator
There was a problem hiding this comment.
Would there be a concern here that it might introduce a command injection attack surface with shell=true?
Alternatively, consider:
import shutil
sf_path = shutil.which("sf") or shutil.which("sf.cmd") # resolves the .cmd on Windows
if sf_path is None:
raise RuntimeError("The 'sf' command was not found. Install Salesforce CLI: ...")
# then args[0] = sf_path, keep shell=False everywhere
Addresses reviewer concern about command injection risk with shell=True. shutil.which resolves the sf executable (falling back to sf.cmd on Windows), keeping shell=False everywhere while still working on Windows.
rkang-sf
approved these changes
Jul 7, 2026
…lled The previous commit replaced shell=True + "sf" string with shutil.which() to resolve the sf executable path before calling subprocess.run. This moved the "sf not found" check earlier — before _run_sf_command is even defined — meaning tests that only mocked subprocess.run were now hitting the shutil.which() call for real. On CI (Linux, no SF CLI), which() returns None and raises immediately, causing all 11 SFCLITokenProvider and SFCLIDataCloudReader tests to fail with "sf command was not found" rather than exercising the error condition they were testing. Fix: mock shutil.which alongside subprocess.run in each affected test, using the same unittest.mock.patch pattern already used throughout the test suite. return_value="sf" keeps the resolved path consistent with the hardcoded "sf" string the command-list assertions in test_sf_cli.py expect. The "sf not found" tests are updated to patch shutil.which returning None instead of patching subprocess.run with FileNotFoundError, since that FileNotFoundError path was removed from the production code. No new test files, no new imports, no new patterns — all changes are confined to existing test classes and follow the same nesting style used in TestCredentialsTokenProvider and the rest of the suite.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Windows,
subprocess.run(["sf", ...])raisesFileNotFoundErrorbecausesfis installed as a.cmdwrapper, not a plain executable. Python's subprocess can only resolve.cmdfiles whenshell=Trueis passed. The existing error handler catches this and tells users to install the CLI — but the CLI is installed and working; subprocess just can't find it.Fix
Pass
shell=sys.platform == "win32"tosubprocess.runin_run_sf_command. No-op on macOS/Linux.