Scenario __ENV variables are undefined when iterations or duration is provided

Here’s a simple example (referred to as test.js below):

import http from 'k6/http';

export const options = {
  scenarios: {
    example_scenario: {
      executor: 'shared-iterations',
      env: { EXAMPLEVAR: 'testing' }
    }
  }
}

export default function () {
  console.log('__ENV.EXAMPLEVAR: ' + __ENV.EXAMPLEVAR)
  http.get('https://test.k6.io');
}

When I run k6 run test.js, I see __ENV.EXAMPLEVAR: testing as expected. However, when I run k6 run --vus 10 --duration 30s test.js or k6 run --vus 10 --iterations 100 test.js, I see __ENV.EXAMPLEVAR: undefined.

Is this a bug, or is there a way to change the script to fix this?

As a workaround, setting iterations and vus in options like this instead of passing them to k6 run works:

export const options = {
  scenarios: {
    example_scenario: {
      executor: 'shared-iterations',
      env: { EXAMPLEVAR: 'testing' },
      iterations: 100,
      vus: 10
    }
  }
}

Hi @bslaught , welcome to the community forum.

Effectively using any of the --iteration/-i , --duration/-d, --vus/-u options tells k6 “don’t look at options.scenarios, build me a new one with one executor that has these properties”. While in your case you can argue that this should work - it can’t in the general one:

  1. combinations of the three option can configure different executor types - different than shared-iterations.
  2. you have one executor, but you can have multiple ones.

Hope this explains why this happens

I see. It might help to print a message stating that scenarios are being ignored (I see scenarios: (100.00%) 1 scenario, 10 max VUs, 1m0s max duration (incl. graceful stop) when providing these options), or note in Scenarios that scenarios are skipped when these options are passed.

That doesn’t seem like a bad idea I guess - I’ve opened an issue. You can add more info if you want.

Although there is already some evidence in the output - just on the line below the one you quoted you will see the name of the scenario(s) and it changes from example_scenario to default

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * example_scenario: 1 iterations shared among 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)
  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 2 iterations shared among 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

Oh, I had missed that. Thanks for looking and opening the issue!