Deny setting operations on __ENV variables from JS code
#1.722 aberto em 13 de nov. de 2020
Métricas do repositório
- Stars
- (30.564 stars)
- Métricas de merge de PR
- (Mesclagem média 11d 22h) (44 fundiu PRs em 30d)
Description
Update — the current agreement from the comments:
https://github.com/grafana/k6/issues/1722#issuecomment-796752500
So, yeah, I think we should either close this issue and https://github.com/grafana/k6/pull/1877, or "fix" it by freezing __ENV so any attempts to change it won't silently fail, but would throw an exception. The script pattern could be used for having default values of script options, instead of modifying __ENV:
const myScriptOption = (typeof __ENV.something !== 'undefined') ? __ENV.something : 'defaultValue';This has been broken for a while, but very few people have complained and there's an easy workaround shown above, so my vote is for "fixing" the current behavior in the sense of making it permanent and explicit by freezing __ENV 😅
if (typeof __ENV.something == "undefined") {
__ENV.something= "test";
}
export default function () {
console.log(__ENV.something);
}
This code should print "test" and it did up to https://github.com/loadimpact/k6/commit/e1a5c0af001977b6a10945791f5b16288937d391 and if the golang version is go1.13.8 but not if it is 1.15.3 (or 1.14.6)

This is fairly common pattern to fix your __ENV variables in cases where you didn't provide them but you use them through the script.