bruno: Regression: Environment Variables Values Not Working Since v1.18.0

I have checked the following:

  • I use the newest version of bruno.
  • I’ve searched existing issues and found nothing related to my issue.

Describe the bug

Since updating to version 1.18.0, environment variables used within the values of other environment variables no longer work as expected. This feature was available and worked correctly since version 1.17.0.

Setup

Let’s take a GET request at https://catfact.ninja/fact that retrieves random cat facts via text message every day : image You can see that the URL is a variable, which comes from the environment variables as shown here : image As you can see, url is also made up of another environment variable Cat.

.bru file to reproduce the bug

environements/Local.bru

vars {
  Cat: https://catfact.ninja
  url: {{Cat}}/fact
}

cat.bru

meta {
  name: cat
  type: http
  seq: 3
}

get {
  url: {{url}}
  body: none
  auth: none
}

Screenshots/Live demo link

Versions before v1.17.0

I would get an error : image image

Version v1.17.0

I would get a 200 OK with the wanted response : image image

Version 1.18.0

But since the 1.18.0 update, I get the same error as I did before version 1.17.0: image image

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 15

Most upvoted comments

@technodrome you could add your script in the collection pre-request script and use the token in your requests

i have attached a sample bruno collection json this is in v1.18.0

token_test.json

Does not work with 1.20.1. On top of that, declaring something as collection var just to make it work (if it actually did) is not the right approach IMO. Even when I move the script to collection pre-request script section, I see my token using getVar in the dev console but Collection variables do not show this programmatically defined variable and it is also unavailable (red font) in the auth bearer token input field using {{auth_token}}. Broken.

I’ll have to stick with Insomnia which even has nice built-in dialogue for these response variables. I do not mind defining things programmatically but it seems completely broken to me.

Hey @Peperomain

sure, here’s a simple script that’l create a equivalent collection var with the interpolated env var value.

const patternRegex = /\{\{([^}]+)\}\}/g;

const vars = bru.envVariables;

console.log("before:");
console.log(vars);
console.log("-----------------");

const interpolate = (str) => {
  return str && str.replaceAll(patternRegex, (match, placeholder) => {
    const replacement = vars[placeholder];
    return replacement !== undefined
      ? patternRegex.test(replacement)
        ? interpolate(replacement)
        : replacement
      : match;
  });
};

let interpolatedVars = {};
Object.entries(vars).map(([key, value]) => {
  interpolatedVars[key] = interpolate(value);
  bru.setVar(key, interpolate(value));
});

console.log("after:");
console.log(interpolatedVars);
console.log("-----------------");
vars {
  host: http://example.com
  path: foo
  urlWithPath: {{url}}{{path}}/bar
  url: https://example.com/
  bigUrl: {{urlWithPath}}/baz
}

You can add this in the collection level pre-request script. And since the collection variables hold higher precedence over env vars, interpolated values will be used.

https://github.com/usebruno/bruno/assets/159901171/a781c6ac-1b81-413d-980d-ddb38ab5001b

this issue is not fixed in v20. if you execute the collection i posted, the problem still exists. i’ve just created a new bug issue.

@Peperomain

The fix in v1.17.0 was incidental and not a proper one.

For example: Screenshot 2024-05-23 at 6:28:31 PM Screenshot 2024-05-23 at 6:28:44 PM

The above scenario doesn’t work in v1.17.0 either.

To properly solve this, multi-level interpolation needs to be done.

I do plan on working on this if time permits and based on my other task priorities.

This issue can be overcome through scripting. Do let me know if you need any help with it.