Skip to content

Commit

Permalink
Merge pull request #42717 from Thevakumar-Luheerathan/fix-ballerina-l…
Browse files Browse the repository at this point in the history
…ang-iss-42106

Warn about corruption in Dependencies.toml.
  • Loading branch information
Thevakumar-Luheerathan authored Jun 11, 2024
2 parents 18deb9f + 0ddd3f5 commit a92dce6
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Set;

import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException;
import static io.ballerina.projects.internal.ProjectDiagnosticErrorCode.CORRUPTED_DEPENDENCIES_TOML;
import static io.ballerina.projects.util.ProjectConstants.DOT;
import static io.ballerina.projects.util.ProjectConstants.TOOL_DIAGNOSTIC_CODE_PREFIX;

Expand Down Expand Up @@ -207,6 +208,13 @@ public void execute(Project project) {
throw createLauncherException("package resolution contains errors");
}

// Add corrupted dependencies toml diagnostic
project.currentPackage().dependencyManifest().diagnostics().diagnostics().forEach(diagnostic -> {
if (diagnostic.diagnosticInfo().code().equals(CORRUPTED_DEPENDENCIES_TOML.diagnosticId())) {
diagnostics.add(diagnostic);
}
});

// Package resolution is successful. Continue compiling the package.
if (project.buildOptions().dumpBuildTime()) {
start = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,31 @@ public void testBuildBalProjectWithDumpRawGraphsFlag() throws IOException {
ProjectUtils.deleteDirectory(projectPath.resolve("target"));
}

@Test(description = "Build a package with corrupted Dependencies.toml file")
public void testBuildWithCorruptedDependenciesToml() throws IOException {
Path projectPath = this.testResources.resolve("corrupted-dependecies-toml-file");
cleanTarget(projectPath);
System.setProperty(USER_DIR_PROPERTY, projectPath.toString());
Path sourcePath = projectPath.resolve("Dependencies-corrupt.toml");
Path destinationPath = projectPath.resolve("Dependencies.toml");
Files.copy(sourcePath, destinationPath);
BuildCommand buildCommand = new BuildCommand(projectPath, printStream, printStream, false);
new CommandLine(buildCommand);
buildCommand.execute();
String buildLog = readOutput(true);
Assert.assertEquals(
buildLog.replaceAll("\r", ""),
getOutput("corrupted-dependencies-toml.txt").replaceAll("\r", ""));
String depContent = Files.readString(projectPath.resolve("Dependencies.toml"), Charset.defaultCharset())
.replace("/r" , "");
String ballerinaShortVersion = RepoUtils.getBallerinaShortVersion();
String corrcetDepContent = Files.readString(projectPath.resolve("Dependencies-corrected.toml"),
Charset.defaultCharset()).replace("/r" , "")
.replace("DIST_VERSION", ballerinaShortVersion);
Assert.assertEquals(depContent, corrcetDepContent);
Files.delete(destinationPath);
}

@Test(description = "Test bir cached project build performance")
public void testBirCachedProjectBuildPerformance() {
Path projectPath = this.testResources.resolve("noClassDefProject");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Compiling source
luheerathan/test1:0.1.0
WARNING [Dependencies.toml:(6:1,18:1)] Detected corrupted Dependencies.toml file. Dependencies will be updated to the latest versions.

Generating executable
target/bin/test1.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Compiling source
luheerathan/test1:0.1.0
WARNING [Dependencies.toml:(6:1,18:1)] Detected corrupted Dependencies.toml file. Dependencies will be updated to the latest versions.

Generating executable
target/bin/test1.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "luheerathan"
name = "test1"
version = "0.1.0"
distribution = "2201.9.0"

[build-options]
observabilityIncluded = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "DIST_VERSION"

[[package]]
org = "luheerathan"
name = "test1"
version = "0.1.0"
modules = [
{org = "luheerathan", packageName = "test1", moduleName = "test1"}
]

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201"

[[package]]
org = "luheerathan"
name = "test1"
version = "0.1.0"
modules = [
{org = "luheerathan", packageName = "test1", moduleName = "test1"}
]

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public function main() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,19 @@ private DependencyManifestBuilder(TomlDocument dependenciesToml,
this.dependenciesToml = Optional.ofNullable(dependenciesToml);
this.packageDescriptor = packageDescriptor;
this.diagnosticList = new ArrayList<>();
this.dependencyManifest = parseAsDependencyManifest();
DependencyManifest parsedDependecyManifest = parseAsDependencyManifest();
if (parsedDependecyManifest.diagnostics().hasErrors()) {
var diagnosticInfo = new DiagnosticInfo(
ProjectDiagnosticErrorCode.CORRUPTED_DEPENDENCIES_TOML.diagnosticId(),
"Detected corrupted 'Dependencies.toml' file. Dependencies will be updated to the latest versions.",
DiagnosticSeverity.WARNING);
var diagnostic = DiagnosticFactory.createDiagnostic(diagnosticInfo,
this.dependenciesToml.get().toml().rootNode().location());
this.dependencyManifest = DependencyManifest.from(null, null,
Collections.emptyList(), Collections.emptyList(), new DefaultDiagnosticResult(List.of(diagnostic)));
} else {
this.dependencyManifest = parsedDependecyManifest;
}
}

public static DependencyManifestBuilder from(TomlDocument dependenciesToml,
Expand Down
Loading

0 comments on commit a92dce6

Please sign in to comment.