Skip to content
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

Use package:_macro_host in _analyzer_macros. #28

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

davidmorgan
Copy link
Contributor

This now actually builds and runs the test macro via a host injected into the analyer.

@davidmorgan davidmorgan force-pushed the end-to-end branch 2 times, most recently from 7d9bb52 to 175c819 Compare August 7, 2024 11:50
import 'package:macros/src/executor.dart' as injected;

/// Injected macro implementation for the analyzer.
class MacroImplementation implements injected.MacroImplementation, HostService {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the flip side here, I would call this AnalyzerMacroImplementation, to avoid duplicate names. Same for all those below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/// Injected macro implementation for the analyzer.
class MacroImplementation implements injected.MacroImplementation, HostService {
final Uri packageConfig;
final Map<String, String> macroImplByName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can infer the keys are names (probably class names), but I don't know what the values are. Can you add a comment describing this a bit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's described in the comment where it's passed in to start: value are fully qualified name of the implementation.

This will anyway go away in the next PR when the placeholder pubspec reader is used :)

required Map<String, String> macroImplByName,
}) async {
final result = MacroImplementation._(packageConfig, macroImplByName);
result._host = await MacroHost.serve(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this pattern is again repeated a bunch across these implementations. I will stop harping on it but something about the design here definitely seems off, that we require so much late initialization and cyclic dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably always correct to refactor, so please do keep mentioning :) thanks.

Here again it's due to the class doing two things; moved the host service to a separate class.

}

class MacroPackageConfigs implements injected.MacroPackageConfigs {
final MacroImplementation impl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

MacroPackageConfigs(this.impl);

@override
bool isMacro(Uri uri, String name) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit weird that this is just a wrapper which calls a function by the same name, but which takes slightly different args. Can we unify the signatures? Similar patterns below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because the injected API assumes the package config URI is known from context, but the _macro_host API wants it passed in.

We could update _macro_host API to match, also assuming it from context, but it's not clear to me yet what the scope of one MacroHost will be, it only makes sense if it's going to be for a single package config.

So, maybe :)


RunningMacro._(this.impl, this.name, this.implementation);

static RunningMacro run(MacroImplementation impl, QualifiedName name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this instead return a Future<RunningMacro> and we can drop the _started field and the awaits below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeeeah that's what I had originally, but the RunningMacro method that gets injected in the analyzer is sync so this has to be sync. I added a TODO to consider changing that upstream :)

}
}

/// Converts [AugmentResponse] to [injected.MacroExecutionResult].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have TODOs for the empty responses here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is basically one big TODO :) added at the top.

@davidmorgan davidmorgan merged commit db556ad into dart-lang:main Aug 8, 2024
39 checks passed
@davidmorgan davidmorgan deleted the end-to-end branch August 8, 2024 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants