test icon indicating copy to clipboard operation
test copied to clipboard

Cannot access environment on dart test

Open mnordine opened this issue 3 years ago • 9 comments

void main() {
  print(const bool.hasEnvironment('blah'));
}

dart -Dblah=true test outputs:

false
No tests were found.

It seems like the environment does not exist when you run dart test

Dart SDK 2.18.4

mnordine avatar Nov 21 '22 21:11 mnordine

Should work according to https://github.com/dart-lang/test/issues/1509, but indeed does not work. Must be a regression.

/cc @bkonyi @natebosch

mraleph avatar Nov 21 '22 21:11 mraleph

It seems it doesn't pick up any environment variables, either.

mnordine avatar Nov 21 '22 22:11 mnordine

You can get back to the old behavior with the flag --use-data-isolate-strategy

With the new strategy we might need to build support for parsing the arguments as passed to the test runner. If we did that we'd probably want it to be general and work for dart2js tests as well.

cc @jakemac53

natebosch avatar Nov 21 '22 22:11 natebosch

You can get back to the old behavior with the flag --use-data-isolate-strategy

Confirmed on my machine that dart -Dblah=true test --use-data-isolate-strategy works.

It still doesn't pick up any environment variables, though.

So If I do

MY_VAR=true
import 'dart:io' show Platform;

void main() {
  print(Platform.environment['MY_VAR']);
}

dart test --use-data-isolate-strategy outputs:

null

mnordine avatar Nov 21 '22 22:11 mnordine

It still doesn't pick up any environment variables, though.

I can't repro. Either strategy picks up the environment for me. Are you confident that the environment for the dart test process has the variable?

natebosch avatar Nov 21 '22 23:11 natebosch

Are you confident that the environment for the dart test process has the variable?

My bad, I didn't export the env var. After I did, it works.

mnordine avatar Nov 22 '22 00:11 mnordine

I don't see this as a high priority feature personally, and adding -D support may also further complicate any future plans regarding DDC or other modular compilation. If we add it, I would probably want to make it only a global option to alleviate that issue (build_web_compilers also only supports it as a global for this reason).

jakemac53 avatar Nov 22 '22 15:11 jakemac53