Skip to content

Commit 464641f

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Extend BaselPackageUriResolver to handle files outside of lib.
Generated files can exist outside of `lib`. In a Bazel workspace these are referred to using `file:` URIs that point to the generated code directory. We need to be able to resolve references both from the generated code to non-generated code and vice versa. This is blocking the use of the migration tool in Bazel workspaces, since the migration tool doesn't have the same level of error recovery as the rest of the analyzer, so it needs to be able to resolve all files including generated ones. But it should also improve the user experience for using the analysis server in general in Bazel workspaces, by reducing the number of nuisance "URI not resolved" errors. Change-Id: I38dababd29f4490746cc6e1eeede9f438526a815 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153520 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 36e89f8 commit 464641f

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

pkg/analyzer/lib/src/workspace/bazel.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class BazelPackageUriResolver extends UriResolver {
5959
@override
6060
Source resolveAbsolute(Uri uri, [Uri actualUri]) {
6161
return _sourceCache.putIfAbsent(uri, () {
62+
if (uri.scheme == 'file') {
63+
var pathRelativeToRoot = _workspace._relativeToRoot(uri.path);
64+
if (pathRelativeToRoot == null) return null;
65+
var fullFilePath = _context.join(_workspace.root, pathRelativeToRoot);
66+
File file = _workspace.findFile(fullFilePath);
67+
return file?.createSource(uri);
68+
}
6269
if (uri.scheme != 'package') {
6370
return null;
6471
}
@@ -306,6 +313,32 @@ class BazelWorkspace extends Workspace
306313
}
307314
}
308315

316+
String _relativeToRoot(String p) {
317+
path.Context context = provider.pathContext;
318+
// genfiles
319+
if (genfiles != null && context.isWithin(genfiles, p)) {
320+
return context.relative(p, from: genfiles);
321+
}
322+
// bin
323+
for (String bin in binPaths) {
324+
if (context.isWithin(bin, p)) {
325+
return context.relative(p, from: bin);
326+
}
327+
}
328+
// READONLY
329+
if (readonly != null) {
330+
if (context.isWithin(readonly, p)) {
331+
return context.relative(p, from: readonly);
332+
}
333+
}
334+
// Not generated
335+
if (context.isWithin(root, p)) {
336+
return context.relative(p, from: root);
337+
}
338+
// Failed reverse lookup
339+
return null;
340+
}
341+
309342
/// Find the Bazel workspace that contains the given [filePath].
310343
///
311344
/// This method walks up the file system from [filePath], looking for various

pkg/analyzer/test/src/workspace/bazel_test.dart

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,58 @@ class BazelPackageUriResolverTest with ResourceProviderMixin {
130130
exists: true);
131131
}
132132

133+
void test_resolveAbsolute_file_bin_to_genfiles() {
134+
_addResources([
135+
'/workspace/WORKSPACE',
136+
'/workspace/bazel-genfiles/my/foo/test/foo1.dart',
137+
'/workspace/bazel-bin/'
138+
]);
139+
_assertResolve('file:///workspace/bazel-bin/my/foo/test/foo1.dart',
140+
'/workspace/bazel-genfiles/my/foo/test/foo1.dart',
141+
restore: false);
142+
}
143+
144+
void test_resolveAbsolute_file_genfiles_to_workspace() {
145+
_addResources([
146+
'/workspace/WORKSPACE',
147+
'/workspace/bazel-genfiles/',
148+
'/workspace/my/foo/test/foo1.dart'
149+
]);
150+
_assertResolve('file:///workspace/bazel-genfiles/my/foo/test/foo1.dart',
151+
'/workspace/my/foo/test/foo1.dart',
152+
restore: false);
153+
}
154+
155+
void test_resolveAbsolute_file_not_in_workspace() {
156+
_addResources([
157+
'/workspace/WORKSPACE',
158+
'/workspace/bazel-genfiles/',
159+
'/other/my/foo/test/foo1.dart'
160+
]);
161+
_assertNoResolve('file:///other/my/foo/test/foo1.dart');
162+
}
163+
164+
void test_resolveAbsolute_file_readonly_to_workspace() {
165+
_addResources([
166+
'/workspace/WORKSPACE',
167+
'/READONLY/workspace/',
168+
'/workspace/my/foo/test/foo1.dart'
169+
]);
170+
_assertResolve('file:///READONLY/workspace/my/foo/test/foo1.dart',
171+
'/workspace/my/foo/test/foo1.dart',
172+
restore: false);
173+
}
174+
175+
void test_resolveAbsolute_file_workspace_to_genfiles() {
176+
_addResources([
177+
'/workspace/WORKSPACE',
178+
'/workspace/bazel-genfiles/my/foo/test/foo1.dart'
179+
]);
180+
_assertResolve('file:///workspace/my/foo/test/foo1.dart',
181+
'/workspace/bazel-genfiles/my/foo/test/foo1.dart',
182+
restore: false);
183+
}
184+
133185
void test_resolveAbsolute_genfiles() {
134186
_addResources([
135187
'/workspace/WORKSPACE',
@@ -465,6 +517,11 @@ class BazelPackageUriResolverTest with ResourceProviderMixin {
465517
resolver = BazelPackageUriResolver(workspace);
466518
}
467519

520+
void _assertNoResolve(String uriStr) {
521+
var uri = Uri.parse(uriStr);
522+
expect(resolver.resolveAbsolute(uri), isNull);
523+
}
524+
468525
void _assertResolve(String uriStr, String posixPath,
469526
{bool exists = true, bool restore = true}) {
470527
Uri uri = Uri.parse(uriStr);

0 commit comments

Comments
 (0)