I installed Ballerina Swan Lake 2201.7.2 and created a new Ballerina package using bal new hello command. The main.bal file contained the following code.
import ballerina/io;
public function main() {
io:println("Hello, World!");
}
Ballerina.toml file content was as follows.
[package]
org = "demo"
name = "hello"
version = "0.1.0"
distribution = "2201.7.2"
[build-options]
observabilityIncluded = true
Then I ran the bal run command and it gave the output successfully.
Compiling source
demo/hello:0.1.0
Running executable
Hello, World!
Then I changed my Ballerina Swan Lake version to 2201.6.1 using the bal dist use 2201.6.1 command.
After that, to compile and run the package with Ballerina Swan Lake version 2201.6.1, I used the bal run command. But it gave the following error.
Compiling source
demo/hello:0.1.0
ERROR [main.bal:(1:1,1:21)] cannot resolve module 'ballerina/io'
ERROR [main.bal:(4:5,4:32)] undefined function 'println'
ERROR [main.bal:(4:5,4:32)] undefined module 'io'
error: compilation contains errors
Why is that?
What Ballerina guarantees is that it will continue to support compiling packages using a higher Ballerina version that were already compiled with a lower Ballerina version, given that there are no backward incompatible changes introduced with the new releases. (Usually when there are backward incompatible changes, they are mentioned in the release note.)
There is no guarantee that the Ballerina packages compiled with a higher version of Ballerina will compile with a lower version of Ballerina due to the incompatible dependencies used by the package. For example, the above scenario can be explained as follows.
When compiling the program using Ballerina version
2201.7.2, it creates theDependencies.tomlfile which contains the following.It shows the Ballerina package's dependencies and their minimum supported versions. For example, here Ballerina version
2201.7.2requires at leastioversion1.5.0.When the package is then compiled with Ballerina version
2201.6.1, it reads thisDependencies.tomlfile and check whether thisiomodule version is supported by Ballerina version2201.6.1. When it doesn't it throws the abovecannot resolve module 'ballerina/io'error.As a remedy, what you can do is deleting the
Dependecies.tomlfile created by the Ballerina version2201.7.2, and compile the package using the version2201.6.1. Then it will run the compile and run the code without any errors.You can also see the below
Dependencies.tomlfile created with the version2201.6.1which says the Ballerina version requires at leastioversion1.4.1. Whileioversion1.4.1falls within the compatibleioversion ranges supported by the Ballerina version2201.6.1,ioversion1.5.0is not supported by the Ballerina version2201.6.1. That's why the previous error occurred.