Requester icon indicating copy to clipboard operation
Requester copied to clipboard

Env variables no longer loaded

Open alexvanacker opened this issue 7 years ago • 12 comments

OS:

 ~/  uname -srm
Linux 4.18.0-14-generic x86_64
 ~/  cat /etc/os-release
NAME="Ubuntu"
VERSION="18.10 (Cosmic Cuttlefish)"
ID=ubuntu
ID_LIKE=debian

Sublime text version:3.1.1, build 3176 Request version: v2.35.0

Ever since the update to 2.35, my env variables are not loaded. This is for both file and local env variables (i.e. even if the variables are in my .pyr file). Example:

###env
auth_token = 'some_token'
host = 'http://localhost:8080/'
default_headers = {'auth': auth_token, 'Content-Type': 'application/json'}
rest_path = host + 'some/path'
###env

get(
    rest_path + 'organization/40/folders',
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }
)

Error shown in console:

'NoneType' object has no attribute 'path'
error: PrepareRequest Error: name 'rest_path' is not defined
requests.get(
    rest_path + 'organization/40/folders',
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }…

alexvanacker avatar Feb 11 '19 10:02 alexvanacker

I have the same issue. I'm using version v2.36.0 and is not fixed yet. Any news about this?

cesuarez avatar Feb 18 '19 15:02 cesuarez

Hey all, sorry I didn't see this until now.

I made some big improvements to how env vars were parsed and the combined env string (from env block + env file) is computed in 2.35.0 and 2.36.0.

Improved merging of env block and env file. Concatenation order of strings parsed from env block and env file determined by relative order of env block and env_file line in requester file.

Also, request history now persists combined env string to history file, which means any request can be re-sent even if original requester file no longer present.

@alexvanacker I broke env var parsing in some cases in 2.35.0, but as of 2.36.0 everything should work. I improved testing of env block parsing in 2.36.0 to make sure this doesn't happen again.

For example, the following works for me:

###env
auth_token = 'some_token'
host = 'https://httpbin.org'
default_headers = {'auth': auth_token, 'Content-Type': 'application/json'}
rest_path = host + '/get'
###env

get(
    rest_path,
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }
)

It's basically identical to your example, but it points at httpbin instead of localhost. This works if it's run from an unsaved view or a file that's saved. It also works if it's run from the requester history, even if the original requester file has been deleted.

Please give this a try and let me know if it works.

@cesuarez Again, I don't think this is a problem in 2.36.0. Could you show me your requester file, the request you're trying to run, and the error that's printed to the ST console?

kylebebak avatar Mar 13 '19 17:03 kylebebak

No worries. I tested it with the latest version and it seems to work now :)

Great job!

alexvanacker avatar Mar 15 '19 09:03 alexvanacker

@alexvanacker I'm glad it's working =)

Also, if anyone wants to know how Requester parses env vars and persists them so they can be used from requests being resent from response tabs or from the requester history, read here.

kylebebak avatar Mar 15 '19 14:03 kylebebak

@kylebebak Thanks for answering. I'm still having this issue, even in the current 2.37 version. I tried reinstalling and it's still happening.

My code used to work before, even when I didn't change it. I even tried using alexvanacker code that you said it should work well (this is a saved file):

###env
auth_token = 'some_token'
host = 'https://httpbin.org'
default_headers = {'auth': auth_token, 'Content-Type': 'application/json'}
rest_path = host + '/get'
###env

get(
    rest_path,
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }
)

When I execute the request, it prompts me this error:

Screenshot from 2019-03-18 11-39-49

Followed by this one:

Screenshot from 2019-03-18 11-43-10

This is my console log output:

error: EnvBlock Error:
'NoneType' object has no attribute 'path'

Open the console to see the full environment string

EnvString:
``
auth_token = 'some_token'
host = 'https://httpbin.org'
default_headers = {'auth': auth_token, 'Content-Type': 'application/json'}
rest_path = host + '/get'
``
error: PrepareRequest Error: name 'rest_path' is not defined
requests.get(
    rest_path,
    headers = default_headers,
    json = {
        "name": "A",
        "parentId": 58
    }
)

cesuarez avatar Mar 18 '19 14:03 cesuarez

Hmmm, it looks like the exception is being raised in add_path.py, by this context manager:

import sys
import os


class add_path():
    def __init__(self, *parts):
        self.path = os.path.normpath(os.path.join(*parts))

    def __enter__(self):
        sys.path.insert(0, self.path)

    def __exit__(self, exc_type, exc_value, traceback):
        while True:
            try:
                sys.path.remove(self.path)
            except ValueError:
                break

My guess is self is None in either the enter or exit method of this context manager, but I can't reproduce the error on my platform (OSX 10.12, ST 3197).

What platform are you on? Sublime Text version? OS? This might be a bug in Python3.3 that I can work around, it would just be good to have more info.

kylebebak avatar Mar 18 '19 19:03 kylebebak

@cesuarez

I pushed a fix (2.37.1) that should hopefully deal with this problem. Can you check if this works now?

kylebebak avatar Mar 19 '19 07:03 kylebebak

@kylebebak it's working now! thanks for the fix

cesuarez avatar Mar 20 '19 17:03 cesuarez

Cool Cesar, thanks for bringing this to my attention. Just out of curiosity, if you don't mind, could you share you platform details?

This seems like it's a bug in Python 3.3.6.

kylebebak avatar Mar 21 '19 13:03 kylebebak

I'm using Python 3.3.6 indeed. My OS is Ubuntu 18.04

cesuarez avatar Mar 21 '19 17:03 cesuarez

Thanks!

kylebebak avatar Mar 21 '19 19:03 kylebebak

Thanks for fixing this!

I was seeing this on MacOS with Requester v2.38.0, Sublime Text build 3207, Python 3.3.6. All I had to do was restart Sublime and it started working again.

YayC avatar Apr 12 '19 16:04 YayC