Lazy construction of cache path. (#3837)

diff --git a/lib/src/system_cache.dart b/lib/src/system_cache.dart
index 494d3b0..90bd7d5 100644
--- a/lib/src/system_cache.dart
+++ b/lib/src/system_cache.dart
@@ -32,7 +32,8 @@
 /// cache.
 class SystemCache {
   /// The root directory where this package cache is located.
-  final String rootDir;
+  String get rootDir => _rootDir ??= defaultDir;
+  String? _rootDir;
 
   String rootDirForSource(CachedSource source) => p.join(rootDir, source.name);
 
@@ -90,6 +91,7 @@
   SdkSource get sdk => SdkSource.instance;
 
   /// The default credential store.
+  /// TODO(sigurdm): this does not really belong in the cache.
   final TokenStore tokenStore;
 
   /// If true, cached sources will attempt to use the cached packages for
@@ -101,7 +103,7 @@
   /// If [isOffline] is `true`, then the offline hosted source will be used.
   /// Defaults to `false`.
   SystemCache({String? rootDir, this.isOffline = false})
-      : rootDir = rootDir ?? SystemCache.defaultDir,
+      : _rootDir = rootDir,
         tokenStore = TokenStore(dartConfigDir);
 
   /// Loads the package identified by [id].
diff --git a/test/golden_file.dart b/test/golden_file.dart
index 515a6b8..82024cb 100644
--- a/test/golden_file.dart
+++ b/test/golden_file.dart
@@ -157,7 +157,7 @@
   /// log stdout/stderr and exit code to golden file.
   Future<void> run(
     List<String> args, {
-    Map<String, String>? environment,
+    Map<String, String?>? environment,
     String? workingDirectory,
     String? stdin,
   }) async {
diff --git a/test/help_test.dart b/test/help_test.dart
index 767aad9..3e0a0f3 100644
--- a/test/help_test.dart
+++ b/test/help_test.dart
@@ -46,7 +46,9 @@
         [...c, '--help'],
         environment: {
           // Use more columns to avoid unintended line breaking.
-          '_PUB_TEST_TERMINAL_COLUMNS': '200'
+          '_PUB_TEST_TERMINAL_COLUMNS': '200',
+          'HOME': null,
+          'PUB_CACHE': null,
         },
       );
     });
diff --git a/test/test_pub.dart b/test/test_pub.dart
index 2bb9002..8f84125 100644
--- a/test/test_pub.dart
+++ b/test/test_pub.dart
@@ -968,7 +968,7 @@
 Future<void> runPubIntoBuffer(
   List<String> args,
   StringBuffer buffer, {
-  Map<String, String>? environment,
+  Map<String, String?>? environment,
   String? workingDirectory,
   String? stdin,
 }) async {