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

Pass dart defines when -c kernel #2272

Open
davidmorgan opened this issue Aug 29, 2024 · 6 comments
Open

Pass dart defines when -c kernel #2272

davidmorgan opened this issue Aug 29, 2024 · 6 comments

Comments

@davidmorgan
Copy link

Testing code that uses bool.fromEnvironment, it looks like

dart -Dfoo=true test

never picks up the true value with the default -c kernel, but with -c source it does.

This took me a while to figure out, maybe it could be supported?

It looks like "flutter test" does support it via --dart-define, although I didn't try it.

Maybe just a note in the docs that it works with flutter test but there's no full equivalent in dart test?

Thanks :)

@jakemac53
Copy link
Contributor

I think we probably would want to support it like dart test -Dfoo=true - what you are doing is actually setting the define for the test runner itself, which happens to work in "source" mode because I think we run a sub-isolate and let the VM do its own compilation which inherits the defines.

@jakemac53
Copy link
Contributor

cc @natebosch this could be one answer to people asking for command line arguments to tests.

@natebosch
Copy link
Member

I don't think we have any way to read these values within the Dart code of the test runner. This would require a change in the Dart CLI to specially forward these to the test runner. @bkonyi does that seem feasible?

@jakemac53
Copy link
Contributor

jakemac53 commented Aug 29, 2024

We should be able to support passing these as our own args though right? I am suggesting we add a --define (with -D shorthand) multi option argument to package:test. And pass that along to the compilers. We don't actually want the defines available to the test runner itself, just passed when compiling the tests.

Although, I don't think we could support it if running from source the way we currently do.

@bkonyi
Copy link
Contributor

bkonyi commented Aug 30, 2024

We want the actual test to have access to this code, right? If so, it needs to be provided at kernel compile time so package:test will need to handle that when running from kernel.

However, if we're running from source these defines need to be set at runtime (e.g., what dart -Dfoo=bar test currently does). Without support for setting environment via Isolate.spawnUri (the proposal I made in dart-lang/sdk#55812 that was ultimately shot down), we'll need special plumbing to make this work.

@jakemac53
Copy link
Contributor

Right, I don't see how we could support this uniformly for all modes (running via Isolate.spawnUri as well as separately compiled and executed programs), because with the tools currently available the argument would have to be passed in a different location for each.

I think @natebosch was suggesting though that if the Dart CLI forwarded the -D flags that precede the test command to the test runner as regular arguments, it could be supported. Or, we would need a Dart API for the test runner to read all the -D arguments it was executed with, which I don't think is available. You can only read known variables.

This also would only work for the dart test command and not dart run test or dart bin/test.dart etc. So, still not really a full solution but would work for the majority of use cases.

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

4 participants