Skip to content

Update usages of fromEnvironment to handle breaking change #40678 #362

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

Closed
eernstg opened this issue Mar 23, 2020 · 1 comment
Closed

Update usages of fromEnvironment to handle breaking change #40678 #362

eernstg opened this issue Mar 23, 2020 · 1 comment

Comments

@eernstg
Copy link

eernstg commented Mar 23, 2020

SDK issue 40678 is concerned with a breaking change which will be performed in the Dart language and core libraries: The constant constructors int.fromEnvironment and String.fromEnvironment will have default values 0 and "", respectively, for the parameter named defaultValue.

This means that the following usages in protobuf will have a new semantics (and presumably will not work as intended):

./third_party/pkg/protobuf/query_benchmark/bin/encode_json.dart:  String path = const String.fromEnvironment('testfile') ?? 'testdata/500.pb';
./third_party/pkg/protobuf/query_benchmark/bin/decode.dart:  String path = const String.fromEnvironment('testfile') ?? 'testdata/500.pb';
./third_party/pkg/protobuf/query_benchmark/bin/set_nested_value.dart:  String path = const String.fromEnvironment('testfile') ?? 'testdata/500.pb';
./third_party/pkg/protobuf/query_benchmark/bin/encode.dart:  String path = const String.fromEnvironment('testfile') ?? 'testdata/500.pb';
./third_party/pkg/protobuf/query_benchmark/bin/decode_json.dart:  String path = const String.fromEnvironment('testfile') ?? 'testdata/500.pb';

The expressions should be changed as follows in order to work:

// Current form
String path = const String.fromEnvironment('testfile') ?? 'testdata/500.pb';

// New form that will work also after the breaking change has occurred.
String path = const bool.hasEnvironment('testfile')
    ? const String.fromEnvironment('testfile')
    : 'testdata/500.pb';

The constructor bool.hasEnvironment has been added to Dart as of dart-lang/sdk@4aba581. Until that commit is available, it is possible to replace const bool.hasEnvironment('testfile') by (const String.fromEnvironment('testfile', defaultValue: 'a') == const String.fromEnvironment('testfile', defaultValue: 'b')).

@osa1
Copy link
Member

osa1 commented Apr 13, 2022

It seems like this was fixed by f611f23. I checked uses of fromEnvironment methods. We don't use int.fromEnvironment, and for String.fromEnvironment we always specify the default values:

query_benchmark/bin/decode_json.dart:11:      const String.fromEnvironment('testfile', defaultValue: 'testdata/500.pb');
query_benchmark/bin/decode.dart:11:      const String.fromEnvironment('testfile', defaultValue: 'testdata/500.pb');
query_benchmark/bin/set_nested_value.dart:14:      const String.fromEnvironment('testfile', defaultValue: 'testdata/500.pb');
query_benchmark/bin/encode_json.dart:11:      const String.fromEnvironment('testfile', defaultValue: 'testdata/500.pb');
query_benchmark/bin/encode.dart:11:      const String.fromEnvironment('testfile', defaultValue: 'testdata/500.pb');

@osa1 osa1 closed this as completed Apr 13, 2022
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

2 participants