Skip to content

Commit df48d2f

Browse files
committed
Rewrite any URIs to canonical, not only file://.
Change-Id: I690154d54818ec576908563ea6f3ed76799be951 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/222240 Reviewed-by: Samuel Rawlins <[email protected]>
1 parent ad9e6d2 commit df48d2f

File tree

7 files changed

+83
-21
lines changed

7 files changed

+83
-21
lines changed

pkg/analyzer/lib/src/dart/analysis/file_state.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ class FileSystemState {
873873

874874
File resource = _resourceProvider.getFile(path);
875875

876-
var rewrittenUri = rewriteFileToPackageUri(_sourceFactory, uri);
876+
var rewrittenUri = rewriteToCanonicalUri(_sourceFactory, uri);
877877
if (rewrittenUri == null) {
878878
return Either2.t1(null);
879879
}

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class LibraryAnalyzer {
783783
}
784784

785785
var absoluteUri = resolveRelativeUri(_library.uri, relativeUri);
786-
return rewriteFileToPackageUri(_sourceFactory, absoluteUri);
786+
return rewriteToCanonicalUri(_sourceFactory, absoluteUri);
787787
}
788788

789789
/// Return the result of resolve the given [uriContent], reporting errors

pkg/analyzer/lib/src/summary2/element_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
10091009
var absoluteUri = resolveRelativeUri(_libraryBuilder.uri, relativeUri);
10101010

10111011
var sourceFactory = _linker.analysisContext.sourceFactory;
1012-
return rewriteFileToPackageUri(sourceFactory, absoluteUri);
1012+
return rewriteToCanonicalUri(sourceFactory, absoluteUri);
10131013
}
10141014

