bruno icon indicating copy to clipboard operation
bruno copied to clipboard

Setting the env variable through script cause error on setting manually error

Open developer-sunnywu opened this issue 1 year ago • 10 comments

I have checked the following:

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

Describe the bug

Reproduce process

  1. Running a request and there is pre script to setup some env variables.
  2. Edit the env variable (same or different) manually
  3. Click save
  4. Show error "An error occurred while saving the changes"

Resolution for now

  1. Need to restart the bruno everytime to edit the env variable after every request.

.bru file to reproduce the bug

No response

developer-sunnywu avatar Oct 30 '24 07:10 developer-sunnywu

+1

I experienced this, too and was just about to create an issue.

In my case it is a post script and the error occurs on every change to the environment.

JoergJans avatar Nov 03 '24 14:11 JoergJans

Hi, @developer-sunnywu , I was able to reproduce this issue and we're working on this. Thanks for reporting!

sreelakshmi-bruno avatar Nov 05 '24 08:11 sreelakshmi-bruno

Hi @developer-sunnywu , @JoergJans, this issue no longer happens to me in 1.35. Can you check it out? Thanks!

sreelakshmi-bruno avatar Nov 25 '24 06:11 sreelakshmi-bruno

@sreelakshmi-bruno I am using v.1.36.1, the problem still persist, can you help to check? Thanks

wusunny2022 avatar Dec 14 '24 08:12 wusunny2022

I've had the same issue on v1.38.1. After opening Console (Ctrl+Shift+I) I discovered that, in my case, the issue was Global environment variable stored as integer instead of string. I stored some values during postprocessing of the response and saved some integer values. After that, no variable can be modified, nor programmatically nor via Editor. I manually edited the Environment definition file, converting integers into strings, and after that everything was working fine again, Workaround that I use at the moment is to store every variable as string. I hope this helps.

kufti avatar Jan 29 '25 14:01 kufti

I've had the same issue on v1.38.1. After opening Console (Ctrl+Shift+I) I discovered that, in my case, the issue was Global environment variable stored as integer instead of string. I stored some values during postprocessing of the response and saved some integer values. After that, no variable can be modified, nor programmatically nor via Editor. I manually edited the Environment definition file, converting integers into strings, and after that everything was working fine again, Workaround that I use at the moment is to store every variable as string. I hope this helps.

That fixed my case, thanks!

tmbs avatar Mar 28 '25 14:03 tmbs

Apologies for the delay it's taking to fix this issue. We have a draft PR in place which attempts to solve this issue. Since it's a key change, it'll take more time to approve the approach and merge it. Until then, manually stringifying the variables before storing it seems to be the workaround. Thanks!

sreelakshmi-bruno avatar Mar 31 '25 11:03 sreelakshmi-bruno

I am having this issue in version 2.0.1 when trying to save a change to a Global Environment variable.

edwa1359 avatar Apr 03 '25 12:04 edwa1359

Same issue on 2.3.0, when I try to add a global env variable, If I restart the app it works but it seems like when i send some requests and then try to add another var I get the same error "An error occurred while saving the changes"

MikeJacquot avatar May 22 '25 08:05 MikeJacquot

@MikeJacquot This issue might be occurring because you're assigning an object to a Global Variable in your requests, rather than a string or number.

While you can set and retrieve objects using the bru.setGlobalEnvVar() API, the Bruno UI currently only supports primitive values (strings and numbers) for global variables. As a result, saving or editing variables through the UI can cause it to break.

We’re aware of this limitation and are working on a solution. In the meantime, a temporary workaround is to use bru.setVar() and bru.getVar() instead. These APIs store values in memory rather than the global environment. However, keep in mind that these values are ephemeral and will be lost when you restart the app.

helloanoop avatar May 22 '25 11:05 helloanoop

I have an issue that is similar to the scenario above.

  1. I have a request with a post-script that sets the environment variable bru.setEnvVar("token",res("access_token"));
  2. When I send the request, the environment variable gets set. (Let's call this request 1)
  3. When I use the token variable in a subsequent request ( let's call it Request 2), it works fine.
  4. Now, if I edit the environment variable token manually with a new value, it gets updated in the environment variable. I can see it updated.
  5. When I run Request 2 this time (assuming it will pick up the manually updated value), it still uses the value that was set by request 1.
  6. Only after I restart the application, Request 2 picks up the manually updated value.

amrutprabhu avatar Aug 05 '25 14:08 amrutprabhu

@amrutprabhu If you’ve set any object/integer via script, it ends up corrupting the environment. After that, any manual updates won’t get saved to the file.

Bruno doesn’t throw an error when you try to update manually, but you’ll notice the “Changes saved successfully” pop-up doesn’t show up.

As a workaround for now, you can stringify any objects before setting them via script. A similar issue also exists in the global environment: #4680 (comment)

anusree-bruno avatar Aug 07 '25 14:08 anusree-bruno

@amrutprabhu If you’ve set any object/integer via script, it ends up corrupting the environment. After that, any manual updates won’t get saved to the file.

Bruno doesn’t throw an error when you try to update manually, but you’ll notice the “Changes saved successfully” pop-up doesn’t show up.

As a workaround for now, you can stringify any objects before setting them via script. A similar issue also exists in the global environment: #4680 (comment)

I did try to stringify the object before setting it, and I still get the same behaviour.

bru.setEnvVar("token",JSON.stringify(res("access_token")));

Also, when I try saving the value manually, "Changes saved successfully" does pop up, and when I try to use the variable, it still uses what was stored by the script earlier.

amrutprabhu avatar Aug 07 '25 14:08 amrutprabhu

Hi @amrutprabhu would you be able to share a sample collection with me to reproduce this?

anusree-bruno avatar Aug 11 '25 12:08 anusree-bruno

Hi, this issue is no longer reproducible in the latest version (v2.13.2). We’ve fixed the bug where setting an env to a non-string value would corrupt it, and we’ve also added multiline support for env variables.

I’m closing this issue as it has been resolved. Please feel free to reopen it if you run into any related problems again.

sreelakshmi-bruno avatar Nov 08 '25 08:11 sreelakshmi-bruno