usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
27.74k stars 1.28k forks source link

Cannot run a script file in parent folder #3384

Closed Nizoryu closed 4 days ago

Nizoryu commented 3 weeks ago

I have checked the following:

Describe the bug

I have no problem running my script from the root folder of the collection.

bruno/
├── demo/
    └── environnements
    └── bruno.json
    └── get demo.bru
    └── script.js

When I move the file to the parent folder like this:

bruno/
├── demo/
    └── environnements
    └── bruno.json
    └── get demo.bru
└── script.js

i have this issue: Error invoking remote method 'send-http-request': VMError: Cannot find module '../script.js'

.bru file to reproduce the bug

meta { name: get demo type: http seq: 2 }

get { url: http://localhost:3000 body: none auth: none }

script:pre-request { const script = require("../script.js"); // const moment = require('moment');

console.log(bru.getVar('today')); }

Screenshots/Live demo link

image

sreelakshmi-bruno commented 2 weeks ago

Hi @Nizoryu, access to files outside of the collection path is not allowed in Bruno. When doing so you'll get an error message saying 'Error: Access to files outside of the collectionPath is not allowed'. I'm not sure why you are getting a 'Cannot find module' message. Can you check again and get back? Thanks! Also from the file structure you've shown, I assume 'demo' as your collection.

Related to #1397 and #795

sreelakshmi-bruno commented 2 weeks ago

Could you share your use case? Are you looking to share a common script file across multiple collections?

Nizoryu commented 5 days ago

Could you share your use case? Are you looking to share a common script file across multiple collections?

bruno-demo-external-script.zip

tlaloc911 commented 5 days ago

@Nizoryu , it is possible to do that, with two considerations

1) You have to declare the external path (take a look into #795) 2) It only works in pre-request. There is an unmmerged PR that aims to make it work in post-response: #1397

Nizoryu commented 4 days ago

@Nizoryu , it is possible to do that, with two considerations

  1. You have to declare the external path (take a look into [feature] allow multiple script context roots #795)
  2. It only works in pre-request. There is an unmmerged PR that aims to make it work in post-response: feat(scripting): allow multiple script context roots in response scripts #1397

Thank you for you help, the external path is perfect for my use case.