Skip to content

Commit 4426bc3

Browse files
committed
Avoid changing current directory in dart:io
1 parent e3e63ec commit 4426bc3

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

dwds/test/fixtures/project.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ class TestProject {
2222
late Directory _fixturesCopy;
2323

2424
/// The top level directory in which we run the test server, e.g.
25-
/// "/tmp/_testSound".
25+
/// "/tmp/_testSound/".
2626
String get absolutePackageDirectory =>
27-
p.join(_fixturesCopy.absolute.path, packageDirectory);
27+
// Return it as a directory with a trailing slash.
28+
Directory.fromUri(
29+
_fixturesCopy.absolute.uri.resolve(packageDirectory),
30+
).uri.path;
2831

2932
/// The directory to build and serve, e.g. "example".
3033
String get directoryToServe => p.split(webAssetsPath).first;
@@ -242,9 +245,18 @@ class TestProject {
242245
} on FileSystemException catch (_) {
243246
assert(Platform.isWindows);
244247
// On Windows, the build daemon process might still be accessing the
245-
// working directory, so wait a few seconds and then try again.
246-
await Future.delayed(const Duration(seconds: 5));
247-
_fixturesCopy.deleteSync(recursive: true);
248+
// working directory, so try again with an exponential backoff.
249+
var seconds = 1;
250+
final maxAttempts = 3;
251+
for (var attempt = 0; attempt < maxAttempts; attempt++) {
252+
try {
253+
_fixturesCopy.deleteSync(recursive: true);
254+
break;
255+
} on FileSystemException catch (_) {
256+
await Future.delayed(Duration(seconds: seconds));
257+
seconds *= 2;
258+
}
259+
}
248260
}
249261
}
250262

