-
Notifications
You must be signed in to change notification settings - Fork 214
Simpler in memory filesystem #3823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davidmorgan
merged 4 commits into
dart-lang:master
from
davidmorgan:simpler-in-memory-filesystem
Feb 6, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
build_resolvers/lib/src/analysis_driver_filesystem.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
// Copyright (c) 2025, 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. | ||
|
||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:analyzer/file_system/file_system.dart'; | ||
import 'package:analyzer/source/file_source.dart'; | ||
// ignore: implementation_imports | ||
import 'package:analyzer/src/clients/build_resolvers/build_resolvers.dart'; | ||
import 'package:build/build.dart' hide Resource; | ||
import 'package:path/path.dart' as p; | ||
|
||
/// The in-memory filesystem that is the analyzer's view of the build. | ||
/// | ||
/// Tracks modified paths, which should be passed to | ||
/// `AnalysisDriver.changeFile` to update the analyzer state. | ||
class AnalysisDriverFilesystem implements UriResolver, ResourceProvider { | ||
final Map<String, String> _data = {}; | ||
final Set<String> _changedPaths = {}; | ||
davidmorgan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Methods for use by `AnalysisDriverModel`. | ||
|
||
/// Whether [path] exists. | ||
bool exists(String path) => _data.containsKey(path); | ||
|
||
/// Reads the data previously written to [path]. | ||
/// | ||
/// Throws if ![exists]. | ||
String read(String path) => _data[path]!; | ||
|
||
/// Deletes the data previously written to [path]. | ||
/// | ||
/// Records the change in [changedPaths]. | ||
/// | ||
/// Or, if it's missing, does nothing. | ||
void deleteFile(String path) { | ||
if (_data.remove(path) != null) _changedPaths.add(path); | ||
} | ||
|
||
/// Writes [content] to [path]. | ||
/// | ||
/// Records the change in [changedPaths], only if the content actually | ||
/// changed. | ||
void writeFile(String path, String content) { | ||
final oldContent = _data[path]; | ||
_data[path] = content; | ||
if (content != oldContent) _changedPaths.add(path); | ||
davidmorgan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Paths that were modified by [deleteFile] or [writeFile] since the last | ||
/// call to [clearChangedPaths]. | ||
Iterable<String> get changedPaths => _changedPaths; | ||
|
||
/// Clears [changedPaths]. | ||
void clearChangedPaths() => _changedPaths.clear(); | ||
|
||
// `UriResolver` methods. | ||
|
||
/// Converts [path] to [Uri]. | ||
/// | ||
/// [path] must be absolute and matches one of two formats: | ||
/// | ||
/// ``` | ||
/// /<package>/lib/<rest> --> package:<package>/<rest> | ||
/// /<package>/<rest> --> asset:<package>/<rest> | ||
/// ``` | ||
@override | ||
Uri pathToUri(String path) { | ||
davidmorgan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!path.startsWith('/')) { | ||
throw ArgumentError.value('path', path, 'Must start with "/". '); | ||
} | ||
final pathSegments = path.split('/'); | ||
// First segment is empty because of the starting `/`. | ||
final packageName = pathSegments[1]; | ||
if (pathSegments[2] == 'lib') { | ||
return Uri( | ||
scheme: 'package', | ||
pathSegments: [packageName].followedBy(pathSegments.skip(3)), | ||
); | ||
} else { | ||
return Uri( | ||
scheme: 'asset', | ||
pathSegments: [packageName].followedBy(pathSegments.skip(2)), | ||
); | ||
} | ||
} | ||
|
||
@override | ||
Source? resolveAbsolute(Uri uri, [Uri? actualUri]) { | ||
final assetId = parseAsset(uri); | ||
if (assetId == null) return null; | ||
|
||
var file = getFile(assetPath(assetId)); | ||
return FileSource(file, assetId.uri); | ||
} | ||
|
||
/// Path of [assetId] for the in-memory filesystem. | ||
static String assetPath(AssetId assetId) => | ||
'/${assetId.package}/${assetId.path}'; | ||
|
||
/// Attempts to parse [uri] into an [AssetId]. | ||
/// | ||
/// Handles 'package:' or 'asset:' URIs, as well as 'file:' URIs that have the | ||
/// same pattern used by [assetPath]. | ||
/// | ||
/// Returns null if the Uri cannot be parsed. | ||
static AssetId? parseAsset(Uri uri) { | ||
if (uri.isScheme('package') || uri.isScheme('asset')) { | ||
return AssetId.resolve(uri); | ||
} | ||
if (uri.isScheme('file')) { | ||
if (!uri.path.startsWith('/')) { | ||
throw ArgumentError.value( | ||
'uri.path', uri.path, 'Must start with "/". '); | ||
} | ||
final parts = uri.path.split('/'); | ||
// First part is empty because of the starting `/`, second is package, | ||
// remainder is path in package. | ||
return AssetId(parts[1], parts.skip(2).join('/')); | ||
} | ||
return null; | ||
} | ||
|
||
// `ResourceProvider` methods. | ||
|
||
@override | ||
p.Context get pathContext => p.posix; | ||
davidmorgan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@override | ||
File getFile(String path) => _Resource(this, path); | ||
|
||
@override | ||
Folder getFolder(String path) => _Resource(this, path); | ||
|
||
// `ResourceProvider` methods that are not needed. | ||
|
||
@override | ||
Link getLink(String path) => throw UnimplementedError(); | ||
|
||
@override | ||
Resource getResource(String path) => throw UnimplementedError(); | ||
|
||
@override | ||
Folder? getStateLocation(String pluginId) => throw UnimplementedError(); | ||
} | ||
|
||
/// Minimal implementation of [File] and [Folder]. | ||
/// | ||
/// Provides only what the analyzer actually uses during analysis. | ||
class _Resource implements File, Folder { | ||
final AnalysisDriverFilesystem filesystem; | ||
@override | ||
final String path; | ||
|
||
_Resource(this.filesystem, this.path); | ||
|
||
@override | ||
bool get exists => filesystem.exists(path); | ||
|
||
@override | ||
String get shortName => filesystem.pathContext.basename(path); | ||
|
||
@override | ||
Uint8List readAsBytesSync() { | ||
// TODO(davidmorgan): the analyzer reads as bytes in `FileContentCache` | ||
// then converts back to `String` and hashes. It should be possible to save | ||
// that work, for example by injecting a custom `FileContentCache`. | ||
return utf8.encode(filesystem.read(path)); | ||
} | ||
|
||
@override | ||
String readAsStringSync() => filesystem.read(path); | ||
|
||
// `Folder` methods. | ||
|
||
@override | ||
bool contains(String path) => | ||
filesystem.pathContext.isWithin(this.path, path); | ||
|
||
// Most `File` and/or `Folder` methods are not needed. | ||
|
||
@override | ||
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | ||
|
||
// Needs an explicit override to satisfy both `File.copyTo` and | ||
// `Folder.copyTo`. | ||
@override | ||
_Resource copyTo(Folder _) => throw UnimplementedError(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch. I did not expect that clients will start implementing
ResourceProvider
.This will potentially complicate making any change to this interface.