diff --git a/lib/dartdoc.dart b/lib/dartdoc.dart index 70c31c775d..f500ad86f1 100644 --- a/lib/dartdoc.dart +++ b/lib/dartdoc.dart @@ -262,7 +262,7 @@ class Dartdoc { return dartdocResults; } finally { // Clear out any cached tool snapshots and temporary directories. - SnapshotCache.instance?.dispose(); + SnapshotCache.instanceFor(config.resourceProvider).dispose(); // ignore: unawaited_futures ToolTempFileTracker.instance?.dispose(); } diff --git a/lib/src/tool_definition.dart b/lib/src/tool_definition.dart index 039d501e5d..be4b4a1bb5 100644 --- a/lib/src/tool_definition.dart +++ b/lib/src/tool_definition.dart @@ -109,8 +109,8 @@ class DartToolDefinition extends ToolDefinition { assert(args[0] == command.first); // Set up flags to create a new snapshot, if needed, and use the first run // as the training run. - SnapshotCache.createInstance(_resourceProvider); - var snapshot = SnapshotCache.instance.getSnapshot(command.first); + var snapshotCache = SnapshotCache.instanceFor(_resourceProvider); + var snapshot = snapshotCache.getSnapshot(command.first); var snapshotFile = snapshot._snapshotFile; var snapshotPath = _resourceProvider.pathContext.absolute(snapshotFile.path); @@ -212,11 +212,11 @@ class _Snapshot { void _snapshotCompleted() => _snapshotCompleter.complete(); } -/// A singleton that keeps track of cached snapshot files. The [dispose] +/// A class that keeps track of cached snapshot files. The [dispose] /// function must be called before process exit to clean up snapshots in the /// cache. class SnapshotCache { - static SnapshotCache _instance; + static final _instances = {}; final Folder snapshotCache; final ResourceProvider _resourceProvider; @@ -227,10 +227,12 @@ class SnapshotCache { : snapshotCache = _resourceProvider.createSystemTemp('dartdoc_snapshot_cache_'); - static SnapshotCache get instance => _instance; - - static SnapshotCache createInstance(ResourceProvider resourceProvider) => - _instance ??= SnapshotCache._(resourceProvider); + /// Returns a [SnapshotCache] for a given [ResourceProvider], creating one + /// only if one doesn't exist yet for the given [ResourceProvider]. + factory SnapshotCache.instanceFor(ResourceProvider resourceProvider) { + return _instances.putIfAbsent( + resourceProvider, () => SnapshotCache._(resourceProvider)); + } _Snapshot getSnapshot(String toolPath) { if (snapshots.containsKey(toolPath)) { @@ -243,7 +245,7 @@ class SnapshotCache { } void dispose() { - _instance = null; + _instances.remove(_resourceProvider); if (snapshotCache != null && snapshotCache.exists) { return snapshotCache.delete(); } diff --git a/lib/src/tool_runner.dart b/lib/src/tool_runner.dart index 1cfa337f41..fcdf5ab734 100644 --- a/lib/src/tool_runner.dart +++ b/lib/src/tool_runner.dart @@ -175,7 +175,7 @@ class ToolRunner { // find out where their script was coming from as an absolute path on the // filesystem. envWithInput['DART_SNAPSHOT_CACHE'] = pathContext.absolute( - SnapshotCache.createInstance(resourceProvider).snapshotCache.path); + SnapshotCache.instanceFor(resourceProvider).snapshotCache.path); if (toolDefinition.setupCommand != null) { envWithInput['DART_SETUP_COMMAND'] = toolDefinition.setupCommand[0]; } diff --git a/test/tool_runner_test.dart b/test/tool_runner_test.dart index 71515b1c70..5139947583 100644 --- a/test/tool_runner_test.dart +++ b/test/tool_runner_test.dart @@ -95,7 +95,8 @@ echo: tearDownAll(() { tempDir?.deleteSync(recursive: true); tracker?.dispose(); - SnapshotCache.instance?.dispose(); + SnapshotCache.instanceFor(pubPackageMetaProvider.resourceProvider) + .dispose(); setupFile = null; tempDir = null; });