10151015
LibraryElement? _selectLibrary(NamespaceDirective node) {

pkg/analyzer/lib/src/util/uri.dart

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,10 @@ String fileUriToNormalizedPath(Context context, Uri fileUri) {
1313
return path;
1414
}
1515

16-
/// If the [absoluteUri] is a `file` URI that has corresponding `package` URI,
17-
/// return it. If the URI is not valid, e.g. has empty path segments, so
18-
/// does not represent a valid file path, return `null`.
19-
Uri? rewriteFileToPackageUri(SourceFactory sourceFactory, Uri absoluteUri) {
20-
// Only file URIs get rewritten into package URIs.
21-
if (!absoluteUri.isScheme('file')) {
22-
return absoluteUri;
23-
}
24-
25-
// It must be a valid URI, e.g. `file:///home/` is not.
26-
var pathSegments = absoluteUri.pathSegments;
27-
if (pathSegments.isEmpty || pathSegments.last.isEmpty) {
28-
return null;
29-
}
30-
31-
// We ask for Source only because `restoreUri` needs it.
32-
// TODO(scheglov) Add more direct way to convert a path to URI.
16+
/// Return the canonical URI for the given [absoluteUri], for example a `file`
17+
/// URI to the corresponding `package` URI. If the URI is not valid, so does
18+
/// not represent a valid file path, return `null`.
19+
Uri? rewriteToCanonicalUri(SourceFactory sourceFactory, Uri absoluteUri) {
3320
var source = sourceFactory.forUri2(absoluteUri);
3421
if (source == null) {
3522
return null;

pkg/analyzer/test/src/dart/analysis/driver_test.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ import 'package:test_reflective_loader/test_reflective_loader.dart';
2828

2929
import '../../../util/element_type_matchers.dart';
3030
import '../../../utils.dart';
31+
import '../resolution/context_collection_resolution.dart';
3132
import 'base.dart';
3233

3334
main() {
3435
defineReflectiveSuite(() {
3536
defineReflectiveTests(AnalysisDriverSchedulerTest);
3637
defineReflectiveTests(AnalysisDriverTest);
38+
defineReflectiveTests(AnalysisDriver_BazelWorkspaceTest);
3739
});
3840
}
3941

@@ -49,6 +51,59 @@ Future pumpEventQueue([int times = 5000]) {
4951
return Future.delayed(Duration.zero, () => pumpEventQueue(times - 1));
5052
}
5153

54+
@reflectiveTest
55+
class AnalysisDriver_BazelWorkspaceTest extends BazelWorkspaceResolutionTest {
56+
void test_nestedLib_notCanonicalUri() async {
57+
var outerLibPath = '$workspaceRootPath/my/outer/lib';
58+
59+
var innerPath = convertPath('$outerLibPath/inner/lib/b.dart');
60+
var innerUri = Uri.parse('package:my.outer.lib.inner/b.dart');
61+
newFile(innerPath, content: 'class B {}');
62+
63+
var analysisSession = contextFor(innerPath).currentSession;
64+
65+
void assertInnerUri(ResolvedUnitResult result) {
66+
var innerLibrary = result.libraryElement.importedLibraries
67+
.where((e) => e.source.fullName == innerPath)
68+
.single;
69+
expect(innerLibrary.source.uri, innerUri);
70+
}
71+
72+
// Reference "inner" using a non-canonical URI.
73+
{
74+
var path = convertPath('$outerLibPath/a.dart');
75+
newFile(path, content: r'''
76+
import 'inner/lib/b.dart';
77+
''');
78+
var result = await analysisSession.getResolvedUnit(path);
79+
result as ResolvedUnitResult;
80+
assertInnerUri(result);
81+
}
82+
83+
// Reference "inner" using the canonical URI, via relative.
84+
{
85+
var path = '$outerLibPath/inner/lib/c.dart';
86+
newFile(path, content: r'''
87+
import 'b.dart';
88+
''');
89+
var result = await analysisSession.getResolvedUnit(path);
90+
result as ResolvedUnitResult;
91+
assertInnerUri(result);
92+
}
93+
94+
// Reference "inner" using the canonical URI, via absolute.
95+
{
96+
var path = '$outerLibPath/inner/lib/d.dart';
97+
newFile(path, content: '''
98+
import '$innerUri';
99+
''');
100+
var result = await analysisSession.getResolvedUnit(path);
101+
result as ResolvedUnitResult;
102+
assertInnerUri(result);
103+
}
104+
}
105+
}
106+
52107
@reflectiveTest
53108
class AnalysisDriverSchedulerTest with ResourceProviderMixin {
54109
final ByteStore byteStore = MemoryByteStore();

pkg/analyzer/test/src/dart/analysis/file_state_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ class FileSystemState_BazelWorkspaceTest extends BazelWorkspaceResolutionTest {
9292
expect(generatedFile.path, generatedPath);
9393
expect(writableFile2, same(generatedFile));
9494
}
95+
96+
void test_getFileForUri_nestedLib_notCanonicalUri() async {
97+
var outerPath = convertPath('$workspaceRootPath/my/outer/lib/a.dart');
98+
var outerUri = Uri.parse('package:my.outer/a.dart');
99+
100+
var innerPath = convertPath('/workspace/my/outer/lib/inner/lib/b.dart');
101+
var innerUri = Uri.parse('package:my.outer.lib.inner/b.dart');
102+
103+
var analysisDriver = driverFor(outerPath);
104+
var fsState = analysisDriver.fsState;
105+
106+
// User code might use such relative URI.
107+
var innerUri2 = outerUri.resolve('inner/lib/b.dart');
108+
expect(innerUri2, Uri.parse('package:my.outer/inner/lib/b.dart'));
109+
110+
// However the returned file must use the canonical URI.
111+
var innerFile = fsState.getFileForUri(innerUri2).t1!;
112+
expect(innerFile.path, innerPath);
113+
expect(innerFile.uri, innerUri);
114+
}
95115
}
96116

97117
@reflectiveTest

pkg/analyzer/test/src/summary/resynthesize_ast2_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ abstract class ResynthesizeAst2Test extends AbstractResynthesizeTest
230230
}
231231

232232
var absoluteUri = resolveRelativeUri(source.uri, relativeUri);
233-
var rewrittenUri = rewriteFileToPackageUri(sourceFactory, absoluteUri);
233+
var rewrittenUri = rewriteToCanonicalUri(sourceFactory, absoluteUri);
234234
if (rewrittenUri == null) {
235235
return;
236236
}

0 commit comments

Comments
 (0)