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

[Improvement]: Allow passing objects to Compiler Plugin Context from Ballerina tools #42338

Closed
Xenowa opened this issue Mar 18, 2024 · 4 comments
Labels
Area/ProjectAPI Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Improvement

Comments

@Xenowa
Copy link

Xenowa commented Mar 18, 2024

Description

Currently, it is not possible to pass context objects created from Ballerina tools to compiler plugins.

Describe your problem(s)

Having an API for passing a context from Ballerina tools to compiler plugins allows for extending the functionalities of the tools. This is particularly useful for the bal scan tool. By providing a single scanner context created by the scan tool, compiler plugins can directly report issues through this context. The scan tool can then seamlessly retrieve and report these analysis issues.

Currently, to achieve this functionality, a write of the analysis issues to a file from the compiler plugin side and a read from the scan tool side are required.

Describe your solution(s)

Introduce an API that allows the user to pass custom arguments to the compiler plugin context from the project API.

@CommandLine.Command(name = "scan")
public class ScanCommand implements BLauncherCmd {
    private Project project;

    public ScanCommand() {
        Path projectPath = Paths.get(System.getProperty(ProjectConstants.USER_DIR));
        this.project = ProjectLoader.loadProject(projectPath);
    }    

    @Override
    public void execute() {
        ScannerContext scannerContext = new ScannerContext();
        
        // This can be a list of key(string)-value(object) pairs.
        project.setCompilerPluginParam("scannerContext", scannerContext);
    }

These arguments can then be accessed via compiler plugins as follows.

public class CustomScannerPlugin extends CompilerPlugin {
    @Override
    public void init(CompilerPluginContext pluginContext) {
        // Access the passed object from the compiler plugin side
        ScannerContext scannerContext = (ScannerContext) pluginContext.getCompilerPluginParam("scannerContext");

        // Perform analysis with scanner context and report issues
        // ...
    }
}

This would simplify the process by eliminating unnecessary file I/O, and enable more efficient and streamlined communication between the scan tool and its compiler plugins.

Related area

-> Other Area

Related issue(s) (optional)

Issue 42121

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@ballerina-bot ballerina-bot added the needTriage The issue has to be inspected and labeled manually label Mar 18, 2024
@MaryamZi
Copy link
Member

The basic requirement is to be able to make something available from a Ballerina Tool to compiler plugins.

With recent changes, io.ballerina.projects.plugins.CompilerPluginContext#userData seems to be available for compiler plugins to share data, but there's no way to add something to this via a tool, before the compiler plugins are engaged (also done by the tool).

@MaryamZi MaryamZi added Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Area/ProjectAPI and removed needTriage The issue has to be inspected and labeled manually labels Mar 20, 2024
@Xenowa
Copy link
Author

Xenowa commented Mar 20, 2024

+1, a way to get values set to compiler plugins from the compiler plugins side is already available as:

public class CustomScannerPlugin extends CompilerPlugin {
    @Override
    public void init(CompilerPluginContext pluginContext) {
        // Access the passed object from the compiler plugin side (Feature already available)
        ScannerContext scannerContext = (ScannerContext) pluginContext.userData().get("scannerContext");

        // Perform analysis with scanner context and report issues
        // ...
    }
}

However, there is no way to add something from the tool side which can then be accessed via the compiler plugin side.

@azinneera
Copy link
Contributor

The basic requirement is to be able to make something available from a Ballerina Tool to compiler plugins.

With recent changes, io.ballerina.projects.plugins.CompilerPluginContext#userData seems to be available for compiler plugins to share data, but there's no way to add something to this via a tool, before the compiler plugins are engaged (also done by the tool).

The following API is available to add data to the cache before the compilation.

public void putData(String key, Map<String, Object> value) {

Usage:

project.projectEnvironmentContext().getService(CompilerPluginCache.class).putData(<FQCN>, <value>)

Note that the key should be the canonical name of the compiler plugin class.

@Xenowa
Copy link
Author

Xenowa commented Mar 20, 2024

The basic requirement is to be able to make something available from a Ballerina Tool to compiler plugins.
With recent changes, io.ballerina.projects.plugins.CompilerPluginContext#userData seems to be available for compiler plugins to share data, but there's no way to add something to this via a tool, before the compiler plugins are engaged (also done by the tool).

The following API is available to add data to the cache before the compilation.

public void putData(String key, Map<String, Object> value) {

Usage:

project.projectEnvironmentContext().getService(CompilerPluginCache.class).putData(<FQCN>, <value>)

Note that the key should be the canonical name of the compiler plugin class.

Tested this solution, and it allows passing a context from the scan tool side to be accessible from the compiler plugin side, however due to the issue #42312 it is currently not possible to cast the object back to its original type from the compiler plugins end.

@Xenowa Xenowa closed this as completed Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/ProjectAPI Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Improvement
Projects
None yet
Development

No branches or pull requests

4 participants