Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot access environment on dart test #1794

Open
mnordine opened this issue Nov 21, 2022 · 9 comments
Open

Cannot access environment on dart test #1794

mnordine opened this issue Nov 21, 2022 · 9 comments

Comments

@mnordine
Copy link

mnordine commented Nov 21, 2022

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

@mraleph mraleph transferred this issue from dart-lang/sdk Nov 21, 2022
@mraleph
Copy link
Member

mraleph commented Nov 21, 2022

Should work according to #1509, but indeed does not work. Must be a regression.

/cc @bkonyi @natebosch

@mnordine
Copy link
Author

mnordine commented Nov 21, 2022

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

@natebosch
Copy link
Member

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

@mnordine
Copy link
Author

mnordine commented Nov 21, 2022

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

@natebosch
Copy link
Member

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?

@mnordine
Copy link
Author

mnordine commented Nov 22, 2022

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.

@jakemac53
Copy link
Contributor

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).

@PvtPuddles
Copy link

PvtPuddles commented Feb 3, 2025

Hello, I have an issue I believe to be closely related to this.

I am trying to test the following code on Mac (arm64), dart 3.6.0:

import 'package:test/test.dart';

void main() {
  final constString = const String.fromEnvironment("STRING");
  final nonConstString = String.fromEnvironment("STRING");

  print(" '$constString' != '$nonConstString' ");

  test("blank test", () {});
}

It seemed that (nearly) no matter how I run this, const String.fromEnvironment didn't work, but String.fromEnvironment did, which goes against my understanding that only the const version is supposed to work..

I tried:

dart run --define=STRING="myStr" test test/weird_issue.dart
00:00 +0: loading test/weird_issue.dart                                                                                                                                                                                                 
 '' != 'myStr' 
00:00 +1: All tests passed!   

After coming across this thread I tried running using the use-data-isolate-strategy flag, which seems to have worked.

dart --define=STRING="myStr" test --use-data-isolate-strategy test/weird_issue.dart
00:00 +0: [VM, Source] loading test/weird_issue.dart                                                                                                                                                                                    
 'myStr' != 'myStr' 
00:00 +1: All tests passed! 

use-data-isolate-strategy seems to slow down my tests considerably, so I am eager to hear if there are other options available.

Hopefully this is also useful for you all.

@jakemac53
Copy link
Contributor

The const constructor is compile time - so it uses the value provided to the compiler, which is typically nothing (the way package:test invokes it). It works with --use-data-isolate-strategy because then compiling happens as a part of the same isolate as the test runner itself, which does have the environment you specified.

The non-const constructor is a runtime lookup which works for the kernel compiler at least - because we still spawn the pre-compiled kernel file as a sub-isolate of the test runner isolate, so at runtime the value does exist.

I doubt either of these solutions would work with the exe compiler, or for any form of web test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants