Skip to content

Commit

Permalink
Macro. Pass 'packagesFileUri' when compiling using KernelCompilationS…
Browse files Browse the repository at this point in the history
…ervice.

Change-Id: I2cdb9864a2c824ca568ded1a8b7c2c7b0d74de5d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366402
Reviewed-by: Morgan :) <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
Reviewed-by: Jake Macdonald <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed May 14, 2024
1 parent d45304c commit 3db9fd3
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 17 deletions.
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/dart/analysis/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ class AnalysisDriver {
declaredVariables: declaredVariables,
sourceFactory: _sourceFactory,
macroSupport: macroSupport,
packagesFile: analysisContext?.contextRoot.packagesFile,
externalSummaries: _externalSummaries,
fileSystemState: _fsState,
);
Expand Down
9 changes: 8 additions & 1 deletion pkg/analyzer/lib/src/dart/analysis/library_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:typed_data';
import 'package:analyzer/dart/analysis/declared_variables.dart';
import 'package:analyzer/dart/element/element.dart'
show CompilationUnitElement, LibraryElement;
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/context/context.dart';
import 'package:analyzer/src/dart/analysis/analysis_options_map.dart';
import 'package:analyzer/src/dart/analysis/byte_store.dart';
Expand Down Expand Up @@ -41,6 +42,7 @@ class LibraryContext {
final InfoDeclarationStore infoDeclarationStore;
final FileSystemState fileSystemState;
final MacroSupport? macroSupport;
final File? packagesFile;
final SummaryDataStore store = SummaryDataStore();

late final AnalysisContextImpl analysisContext;
Expand All @@ -59,6 +61,7 @@ class LibraryContext {
required DeclaredVariables declaredVariables,
required SourceFactory sourceFactory,
required this.macroSupport,
required this.packagesFile,
required SummaryDataStore? externalSummaries,
}) {
analysisContext = AnalysisContextImpl(
Expand Down Expand Up @@ -233,14 +236,18 @@ class LibraryContext {

// If we can compile to kernel, check if there are macros.
var macroSupport = this.macroSupport;
if (macroSupport is KernelMacroSupport && macroLibraries.isNotEmpty) {
var packagesFile = this.packagesFile;
if (macroSupport is KernelMacroSupport &&
packagesFile != null &&
macroLibraries.isNotEmpty) {
var kernelBytes = byteStore.get(cycle.macroKey);
if (kernelBytes == null) {
kernelBytes = await performance.runAsync<Uint8List>(
'macroCompileKernel',
(performance) async {
return await macroSupport.builder.build(
fileSystem: _MacroFileSystem(fileSystemState),
packageFilePath: packagesFile.path,
libraries: macroLibraries,
);
},
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/dart/micro/resolve_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ class FileResolver {
sourceFactory: sourceFactory,
externalSummaries: SummaryDataStore(),
macroSupport: null,
packagesFile: null,
testData: testData?.libraryContext,
);

Expand Down
7 changes: 5 additions & 2 deletions pkg/analyzer/lib/src/summary2/kernel_compilation_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class KernelCompilationService {
/// be requested again using [dispose] or [disposeDelayed].
static Future<Uint8List> compile({
required MacroFileSystem fileSystem,
required String packageFilePath,
required String path,
}) {
_disposeDelayTimer?.cancel();
Expand All @@ -101,9 +102,10 @@ class KernelCompilationService {
var instance = await _instance;
var requestChannel = instance.requestChannel;

var pathContext = fileSystem.pathContext;
MacroFileEntry uriStrToFile(Object? uriStr) {
var uri = uriCache.parse(uriStr as String);
var path = fileSystem.pathContext.fromUri(uri);
var path = pathContext.fromUri(uri);
return fileSystem.getFile(path);
}

Expand All @@ -122,7 +124,8 @@ class KernelCompilationService {
// Now we can compile.
return await requestChannel.sendRequest<Uint8List>('kernelForProgram', {
'sdkSummary': 'dill:vm',
'uri': fileSystem.pathContext.toUri(path).toString(),
'packagesFileUri': pathContext.toUri(packageFilePath).toString(),
'uri': pathContext.toUri(path).toString(),
});
});
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/lib/src/summary2/macro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class MacroKernelBuilder {

Future<Uint8List> build({
required MacroFileSystem fileSystem,
required String packageFilePath,
required List<MacroLibrary> libraries,
}) async {
var macroMainContent = macro.bootstrapMacroIsolate(
Expand All @@ -245,6 +246,7 @@ class MacroKernelBuilder {

return KernelCompilationService.compile(
fileSystem: overlayFileSystem,
packageFilePath: packageFilePath,
path: macroMainPath,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,24 @@ class PubPackageResolutionTest extends ContextResolutionTest

String get workspaceRootPath => '/home';

/// Creates `package:macro` and `package:_macro` files, adds to [config].
void addMacrosEnvironment(
PackageConfigFileBuilder config,
MacrosEnvironment macrosEnvironment,
) {
var packagesRootFolder = getFolder(packagesRootPath);
macrosEnvironment.publicMacrosFolder.copyTo(packagesRootFolder);
macrosEnvironment.privateMacrosFolder.copyTo(packagesRootFolder);
config.add(
name: '_macros',
rootPath: getFolder('$packagesRootPath/_macros').path,
);
config.add(
name: 'macros',
rootPath: getFolder('$packagesRootPath/macros').path,
);
}

/// Build summary bundle for a single URI `package:foo/foo.dart`.
Future<File> buildPackageFooSummary({
required Map<String, String> files,
Expand Down Expand Up @@ -512,17 +530,7 @@ class PubPackageResolutionTest extends ContextResolutionTest
}

if (macrosEnvironment != null) {
var packagesRootFolder = getFolder(packagesRootPath);
macrosEnvironment.publicMacrosFolder.copyTo(packagesRootFolder);
macrosEnvironment.privateMacrosFolder.copyTo(packagesRootFolder);
config.add(
name: '_macros',
rootPath: getFolder('$packagesRootPath/_macros').path,
);
config.add(
name: 'macros',
rootPath: getFolder('$packagesRootPath/macros').path,
);
addMacrosEnvironment(config, macrosEnvironment);
}

writePackageConfig(testPackageRootPath, config);
Expand Down
38 changes: 38 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,44 @@ class X {}
]);
}

/// Test that macros are compiled using the packages file of the package
/// that uses the macro.
test_macroInPackage() async {
var myMacroRootPath = '$workspaceRootPath/my_macro';
writePackageConfig(
myMacroRootPath,
PackageConfigFileBuilder()
..add(name: 'my_macro', rootPath: myMacroRootPath),
);

newAnalysisOptionsYamlFile(
myMacroRootPath,
AnalysisOptionsFileConfig(
experiments: experiments,
).toContent(),
);

newFile(
'$myMacroRootPath/lib/append.dart',
getMacroCode('append.dart'),
);

writeTestPackageConfig(
PackageConfigFileBuilder()
..add(name: 'my_macro', rootPath: myMacroRootPath),
macrosEnvironment: MacrosEnvironment.instance,
);

await assertNoErrorsInCode(r'''
import 'package:my_macro/append.dart';
@DeclareType('B', 'class B {}')
class A {}
void f(B b) {}
''');
}

test_withLints() async {
writeTestPackageAnalysisOptionsFile(AnalysisOptionsFileConfig(
lints: ['unnecessary_this'],
Expand Down
11 changes: 8 additions & 3 deletions pkg/analyzer/test/src/summary/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15147,11 +15147,16 @@ elementFactory
}

var otherRootPath = '$workspaceRootPath/other';
var otherPackageConfig = PackageConfigFileBuilder()
..add(name: 'test', rootPath: testPackageRootPath)
..add(name: 'other', rootPath: otherRootPath);
addMacrosEnvironment(
otherPackageConfig,
MacrosEnvironment.instance,
);
writePackageConfig(
otherRootPath,
PackageConfigFileBuilder()
..add(name: 'test', rootPath: testPackageRootPath)
..add(name: 'other', rootPath: otherRootPath),
otherPackageConfig,
);

newAnalysisOptionsYamlFile(
Expand Down

0 comments on commit 3db9fd3

Please sign in to comment.