Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.63k stars 2.84k forks source link

Inject environment variables for subprocess #38452

Open morganics opened 1 week ago

morganics commented 1 week ago

Is your feature request related to a problem? Please describe.

Using os.environ can cause issues in azure_cli.py, e.g. cannot find the az command even when az is on the path.

Describe the solution you'd like Inject os environment variables in to the identity module, so that we're not dependent on the global os.environ variables.

e.g. line 223 on azure_cli.py

        working_directory = get_safe_working_dir()

        kwargs: Dict[str, Any] = {
            "stderr": subprocess.PIPE,
            "stdin": subprocess.DEVNULL,
            "cwd": working_directory,
            "universal_newlines": True,
            "timeout": timeout,
            "env": dict(self._environ, AZURE_CORE_NO_COLOR="true"), # rather than using os.environ.
        }
        return subprocess.check_output(args, **kwargs)

Describe alternatives you've considered Patching os.environ, which isn't great.

Additional context Messy PATHs can stop the az command being found, despite shutil locating the appropriate path.

xiangyan99 commented 1 week ago

Thanks for the feedback, we’ll investigate asap.

pvaneck commented 6 hours ago

Interesting that shutil.which can find az, but the subprocess is unable to resolve it even when passed in os.environ. From what I understand, shutil.which also retrieves the PATH environment variable from os.environ, which is the environment that should be passed to the subprocess. Something strange must be going on with the subprocess environment. I'm not sure if this stems from our usage of cmd \c and /bin/sh -c, however I can't seem to create a reproduction of the case where shutil can find az, but the subprocess cannot.

Are you encountering the issue on Windows or a Unix-based OS?

In any case, I am testing out just using the full path of the executable found from shutil.which, and just calling that directly. This approach should hopefully resolve the issue.