Skip to content

Commit 793f04c

Browse files
committed
More.
1 parent 5eb72de commit 793f04c

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

build/lib/src/state/filesystem_cache.dart

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,17 @@ class InMemoryFilesystemCache implements FilesystemCache {
151151
final maybeResult = _bytesContentCache[id];
152152
if (maybeResult != null) return maybeResult;
153153

154-
// check canRead first?
154+
// Not in cache. Check pending writes and deletes.
155155
if (_pendingDeletes.containsKey(id)) throw AssetNotFoundException(id);
156156
if (_pendingWrites.containsKey(id)) {
157-
throw StateError('Whoops, should have been cached: $id');
157+
throw StateError('Should be in cache: $id');
158158
}
159159

160-
return _bytesContentCache[id] = ifAbsent();
160+
// Read and cache.
161+
final bytes = _bytesContentCache[id] = ifAbsent();
162+
// Might not be a valid String, only convert to String when read as String.
163+
_stringContentCache.remove(id);
164+
return bytes;
161165
}
162166

163167
@override
@@ -174,18 +178,21 @@ class InMemoryFilesystemCache implements FilesystemCache {
174178
final maybeResult = _stringContentCache[id];
175179
if (maybeResult != null) return maybeResult;
176180

177-
// check canRead first?
181+
// Not in cache. Check pending writes and deletes.
178182
if (_pendingDeletes.containsKey(id)) throw AssetNotFoundException(id);
179183
if (_pendingWrites.containsKey(id)) {
184+
// Might have been written as bytes; only cached as String when it's
185+
// read as String, because it might not be a valid String.
180186
final bytes = _bytesContentCache[id];
181187
if (bytes == null) {
182-
throw StateError('Whoops, should have been cached: $id');
188+
throw StateError('Should be in cache: $id');
183189
} else {
184190
return _stringContentCache[id] = utf8.decode(_bytesContentCache[id]!);
185191
}
186192
}
187193

188194
final bytes = ifAbsent();
195+
_bytesContentCache[id] = bytes;
189196
return _stringContentCache[id] = utf8.decode(bytes);
190197
}
191198

@@ -195,13 +202,12 @@ class InMemoryFilesystemCache implements FilesystemCache {
195202
List<int> contents, {
196203
required void Function() writer,
197204
}) {
198-
_bytesContentCache[id] =
199-
(contents is Uint8List ? contents : Uint8List.fromList(contents));
200205
// [contents] might not be a valid string so defer trying to
201206
// decode it until if/when it's read as a string.
202207
_stringContentCache.remove(id);
208+
_bytesContentCache[id] =
209+
(contents is Uint8List ? contents : Uint8List.fromList(contents));
203210
_canReadCache[id] = true;
204-
205211
_pendingDeletes.remove(id);
206212
_pendingWrites[id] = writer;
207213
}
@@ -213,19 +219,28 @@ class InMemoryFilesystemCache implements FilesystemCache {
213219
Encoding encoding = utf8,
214220
required void Function() writer,
215221
}) {
216-
// TODO: encoding?
217-
_stringContentCache[id] = contents;
218-
_bytesContentCache[id] = utf8.encode(contents);
219-
_canReadCache[id] = true;
222+
// Strings are only cached if utf8.
223+
if (encoding == utf8) {
224+
_stringContentCache[id] = contents;
225+
} else {
226+
_stringContentCache.remove(id);
227+
}
228+
229+
final encoded = encoding.encode(contents);
230+
_bytesContentCache[id] =
231+
encoded is Uint8List ? encoded : Uint8List.fromList(encoded);
220232

233+
_canReadCache[id] = true;
221234
_pendingDeletes.remove(id);
222235
_pendingWrites[id] = writer;
223236
}
224237

225238
@override
226239
void delete(AssetId id, {required void Function() deleter}) {
240+
_stringContentCache.remove(id);
241+
_bytesContentCache.remove(id);
242+
_canReadCache[id] = false;
227243
_pendingWrites.remove(id);
228244
_pendingDeletes[id] = deleter;
229-
_canReadCache[id] = false;
230245
}
231246
}

build_runner/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Compute outputs as needed instead of storing them in the asset graph.
1717
- Refactor invalidation to track current build progress in `Build` instead of
1818
in the asset graph.
19+
- Remove `completeBuild` from `RunnerAssetWriter` as it's no longer needed.
1920

2021
## 2.4.15
2122

build_runner_core/lib/src/generate/build.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ class Build {
311311
},
312312
(e, st) {
313313
if (!done.isCompleted) {
314-
print('Unhandled build failure! $e $st');
315314
_logger.severe('Unhandled build failure!', e, st);
316315
done.complete(BuildResult(BuildStatus.failure, []));
317316
}

build_test/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- Add `testingBuilderConfig` to `testBuilders` to control builder config
1010
override.
1111
- Add `resolvers` parameter to `testBuild` and `testBuilders`.
12+
- Add `readerWriter` and `enableLowResourceMode` parameters to `testBuild`
13+
and `testBuilders`.
1214
- Breaking change: removed `tearDown` parameter to `resolveSources` for
1315
keeping resolvers across multiple tests.
1416
- Breaking change: tests must use new `TestReaderWriter` instead of

build_test/lib/src/test_builder.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,16 @@ Future<TestBuilderResult> testBuilder(
200200
/// builder to run for all inputs. To use the default builder config instead,
201201
/// set [testingBuilderConfig] to `false`.
202202
///
203-
/// Returns a [TestBuilderResult] with the [TestReaderWriter] used for
204-
/// the build, which can be used for further checks.
203+
/// Optionally pass [readerWriter] to pass in the filesystem that will be
204+
/// used during the build. Before the build, [sourceAssets] will be written
205+
/// to it.
206+
///
207+
/// Optionally pass [enableLowResourceMode], which acts like the command
208+
/// line flag; in particular it disables file caching.
209+
///
210+
/// Returns a [TestBuilderResult] with the [BuildResult] and the
211+
/// [TestReaderWriter] used for the build, which can be used for further
212+
/// checks.
205213
Future<TestBuilderResult> testBuilders(
206214
Iterable<Builder> builders,
207215
Map<String, /*String|List<int>*/ Object> sourceAssets, {

0 commit comments

Comments
 (0)