frontend_server_common/lib/src/asset_server.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TestAssetServer implements AssetReader {
2626
// Fallback to "application/octet-stream" on null which
2727
// makes no claims as to the structure of the data.
2828
static const String _defaultMimeType = 'application/octet-stream';
29+
final Uri _projectDirectory;
2930
final FileSystem _fileSystem;
3031
final HttpServer _httpServer;
3132
final Map<String, Uint8List> _files = {};
@@ -41,6 +42,7 @@ class TestAssetServer implements AssetReader {
4142
this._httpServer,
4243
this._packageUriMapper,
4344
this.internetAddress,
45+
this._projectDirectory,
4446
this._fileSystem,
4547
this._sdkLayout,
4648
) {
@@ -65,6 +67,7 @@ class TestAssetServer implements AssetReader {
6567
/// trace.
6668
static Future<TestAssetServer> start(
6769
String sdkDirectory,
70+
Uri projectDirectory,
6871
FileSystem fileSystem,
6972
String index,
7073
String hostname,
@@ -75,8 +78,8 @@ class TestAssetServer implements AssetReader {
7578
final address = (await InternetAddress.lookup(hostname)).first;
7679
final httpServer = await HttpServer.bind(address, port);
7780
final sdkLayout = TestSdkLayout.createDefault(sdkDirectory);
78-
final server = TestAssetServer(
79-
index, httpServer, packageUriMapper, address, fileSystem, sdkLayout);
81+
final server = TestAssetServer(index, httpServer, packageUriMapper, address,
82+
projectDirectory, fileSystem, sdkLayout);
8083
return server;
8184
}
8285

@@ -94,7 +97,7 @@ class TestAssetServer implements AssetReader {
9497
final headers = <String, String>{};
9598

9699
if (request.url.path.endsWith('.html')) {
97-
final indexFile = _fileSystem.file(index);
100+
final indexFile = _fileSystem.file(_projectDirectory.resolve(index));
98101
if (indexFile.existsSync()) {
99102
headers[HttpHeaders.contentTypeHeader] = 'text/html';
100103
headers[HttpHeaders.contentLengthHeader] =
@@ -244,8 +247,7 @@ class TestAssetServer implements AssetReader {
244247
// If this is a dart file, it must be on the local file system and is
245248
// likely coming from a source map request. The tool doesn't currently
246249
// consider the case of Dart files as assets.
247-
final dartFile =
248-
_fileSystem.file(_fileSystem.currentDirectory.uri.resolve(path));
250+
final dartFile = _fileSystem.file(_projectDirectory.resolve(path));
249251
if (dartFile.existsSync()) {
250252
return dartFile;
251253
}
@@ -255,7 +257,10 @@ class TestAssetServer implements AssetReader {
255257
// The file might have been a package file which is signaled by a
256258
// `/packages/<package>/<path>` request.
257259
if (segments.first == 'packages') {
258-
final resolved = _packageUriMapper.serverPathToResolvedUri(path);
260+
var resolved = _packageUriMapper.serverPathToResolvedUri(path);
261+
if (resolved != null) {
262+
resolved = _projectDirectory.resolveUri(resolved);
263+
}
259264
final packageFile = _fileSystem.file(resolved);
260265
if (packageFile.existsSync()) {
261266
return packageFile;
@@ -311,7 +316,7 @@ class TestAssetServer implements AssetReader {
311316
}
312317

313318
String _parseBasePathFromIndexHtml(String index) {
314-
final file = _fileSystem.file(index);
319+
final file = _fileSystem.file(_projectDirectory.resolve(index));
315320
if (!file.existsSync()) {
316321
throw StateError('Index file $index is not found');
317322
}

frontend_server_common/lib/src/devfs.dart

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,11 @@ class WebDevFS {
5151

5252
final TestSdkLayout sdkLayout;
5353
final CompilerOptions compilerOptions;
54-
late final Directory _savedCurrentDirectory;
5554

5655
Future<Uri> create() async {
57-
_savedCurrentDirectory = fileSystem.currentDirectory;
58-
59-
fileSystem.currentDirectory = projectDirectory.toFilePath();
60-
6156
assetServer = await TestAssetServer.start(
6257
sdkLayout.sdkDirectory,
58+
projectDirectory,
6359
fileSystem,
6460
index,
6561
hostname,
@@ -71,7 +67,6 @@ class WebDevFS {
7167
}
7268

7369
Future<void> dispose() {
74-
fileSystem.currentDirectory = _savedCurrentDirectory;
7570
return assetServer.close();
7671
}
7772

@@ -84,7 +79,8 @@ class WebDevFS {
8479
required bool fullRestart,
8580
}) async {
8681
final mainPath = mainUri.toFilePath();
87-
final outputDirectoryPath = fileSystem.file(mainPath).parent.path;
82+
final outputDirectory = fileSystem.directory(
83+
fileSystem.file(projectDirectory.resolve(mainPath)).parent.path);
8884
final entryPoint = mainUri.toString();
8985

9086
var prefix = '';
@@ -103,7 +99,10 @@ class WebDevFS {
10399
final bootstrap = '${prefix}main_module.bootstrap.js';
104100

105101
assetServer.writeFile(
106-
entryPoint, fileSystem.file(mainPath).readAsStringSync());
102+
entryPoint,
103+
fileSystem
104+
.file(projectDirectory.resolve(mainPath))
105+
.readAsStringSync());
107106
assetServer.writeFile(stackMapper, stackTraceMapper.readAsStringSync());
108107

109108
switch (ddcModuleFormat) {
@@ -199,14 +198,13 @@ class WebDevFS {
199198
File metadataFile;
200199
List<String> modules;
201200
try {
202-
final parentDirectory = fileSystem.directory(outputDirectoryPath);
203201
codeFile =
204-
parentDirectory.childFile('${compilerOutput.outputFilename}.sources');
202+
outputDirectory.childFile('${compilerOutput.outputFilename}.sources');
205203
manifestFile =
206-
parentDirectory.childFile('${compilerOutput.outputFilename}.json');
204+
outputDirectory.childFile('${compilerOutput.outputFilename}.json');
207205
sourcemapFile =
208-
parentDirectory.childFile('${compilerOutput.outputFilename}.map');
209-
metadataFile = parentDirectory
206+
outputDirectory.childFile('${compilerOutput.outputFilename}.map');
207+
metadataFile = outputDirectory
210208
.childFile('${compilerOutput.outputFilename}.metadata');
211209
modules = assetServer.write(
212210
codeFile, manifestFile, sourcemapFile, metadataFile);

frontend_server_common/lib/src/frontend_server_client.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ class ResidentCompiler {
402402
if (compilerOptions.moduleFormat == ModuleFormat.ddc)
403403
'--dartdevc-module-format=ddc'
404404
];
405-
406405
_logger.info(args.join(' '));
407406
final workingDirectory = projectDirectory.toFilePath();
408407
_server = await Process.start(sdkLayout.dartAotRuntimePath, args,
@@ -657,7 +656,7 @@ String _toMultiRootPath(
657656
for (final fileSystemRoot in fileSystemRoots) {
658657
final rootPath = fileSystemRoot.toFilePath(windows: Platform.isWindows);
659658
if (filePath.startsWith(rootPath)) {
660-
return '$scheme://${filePath.substring(rootPath.length)}';
659+
return '$scheme:///${filePath.substring(rootPath.length)}';
661660
}
662661
}
663662
return fileUri.toString();

test_common/lib/utilities.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ const fixturesDirName = 'fixtures';
1212

1313
const newDdcTypeSystemVersion = '3.3.0-242.0.dev';
1414

15-
final _currentDirectory = p.current;
16-
1715
/// The path to the webdev directory in the local machine, e.g.
1816
/// '/workstation/webdev'.
1917
String get webdevPath {
20-
final pathParts = p.split(_currentDirectory);
18+
final pathParts = p.split(p.current);
2119
assert(pathParts.contains(webdevDirName));
2220
return p.joinAll(
2321
pathParts.sublist(0, pathParts.lastIndexOf(webdevDirName) + 1),

0 commit comments

Comments
 (0)