Duroktar / Wolf

Wolf is a VsCode extension that enables live inspection of Python code in the editor.
Other
128 stars 7 forks source link

wolf runs into error with env vars #52

Closed Almenon closed 3 years ago

Almenon commented 3 years ago

Python code:

import os

test=os.environ['USERNAME'] #?
path=os.environ['Path'] # also fails
print(path)

Expected result: logs 'almenon' Actual result: image

In AREPL I avoid this problem by using python-shell and passing the env vars in like so:

Constructor of arepl-backend:

        if (!options.env) options.env = {}
        if (process.platform == "darwin") {
            // needed for Mac to prevent ENOENT
            options.env.PATH = ["/usr/local/bin", process.env.PATH].join(":")
        }
        else if (process.platform == "win32") {
            // needed for windows for encoding to match what it would be in terminal
            // https://docs.python.org/3/library/sys.html#sys.stdin
            options.env.PYTHONIOENCODING = "utf8"
        }

In start() method in backend: https://github.com/Almenon/AREPL-backend/blob/209eb5b8ae8cda1677f925749a10cd263f6d9860/index.ts#L171

this.pyshell = new PythonShell('arepl_python_evaluator.py', this.options)

In arepl-vscode I borrowed some code from the python extension, adapted it a bit and hardcoded it into a folder called "env": https://github.com/Almenon/AREPL-vscode/tree/7ed81bd83b8b30b1d18366c7f1254b13b6dc6ba6/src/env

Then in https://github.com/Almenon/AREPL-vscode/blob/d8c9f32d573213163f27e903e31df197be1e8248/src%2FPreviewManager.ts#L5 I have:

    async loadAndWatchEnvVars(){
        const platformService = new PlatformService()
        const envVarsService = new EnvironmentVariablesService(new PathUtils(platformService.isWindows))
        const workspaceService = new WorkspaceService()
        const e = new EnvironmentVariablesProvider(envVarsService,
            this.subscriptions,
            platformService,
            workspaceService,
            process)
        return e.getEnvironmentVariables(areplUtils.getEnvFilePath(), vscodeUtils.getCurrentWorkspaceFolderUri())
    }
        // basically all this does is load a file.. why does it need to be async *sob*
        const env = await this.loadAndWatchEnvVars()

        this.PythonEvaluator = new PythonEvaluator({
            pythonOptions,
            pythonPath,
            env,
        })

The python extension code has the benefit of also reading from the workspace .env file. The downside is that it is very large and complex, probably overengineered IMO.

Almenon commented 3 years ago

I took a look in the code in #54 - did this fix the problem when you tested it? I think you would still have the same issue - the environment vars you pass in just has the PATH in darwin OS's but it should have the USERNAME and other env vars too.

I'm guessing you need to pass in process.env.

Duroktar commented 3 years ago

Hehe, I had to hotfix it afterwards: https://github.com/Duroktar/Wolf/commit/d2c59d9c6b9d47e4c348414b63bf2695a37e5075

And you were 100% right :D I really don't know how it got passed the unit tests to begin with (local pytest cache maybe?), merging in process.env was the magic bit.

PS: Really good to hear from you again old friend :)

Almenon commented 3 years ago

Good to see you again too :)