-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathio.dart
1364 lines (1232 loc) · 44.2 KB
/
io.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Helper functionality to make working with IO easier.
library;
import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:async/async.dart';
import 'package:cli_util/cli_util.dart'
show EnvironmentNotFoundException, applicationConfigHome;
import 'package:collection/collection.dart';
import 'package:http/http.dart' show ByteStream;
import 'package:http_multi_server/http_multi_server.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:pool/pool.dart';
import 'package:stack_trace/stack_trace.dart';
import 'package:tar/tar.dart';
import 'error_group.dart';
import 'exceptions.dart';
import 'exit_codes.dart' as exit_codes;
import 'log.dart' as log;
import 'utils.dart';
export 'package:http/http.dart' show ByteStream;
/// Environment variable names that are recognized by pub.
class EnvironmentKeys {
/// Overrides terminal detection for stdout.
///
/// Supported values:
/// * missing or `''` (empty string): dart:io terminal detection is used.
/// * `"0"`: output as if no terminal is attached
/// - no animations
/// - no ANSI colors
/// - use unicode characters
/// - silent inside [log.errorsOnlyUnlessTerminal]).
/// * `"1"`: output as if a terminal is attached
/// - animations
/// - ANSI colors (can be overriden again with NO_COLOR)
/// - no unicode on Windows
/// - normal verbosity in output inside
/// [log.errorsOnlyUnlessTerminal]).
///
/// This variable is mainly for testing, and no forward compatibility
/// guarantees are given.
static const forceTerminalOutput = '_PUB_FORCE_TERMINAL_OUTPUT';
// TODO(sigurdm): Add other environment keys here.
}
/// The pool used for restricting access to asynchronous operations that consume
/// file descriptors.
///
/// The maximum number of allocated descriptors is based on empirical tests that
/// indicate that beyond 32, additional file reads don't provide substantial
/// additional throughput.
final _descriptorPool = Pool(32);
/// The assumed default file mode on Linux and macOS
const _defaultMode = 420; // 644₈
/// Mask for executable bits in file modes.
const _executableMask = 0x49; // 001 001 001
/// Determines if a file or directory exists at [path].
bool entryExists(String path) =>
dirExists(path) || fileExists(path) || linkExists(path);
/// Returns whether [link] exists on the file system.
///
/// This returns `true` for any symlink, regardless of what it points at or
/// whether it's broken.
bool linkExists(String link) => Link(link).existsSync();
/// Returns whether [file] exists on the file system.
///
/// This returns `true` for a symlink only if that symlink is unbroken and
/// points to a file.
bool fileExists(String file) => File(file).existsSync();
/// Stats [path], assuming it or the entry it is a link to is a file.
///
/// Returns `null` if it is not a file (eg. a directory or not existing).
FileStat? tryStatFile(String path) {
var stat = File(path).statSync();
if (stat.type == FileSystemEntityType.link) {
stat = File(File(path).resolveSymbolicLinksSync()).statSync();
}
if (stat.type == FileSystemEntityType.file) {
return stat;
}
return null;
}
FileStat statPath(String path) {
return File(path).statSync();
}
/// Returns the canonical path for [pathString].
///
/// This is the normalized, absolute path, with symlinks resolved. Broken or
/// recursive symlinks will not be fully resolved.
///
/// This doesn't require [pathString] to point to a path that exists on the
/// filesystem; nonexistent or unreadable path entries are treated as normal
/// directories.
String canonicalize(String pathString) {
final seen = <String>{};
var components = Queue<String>.from(
p.split(p.normalize(p.absolute(pathString))),
);
// The canonical path, built incrementally as we iterate through [components].
var newPath = components.removeFirst();
// Move through the components of the path, resolving each one's symlinks as
// necessary. A resolved component may also add new components that need to be
// resolved in turn.
while (components.isNotEmpty) {
seen.add(p.join(newPath, p.joinAll(components)));
final resolvedPath = _resolveLink(
p.join(newPath, components.removeFirst()),
);
final relative = p.relative(resolvedPath, from: newPath);
// If the resolved path of the component relative to `newPath` is just ".",
// that means component was a symlink pointing to its parent directory. We
// can safely ignore such components.
if (relative == '.') continue;
final relativeComponents = Queue<String>.from(p.split(relative));
// If the resolved path is absolute relative to `newPath`, that means it's
// on a different drive. We need to canonicalize the entire target of that
// symlink again.
if (p.isAbsolute(relative)) {
// If we've already tried to canonicalize the new path, we've encountered
// a symlink loop. Avoid going infinite by treating the recursive symlink
// as the canonical path.
if (seen.contains(relative)) {
newPath = relative;
} else {
newPath = relativeComponents.removeFirst();
relativeComponents.addAll(components);
components = relativeComponents;
}
continue;
}
// Pop directories off `newPath` if the component links upwards in the
// directory hierarchy.
while (relativeComponents.firstOrNull == '..') {
newPath = p.dirname(newPath);
relativeComponents.removeFirst();
}
// If there's only one component left, [resolveLink] guarantees that it's
// not a link (or is a broken link). We can just add it to `newPath` and
// continue resolving the remaining components.
if (relativeComponents.length == 1) {
newPath = p.join(newPath, relativeComponents.single);
continue;
}
// If we've already tried to canonicalize the new path, we've encountered a
// symlink loop. Avoid going infinite by treating the recursive symlink as
// the canonical path.
final newSubPath = p.join(newPath, p.joinAll(relativeComponents));
if (seen.contains(newSubPath)) {
newPath = newSubPath;
continue;
}
// If there are multiple new components to resolve, add them to the
// beginning of the queue.
relativeComponents.addAll(components);
components = relativeComponents;
}
return newPath;
}
/// Returns the transitive target of [link] (if A links to B which links to C,
/// this will return C).
///
/// If [link] is part of a symlink loop (e.g. A links to B which links back to
/// A), this returns the path to the first repeated link (so
/// `transitiveTarget("A")` would return `"A"` and `transitiveTarget("A")` would
/// return `"B"`).
///
/// This accepts paths to non-links or broken links, and returns them as-is.
String _resolveLink(String link) {
final seen = <String>{};
while (linkExists(link) && seen.add(link)) {
link = p.normalize(p.join(p.dirname(link), Link(link).targetSync()));
}
return link;
}
/// Reads the contents of the text file at [path].
String readTextFile(String path) => File(path).readAsStringSync();
/// Reads the contents of the text file at [path].
/// Returns `null` if the operation fails.
String? tryReadTextFile(String path) {
try {
return readTextFile(path);
} on FileSystemException {
// TODO: Consider handlind file-not-found differently from other exceptions.
return null;
}
}
/// Reads the contents of the text file [file].
Future<String> readTextFileAsync(String file) {
return _descriptorPool.withResource(() => File(file).readAsString());
}
/// Reads the contents of the binary file [file].
Uint8List readBinaryFile(String file) {
log.io('Reading binary file $file.');
final contents = File(file).readAsBytesSync();
log.io('Read ${contents.length} bytes from $file.');
return contents;
}
/// Reads the contents of the binary file [file] as a [Stream].
Stream<List<int>> readBinaryFileAsStream(String file) {
log.io('Reading binary file $file.');
final contents = File(file).openRead();
return contents;
}
/// Creates [file] and writes [contents] to it.
///
/// If [dontLogContents] is `true`, the contents of the file will never be
/// logged.
void writeTextFile(
String file,
String contents, {
bool dontLogContents = false,
Encoding encoding = utf8,
}) {
// Sanity check: don't spew a huge file.
log.io('Writing ${contents.length} characters to text file $file.');
if (!dontLogContents && contents.length < 1024 * 1024) {
log.fine('Contents:\n$contents');
}
deleteIfLink(file);
File(file).writeAsStringSync(contents, encoding: encoding);
}
/// Reads the contents of the binary file [file].
void writeBinaryFile(String file, Uint8List data) {
log.io('Writing ${data.length} bytes to file $file.');
File(file).writeAsBytesSync(data);
}
/// Creates [file] and writes [contents] to it.
///
/// If [dontLogContents] is `true`, the contents of the file will never be
/// logged.
Future<void> writeTextFileAsync(
String file,
String contents, {
bool dontLogContents = false,
Encoding encoding = utf8,
}) async {
// Sanity check: don't spew a huge file.
log.io('Writing ${contents.length} characters to text file $file.');
if (!dontLogContents && contents.length < 1024 * 1024) {
log.fine('Contents:\n$contents');
}
deleteIfLink(file);
await File(file).writeAsString(contents, encoding: encoding);
}
/// Writes [stream] to a new file at path [file].
///
/// Replaces any file already at that path. Completes when the file is done
/// being written.
Future<String> createFileFromStream(Stream<List<int>> stream, String file) {
// TODO(nweiz): remove extra logging when we figure out the windows bot issue.
log.io('Creating $file from stream.');
return _descriptorPool.withResource(() async {
deleteIfLink(file);
await stream.pipe(File(file).openWrite());
log.fine('Created $file from stream.');
return file;
});
}
void _chmod(int mode, String file) {
runProcessSync('chmod', [mode.toRadixString(8), file]);
}
/// Deletes [file] if it's a symlink.
///
/// The [File] class overwrites the symlink targets when writing to a file,
/// which is never what we want, so this delete the symlink first if necessary.
void deleteIfLink(String file) {
if (!linkExists(file)) return;
log.io('Deleting symlink at $file.');
Link(file).deleteSync();
}
/// Ensures that [dir] and all its parent directories exist.
///
/// If they don't exist, creates them.
String ensureDir(String dir) {
Directory(dir).createSync(recursive: true);
return dir;
}
/// Creates a temp directory in [base], whose name will be [prefix] with
/// characters appended to it to make a unique name.
///
/// Returns the path of the created directory.
String createTempDir(String base, String prefix) {
final tempDir = Directory(base).createTempSync(prefix);
log.io('Created temp directory ${tempDir.path}');
return tempDir.path;
}
/// Creates a temp directory in the system temp directory, whose name will be
/// 'pub_' with characters appended to it to make a unique name.
///
/// Returns the path of the created directory.
Future<String> _createSystemTempDir() async {
final tempDir = await Directory.systemTemp.createTemp('pub_');
log.io('Created temp directory ${tempDir.path}');
return tempDir.resolveSymbolicLinksSync();
}
String resolveSymlinksOfDir(String dir) {
return Directory(dir).resolveSymbolicLinksSync();
}
/// Lists the contents of [dir].
///
/// If [recursive] is `true`, lists subdirectory contents (defaults to `false`).
/// If [includeHidden] is `true`, includes files and directories beginning with
/// `.` (defaults to `false`). If [includeDirs] is `true`, includes directories
/// as well as files (defaults to `true`).
///
/// [allowed] is a list of hidden filenames to include even when
/// [includeHidden] is `false`.
///
/// Note that dart:io handles recursive symlinks in an unfortunate way. You
/// end up with two copies of every entity that is within the recursive loop.
/// We originally had our own directory list code that addressed that, but it
/// had a noticeable performance impact. In the interest of speed, we'll just
/// live with that annoying behavior.
///
/// The returned paths are guaranteed to begin with [dir]. Broken symlinks won't
/// be returned.
List<String> listDir(
String dir, {
bool recursive = false,
bool includeHidden = false,
bool includeDirs = true,
Iterable<String> allowed = const <String>[],
}) {
final allowListFilter = createFileFilter(allowed);
// This is used in some performance-sensitive paths and can list many, many
// files. As such, it leans more heavily towards optimization as opposed to
// readability than most code in pub. In particular, it avoids using the path
// package, since re-parsing a path is very expensive relative to string
// operations.
return Directory(dir)
.listSync(recursive: recursive)
.where((entity) {
if (!includeDirs && entity is Directory) return false;
if (entity is Link) return false;
if (includeHidden) return true;
// Using substring here is generally problematic in cases where dir has
// one or more trailing slashes. If you do listDir("foo"), you'll get
// back paths like "foo/bar". If you do listDir("foo/"), you'll get
// "foo/bar" (note the trailing slash was dropped. If you do
// listDir("foo//"), you'll get "foo//bar".
//
// This means if you strip off the prefix, the resulting string may have
// a leading separator (if the prefix did not have a trailing one) or it
// may not. However, since we are only using the results of that to call
// contains() on, the leading separator is harmless.
assert(entity.path.startsWith(dir));
var pathInDir = entity.path.substring(dir.length);
// If the basename is in [allowed], don't count its "/." as making the
// file hidden.
if (allowListFilter.any(pathInDir.contains)) {
final allowedBasename = allowListFilter.firstWhere(
pathInDir.contains,
);
pathInDir = pathInDir.substring(
0,
pathInDir.length - allowedBasename.length,
);
}
if (pathInDir.contains('/.')) return false;
if (!Platform.isWindows) return true;
return !pathInDir.contains('\\.');
})
.map((entity) => entity.path)
.toList();
}
/// Returns whether [dir] exists on the file system.
///
/// This returns `true` for a symlink only if that symlink is unbroken and
/// points to a directory.
bool dirExists(String dir) => Directory(dir).existsSync();
/// Tries to resiliently perform [operation].
///
/// Some file system operations can intermittently fail on Windows because
/// other processes are locking a file. We've seen this with virus scanners
/// when we try to delete or move something while it's being scanned. To
/// mitigate that, on Windows, this will retry the operation a few times if it
/// fails.
///
/// For some operations it makes sense to handle ERROR_DIR_NOT_EMPTY
/// differently. They can pass [ignoreEmptyDir] = `true`.
void _attempt(
String description,
void Function() operation, {
bool ignoreEmptyDir = false,
}) {
if (!Platform.isWindows) {
operation();
return;
}
String? getErrorReason(FileSystemException error) {
// ERROR_ACCESS_DENIED
if (error.osError?.errorCode == 5) {
return 'access was denied';
}
// ERROR_SHARING_VIOLATION
if (error.osError?.errorCode == 32) {
return 'it was in use by another process';
}
// ERROR_DIR_NOT_EMPTY
if (!ignoreEmptyDir && _isDirectoryNotEmptyException(error)) {
return 'of dart-lang/sdk#25353';
}
return null;
}
const maxRetries = 50;
for (var i = 0; i < maxRetries; i++) {
try {
operation();
break;
} on FileSystemException catch (error) {
final reason = getErrorReason(error);
if (reason == null) rethrow;
if (i < maxRetries - 1) {
log.io(
'Pub failed to $description because $reason. '
'Retrying in 50ms.',
);
sleep(const Duration(milliseconds: 50));
} else {
fail(
'Pub failed to $description because $reason.\n'
'This may be caused by a virus scanner or having a file\n'
'in the directory open in another application.',
);
}
}
}
}
/// Deletes whatever's at [path], whether it's a file, directory, or symlink.
///
/// If it's a directory, it will be deleted recursively.
void deleteEntry(String path) {
_attempt('delete entry', () {
if (linkExists(path)) {
log.io('Deleting link $path.');
Link(path).deleteSync();
} else if (dirExists(path)) {
log.io('Deleting directory $path.');
Directory(path).deleteSync(recursive: true);
} else if (fileExists(path)) {
log.io('Deleting file $path.');
File(path).deleteSync();
}
});
}
/// Attempts to delete whatever's at [path], but doesn't throw an exception if
/// the deletion fails.
void tryDeleteEntry(String path) {
try {
deleteEntry(path);
} catch (error, stackTrace) {
log.fine(
'Pub failed to delete $path: $error\n'
'${Chain.forTrace(stackTrace)}',
);
}
}
/// "Cleans" [dir].
///
/// If that directory already exists, it is deleted. Then a new empty directory
/// is created.
void cleanDir(String dir) {
if (entryExists(dir)) deleteEntry(dir);
ensureDir(dir);
}
/// Renames (i.e. moves) the directory [from] to [to].
void renameDir(String from, String to) {
_attempt('rename directory', () {
log.io('Renaming directory $from to $to.');
Directory(from).renameSync(to);
}, ignoreEmptyDir: true);
}
/// Renames directory [from] to [to].
/// If it fails with "destination not empty" we log and continue, assuming
/// another process got there before us.
void tryRenameDir(String from, String to) {
ensureDir(p.dirname(to));
try {
renameDir(from, to);
} on FileSystemException catch (e) {
tryDeleteEntry(from);
if (!_isDirectoryNotEmptyException(e)) {
rethrow;
}
log.fine('''
Destination directory $to already existed.
Assuming a concurrent pub invocation installed it.''');
}
}
void copyFile(String from, String to) {
log.io('Copying "$from" to "$to".');
File(from).copySync(to);
}
void renameFile(String from, String to) {
log.io('Renaming "$from" to "$to".');
File(from).renameSync(to);
}
bool _isDirectoryNotEmptyException(FileSystemException e) {
final errorCode = e.osError?.errorCode;
return
// On Linux rename will fail with either ENOTEMPTY or EEXISTS if directory
// exists: https://man7.org/linux/man-pages/man2/rename.2.html
// ```
// #define ENOTEMPTY 39 /* Directory not empty */
// #define EEXIST 17 /* File exists */
// ```
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/asm-generic/errno-base.h#n21
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/asm-generic/errno.h#n20
(Platform.isLinux && (errorCode == 39 || errorCode == 17)) ||
// On Windows this may fail with ERROR_DIR_NOT_EMPTY or
// ERROR_ALREADY_EXISTS
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
(Platform.isWindows && (errorCode == 145 || errorCode == 183)) ||
// On MacOS rename will fail with ENOTEMPTY if directory exists.
// We also catch EEXIST - perhaps that could also be thrown...
// ```
// #define ENOTEMPTY 66 /* Directory not empty */
// #define EEXIST 17 /* File exists */
// ```
// https://github.com/apple-oss-distributions/xnu/blob/bb611c8fecc755a0d8e56e2fa51513527c5b7a0e/bsd/sys/errno.h#L190
(Platform.isMacOS && (errorCode == 66 || errorCode == 17));
}
/// Creates a new symlink at path [symlink] that points to [target].
///
/// Returns a [Future] which completes to the path to the symlink file.
///
/// If [relative] is true, creates a symlink with a relative path from the
/// symlink to the target. Otherwise, uses the [target] path unmodified.
///
/// Note that on Windows, only directories may be symlinked to.
void createSymlink(String target, String symlink, {bool relative = false}) {
if (relative) {
// Relative junction points are not supported on Windows. Instead, just
// make sure we have a clean absolute path because it will interpret a
// relative path to be relative to the cwd, not the symlink, and will be
// confused by forward slashes.
if (Platform.isWindows) {
target = p.normalize(p.absolute(target));
} else {
// If the directory where we're creating the symlink was itself reached
// by traversing a symlink, we want the relative path to be relative to
// it's actual location, not the one we went through to get to it.
final symlinkDir = canonicalize(p.dirname(symlink));
target = p.normalize(p.relative(target, from: symlinkDir));
}
}
log.fine('Creating $symlink pointing to $target');
Link(symlink).createSync(target);
}
/// Creates a new symlink that creates an alias at [symlink] that points to the
/// `lib` directory of package [target].
///
/// If [target] does not have a `lib` directory, this shows a warning if
/// appropriate and then does nothing.
///
/// If [relative] is true, creates a symlink with a relative path from the
/// symlink to the target. Otherwise, uses the [target] path unmodified.
void createPackageSymlink(
String name,
String target,
String symlink, {
bool isSelfLink = false,
bool relative = false,
}) {
// See if the package has a "lib" directory. If not, there's nothing to
// symlink to.
target = p.join(target, 'lib');
if (!dirExists(target)) return;
log.fine("Creating ${isSelfLink ? "self" : ""}link for package '$name'.");
createSymlink(target, symlink, relative: relative);
}
/// Whether the current process is a pub subprocess being run from a test.
///
/// The "_PUB_TESTING" variable is automatically set for all the test code's
/// invocations of pub.
final bool runningFromTest =
Platform.environment.containsKey('_PUB_TESTING') && _assertionsEnabled;
final bool _assertionsEnabled = () {
try {
assert(false);
// ignore: avoid_catching_errors
} on AssertionError {
return true;
}
return false;
}();
final bool runningFromFlutter =
Platform.environment.containsKey('PUB_ENVIRONMENT') &&
(Platform.environment['PUB_ENVIRONMENT'] ?? '').contains('flutter_cli');
/// A regular expression to match the script path of a pub script running from
/// source in the Dart repo.
final _dartRepoRegExp = RegExp(
r'/third_party/pkg/pub/('
r'bin/pub\.dart'
r'|'
r'test/.*_test\.dart'
r')$',
);
/// Whether pub is running from source in the Dart repo.
///
/// This can happen when running tests against the repo, as well as when
/// building Observatory.
final bool runningFromDartRepo = Platform.script.path.contains(_dartRepoRegExp);
/// The path to the root of the Dart repo.
///
/// This throws a [StateError] if it's called when not running pub from source
/// in the Dart repo.
final String dartRepoRoot =
(() {
if (!runningFromDartRepo) {
throw StateError('Not running from source in the Dart repo.');
}
// Get the URL of the repo root in a way that works when either both
// running as a test or as a pub executable.
final url = Platform.script.replace(
path: Platform.script.path.replaceAll(_dartRepoRegExp, ''),
);
return p.fromUri(url);
})();
/// Displays a message and reads a yes/no confirmation from the user.
///
/// Returns a [Future] that completes to `true` if the user confirms or `false`
/// if they do not.
///
/// This will automatically append " (y/N)?" to the message, so [message]
/// should just be a fragment like, "Are you sure you want to proceed". The
/// default for an empty response, or any response not starting with `y` or `Y`
/// is false.
Future<bool> confirm(String message) async {
final reply = await stdinPrompt('$message (y/N)?');
return RegExp(r'^[yY]').hasMatch(reply);
}
/// Writes [prompt] and reads a line from stdin.
Future<String> stdinPrompt(String prompt, {bool? echoMode}) async {
if (runningFromTest) {
log.message(prompt);
} else {
stdout.write('$prompt ');
}
if (echoMode != null && stdin.hasTerminal) {
final previousEchoMode = stdin.echoMode;
try {
stdin.echoMode = echoMode;
final result = stdin.readLineSync() ?? '';
stdout.write('\n');
return result;
} finally {
stdin.echoMode = previousEchoMode;
}
} else {
return stdin.readLineSync() ?? '';
}
}
/// Returns `true` if [stdout] should be treated as a terminal.
///
/// The detected behaviour can be overridden with the environment variable
/// [EnvironmentKeys.forceTerminalOutput].
bool get terminalOutputForStdout {
final environmentValue =
Platform.environment[EnvironmentKeys.forceTerminalOutput];
if (environmentValue == null || environmentValue == '') {
return stdout.hasTerminal;
} else if (environmentValue == '0') {
return false;
} else if (environmentValue == '1') {
return true;
} else {
throw DataException(
'Environment variable ${EnvironmentKeys.forceTerminalOutput} has '
'unsupported value: $environmentValue.',
);
}
}
/// Flushes the stdout and stderr streams, then exits the program with the given
/// status code.
///
/// This returns a Future that will never complete, since the program will have
/// exited already. This is useful to prevent Future chains from proceeding
/// after you've decided to exit.
Future flushThenExit(int status) {
return Future.wait([
stdout.close(),
stderr.close(),
]).then((_) => exit(status));
}
/// Returns a [EventSink] that pipes all data to [consumer] and a [Future] that
/// will succeed when [EventSink] is closed or fail with any errors that occur
/// while writing.
(EventSink<T> consumerSink, Future done) _consumerToSink<T>(
StreamConsumer<T> consumer,
) {
final controller = StreamController<T>(sync: true);
final done = controller.stream.pipe(consumer);
return (controller.sink, done);
}
/// Spawns and runs the process located at [executable], passing in [args].
///
/// Returns a [Future] that will complete with the results of the process after
/// it has ended.
///
/// The spawned process will inherit its parent's environment variables. If
/// [environment] is provided, that will be used to augment (not replace) the
/// the inherited variables.
Future<StringProcessResult> runProcess(
String executable,
List<String> args, {
String? workingDir,
Map<String, String>? environment,
bool runInShell = false,
Encoding stdoutEncoding = systemEncoding,
Encoding stderrEncoding = systemEncoding,
}) {
ArgumentError.checkNotNull(executable, 'executable');
return _descriptorPool.withResource(() async {
ProcessResult result;
try {
(executable, args) = _sanitizeExecutablePath(
executable,
args,
workingDir: workingDir,
);
result = await Process.run(
executable,
args,
workingDirectory: workingDir,
environment: environment,
runInShell: runInShell,
stdoutEncoding: stdoutEncoding,
stderrEncoding: stderrEncoding,
);
} on IOException catch (e) {
throw RunProcessException(
'Pub failed to run subprocess `$executable`: $e',
);
}
log.processResult(executable, result);
return StringProcessResult(
result.stdout as String,
result.stderr as String,
result.exitCode,
);
});
}
/// Spawns the process located at [executable], passing in [args].
///
/// Returns a [Future] that will complete with the [Process] once it's been
/// started.
///
/// The spawned process will inherit its parent's environment variables. If
/// [environment] is provided, that will be used to augment (not replace) the
/// the inherited variables.
@visibleForTesting
Future<PubProcess> startProcess(
String executable,
List<String> args, {
String? workingDir,
Map<String, String>? environment,
bool runInShell = false,
}) {
return _descriptorPool.request().then((resource) async {
Process ioProcess;
try {
(executable, args) = _sanitizeExecutablePath(
executable,
args,
workingDir: workingDir,
);
ioProcess = await Process.start(
executable,
args,
workingDirectory: workingDir,
environment: environment,
runInShell: runInShell,
);
} on IOException catch (e) {
throw RunProcessException(
'Pub failed to run subprocess `$executable`: $e',
);
}
final process = PubProcess(ioProcess);
unawaited(process.exitCode.whenComplete(resource.release));
return process;
});
}
/// Like [runProcess], but synchronous.
StringProcessResult runProcessSync(
String executable,
List<String> args, {
String? workingDir,
Map<String, String>? environment,
bool runInShell = false,
Encoding stdoutEncoding = systemEncoding,
Encoding stderrEncoding = systemEncoding,
}) {
ArgumentError.checkNotNull(executable, 'executable');
ProcessResult result;
try {
(executable, args) = _sanitizeExecutablePath(
executable,
args,
workingDir: workingDir,
);
result = Process.runSync(
executable,
args,
workingDirectory: workingDir,
environment: environment,
runInShell: runInShell,
stdoutEncoding: stdoutEncoding,
stderrEncoding: stderrEncoding,
);
} on IOException catch (e) {
throw RunProcessException('Pub failed to run subprocess `$executable`: $e');
}
log.processResult(executable, result);
return StringProcessResult(
result.stdout as String,
result.stderr as String,
result.exitCode,
);
}
/// Like [runProcess], but synchronous.
/// Always outputs stdout as `List<int>`.
BytesProcessResult runProcessSyncBytes(
String executable,
List<String> args, {
String? workingDir,
Map<String, String>? environment,
bool runInShell = false,
Encoding stderrEncoding = systemEncoding,
}) {
ProcessResult result;
try {
(executable, args) = _sanitizeExecutablePath(
executable,
args,
workingDir: workingDir,
);
result = Process.runSync(
executable,
args,
workingDirectory: workingDir,
environment: environment,
runInShell: runInShell,
stdoutEncoding: null,
stderrEncoding: stderrEncoding,
);
} on IOException catch (e) {
throw RunProcessException('Pub failed to run subprocess `$executable`: $e');
}
log.processResult(executable, result);
return BytesProcessResult(
result.stdout as List<int>,
result.stderr as String,
result.exitCode,
);
}
/// Adaptation of ProcessResult when stdout is a `List<String>`.
class StringProcessResult {
final String stdout;
final String stderr;
final int exitCode;
StringProcessResult(this.stdout, this.stderr, this.exitCode);
bool get success => exitCode == exit_codes.SUCCESS;
}
/// Adaptation of ProcessResult when stdout is a `List<bytes>`.
class BytesProcessResult {
final Uint8List stdout;
final String stderr;
final int exitCode;
BytesProcessResult(List<int> stdout, this.stderr, this.exitCode)
: // Not clear that we need to do this, but seems harmless.
stdout = stdout is Uint8List ? stdout : Uint8List.fromList(stdout);
bool get success => exitCode == exit_codes.SUCCESS;
}
/// A wrapper around [Process] that exposes `dart:async`-style APIs.
class PubProcess {
/// The underlying `dart:io` [Process].
final Process _process;
/// The sink used for passing data to the process's standard input stream.
///
/// Errors on this stream are surfaced through [stdinClosed], [stdout],
/// [stderr], and [exitCode], which are all members of an [ErrorGroup].
final EventSink<List<int>> stdin;
// TODO(nweiz): write some more sophisticated Future machinery so that this
// doesn't surface errors from the other streams/futures, but still passes its
// unhandled errors to them. Right now it's impossible to recover from a stdin
// error and continue interacting with the process.
/// A [Future] that completes when [stdin] is closed, either by the user or by
/// the process itself.
///
/// This is in an [ErrorGroup] with [stdout], [stderr], and [exitCode], so any
/// error in process will be passed to it, but won't reach the top-level error
/// handler unless nothing has handled it.
final Future stdinClosed;
/// The process's standard output stream.
///
/// This is in an [ErrorGroup] with [stdinClosed], [stderr], and [exitCode],
/// so any error in process will be passed to it, but won't reach the
/// top-level error handler unless nothing has handled it.
final ByteStream stdout;