Pysonar-scanner error on windows

I am currently trying to use the latest pysonar-scanner module (0.2.0.520) under Windows and Linux to scan a Poetry project. The scanner works fine under Linux but under windows the almost same scanner call leads to an Error.
I already verified that a bare sonar-scanner call works flawless on my Windows and Linux setup.

I am performing the following command under Linux and Windows in a basic manner:
pysonar-scanner -Dsonar.projectBaseDir=C:/MyPathToHomeDir -Dtoml.path=C:/MyPathToHomeDir/pyproject.toml -X

Under windows the following stack trace is printed:

Error during SonarScanner execution: [WinError 2] The system cannot find the file specified
Traceback (most recent call last):
  File "C:\b01\_w\_temp\venv\Lib\site-packages\pysonar_scanner\__main__.py", line 31, in scan
    env.scan()
  File "C:\b01\_w\_temp\venv\Lib\site-packages\pysonar_scanner\environment.py", line 49, in scan
    self.scanner().scan()
  File "C:\b01\_w\_temp\venv\Lib\site-packages\pysonar_scanner\scanner.py", line 38, in scan
    process = self.execute_command()
              ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\b01\_w\_temp\venv\Lib\site-packages\pysonar_scanner\scanner.py", line 54, in execute_command
    return Popen(cmd, stdout=PIPE, stderr=PIPE)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Python311\Lib\subprocess.py", line 1538, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified

However if i “patch” the package itself by manipulating the scanner.py module inside the pip package, I can get it to work:
If in line 54 inside the execute_command method the block “shell=True” is added, the python module works flawlessly under windows and the error is gone.

The patched method looks like this:

    def execute_command(self) -> Popen:
        cmd = self.compute_command()
        return Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)

Will this behavior be patched in an upcoming release, would it help if i make a pull request with this patch? Maybe there is a better fix for this since the added parameter may also induce a greater security risk.

Hello @tobpio,

Thanks for the bug report and for the detailed analysis you did.
Indeed, there is an issue when the pysonar-scanner is running on Windows. We are aware of this issue, and what you propose could be a good solution.

However, we are currently working on a new version of the python-scanner which will use a new way of interacting with the sonar scanner from a Python environment (a new bootstrapping mechanism). Thus, we don’t intend to make a new release of this version of the Python scanner (without the bootstrapping mechanism). This said, using the new bootstrapping mechanism is ongoing work, and a new release should be done soon (:crossed_fingers:), which should work on Windows without any issues.

You can have a look at our progress here.

Thanks again for the post,

2 Likes

Thank you for the provided link. Good to know the issue will be fixed. Especially Jira makes me optimistic that the update will work :slight_smile: .

1 Like