Skip to content

Commit 7cced92

Browse files
authored
Fix compilation with DDC (#516)
b272632 exposed client certificate through `X509Certificate? get clientCertificate;` getter on `ServiceCall` class. This broke compilation of `grpc_web` code using DDC, but not dart2js. Turns out that dart2js is happy to compile any code using `dart:io` (though the result will not run if you try to use any of those APIs), but DDC rejects such code eagerly. `package:test` runs tests through `dart2js` so DDC breakage was not really caught by CI. Unfortunately this discrepancy between DDC and dart2js puts us in some really weird spot: most of our tests are platform independent, but most of those tests also pull in `dart:io` through transitive dependencies. This commit is the most minimal change we could make to allow the code compile both on the Web and natively. A proper fix should be to go through tests one-by-one and make sure that those that need to run on the Web don't import `dart:io`, but we don't have time to do that right now. This commit also adds a smoke test to the CI to verify that `grpc_web` example builds with DDC.
1 parent c2fb47c commit 7cced92

File tree

10 files changed

+86
-11
lines changed

10 files changed

+86
-11
lines changed

.github/workflows/dart.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ jobs:
1919
sdk: [dev, 2.12.0]
2020
steps:
2121
- uses: actions/checkout@v2
22-
- uses: dart-lang/setup-dart@v1.0
22+
- uses: dart-lang/setup-dart@v1.2
2323
with:
2424
sdk: ${{ matrix.sdk }}
2525
- name: Report version
2626
run: dart --version
2727
- name: Install dependencies
2828
run: dart pub get
29-
- name: Check formatting
29+
- name: Check formatting (using dev dartfmt release)
30+
if: ${{ matrix.sdk == 'dev' }}
3031
run: dart format --output=none --set-exit-if-changed .
3132
- name: Analyze code (introp and examples)
3233
run: |
@@ -39,6 +40,16 @@ jobs:
3940
shell: bash
4041
- name: Analyze code
4142
run: dart analyze --fatal-infos .
43+
- name: Check that grpc-web sample builds with DDC
44+
if: ${{ matrix.sdk == 'dev' }}
45+
# webdev build --no-release to force compilation with DDC.
46+
run: |
47+
dart pub global activate webdev
48+
pushd example/grpc-web
49+
rm -rf build
50+
dart pub global run webdev build --no-release
51+
test -f ./build/main.dart.js
52+
popd
4253
4354
# Run tests on a matrix consisting of three dimensions:
4455
# 1. OS: mac, windows, linux
@@ -61,7 +72,7 @@ jobs:
6172
platform: chrome
6273
steps:
6374
- uses: actions/checkout@v2
64-
- uses: dart-lang/setup-dart@v1.0
75+
- uses: dart-lang/setup-dart@v1.2
6576
with:
6677
sdk: ${{ matrix.sdk }}
6778
- name: Report version

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.0.2
2+
3+
* Fix compilation on the Web with DDC.
4+
15
## 3.0.1
26

37
* Require `package:googleapis_auth` `^1.1.0`

lib/src/server/call.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
import 'dart:io';
16+
import '../shared/io_bits/io_bits.dart' show X509Certificate;
1717

1818
/// Server-side context for a gRPC call.
1919
///

lib/src/server/handler.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
import 'dart:async';
1717
import 'dart:convert';
18-
import 'dart:io';
1918

2019
import 'package:http2/transport.dart';
2120

2221
import '../shared/codec.dart';
2322
import '../shared/codec_registry.dart';
23+
import '../shared/io_bits/io_bits.dart' show X509Certificate;
2424
import '../shared/message.dart';
2525
import '../shared/status.dart';
2626
import '../shared/streams.dart';

lib/src/server/server.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'package:http2/transport.dart';
2020
import 'package:meta/meta.dart';
2121

2222
import '../shared/codec_registry.dart';
23+
import '../shared/io_bits/io_bits.dart' as io_bits;
2324
import '../shared/security.dart';
2425
import 'handler.dart';
2526
import 'interceptor.dart';
@@ -129,8 +130,10 @@ class ConnectionServer {
129130
ServerHandler_ serveStream_(ServerTransportStream stream,
130131
[X509Certificate? clientCertificate]) {
131132
return ServerHandler_(
132-
lookupService, stream, _interceptors, _codecRegistry, clientCertificate)
133-
..handle();
133+
lookupService, stream, _interceptors, _codecRegistry,
134+
// ignore: unnecessary_cast
135+
clientCertificate as io_bits.X509Certificate?,
136+
)..handle();
134137
}
135138
}
136139

@@ -223,8 +226,13 @@ class Server extends ConnectionServer {
223226
ServerHandler_ serveStream_(ServerTransportStream stream,
224227
[X509Certificate? clientCertificate]) {
225228
return ServerHandler_(
226-
lookupService, stream, _interceptors, _codecRegistry, clientCertificate)
227-
..handle();
229+
lookupService,
230+
stream,
231+
_interceptors,
232+
_codecRegistry,
233+
// ignore: unnecessary_cast
234+
clientCertificate as io_bits.X509Certificate?,
235+
)..handle();
228236
}
229237

230238
@Deprecated(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2021, the gRPC project authors. Please see the AUTHORS file
2+
// for details. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
export 'io_bits_io.dart' if (dart.library.html) 'io_bits_web.dart';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2021, the gRPC project authors. Please see the AUTHORS file
2+
// for details. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
export 'dart:io' show HttpStatus, X509Certificate;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2021, the gRPC project authors. Please see the AUTHORS file
2+
// for details. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
export 'dart:html' show HttpStatus;
17+
18+
/// Should not be used on the Web, but is pulled through [ServiceCall] class
19+
/// which is used in the protoc generated code.
20+
class X509Certificate {}

lib/src/shared/status.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// limitations under the License.
1515

1616
import 'dart:convert';
17-
import 'dart:io' show HttpStatus;
1817

1918
import 'package:meta/meta.dart';
2019
import 'package:protobuf/protobuf.dart';
@@ -23,6 +22,7 @@ import '../generated/google/protobuf/any.pb.dart';
2322
import '../generated/google/rpc/code.pbenum.dart';
2423
import '../generated/google/rpc/error_details.pb.dart';
2524
import '../generated/google/rpc/status.pb.dart';
25+
import 'io_bits/io_bits.dart' show HttpStatus;
2626

2727
class StatusCode {
2828
/// The operation completed successfully.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: grpc
22
description: Dart implementation of gRPC, a high performance, open-source universal RPC framework.
33

4-
version: 3.0.1
4+
version: 3.0.2
55

66
repository: https://github.com/grpc/grpc-dart
77

0 commit comments

Comments
 (0)