ballerina-platform / ballerina-lang

The Ballerina Programming Language

Home Page:https://ballerina.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[New Feature]: Introduce a new CLI command/ flag `watch`, that restarts a Ballerina project when the source code changes

gayaldassanayake opened this issue · comments

Description

Introduce a new CLI command (bal watch) or a flag (bal run --watch) to restart a running Ballerina service automatically, based on the file changes by a user.

Describe your problem(s)

Currently, when a user runs a service and a code change happens, the user needs to kill the bal run and retrigger it. This has to be done every time the code changes, which is cumbersome.

Describe your solution(s)

We can implement a file listener, that listens to any source code changes. This gets started with the command/ passed flag. It spawns a new process that runs bal run internally. If any change is to happen to the source code, the listener will get hit, the current bal run process will be terminated, and a new bal run will be executed.

Improvements

  • Extend the restart feature to programs that exit cleanly, without any services that hang indefinitely.
  • Allow manual restarting, even when no code changes occur. This eliminates the requirement for users to terminate the command and rerun when required.
  • Introducing a table in Ballerina.toml, if passing configs to configure the restarting process is needed.

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

This can be implemented by utilizing the run fast feature. #39939

Had an offline discussion with @manuranga and @LakshanWeerasinghe. The following were the main points.

We can create an LS Client from the CLI side. The LS client and a Language Server initialized with the bal run --watch. The changes in the files should be listened and sent as didChange notifications to the LS. With the run fast feature, it quickly re-builds and runs the package.

Since this caches and uses previous phases the performance should be much higher(than the crudely running the RunCommand for each change. run fast is also capable of not restarting if there are build failures, killing the previous build and auto restarting, making the task much easier.

However, we need to finalize how we can reuse an already existing LS instance (If we have opened the project, we already have an LS instance of the project, hence creating another LS instance might be redundant. It was suggested a socket based LSP message passing would allow this scenario (by having multiple clients per one server).

Here is a summary of the finalized design by @azinneera for the live reload feature.

Goal

The primary goal of the Ballerina Live Reload feature is to enhance the developer experience by introducing an automated mechanism that monitors code changes in real-time, triggers recompilation, and seamlessly reloads the Ballerina service. This aims to provide clear and concise feedback on code changes immediately.

This feature does not apply to applications with a main function.

Developer Experience

Run

The application can enable live-reload mode using the existing "run” codeLens. Users can disable live-reload with a setting provided in the VSCode.

Debug

Debugging the code will by default have live reload enabled. This can be disabled via a configuration.

M1

For the first milestone (U10), we will be implementing,

  1. bal run --watch CLI command
  2. VSCode run CodeLens updated to execute run command with --watch flag.