-
Notifications
You must be signed in to change notification settings - Fork 14
Support compiling multi-module packages #161
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
Merged
warunalakshitha
merged 8 commits into
ballerina-platform:main
from
azinneera:prj_api-m3
Feb 24, 2026
+870
−169
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a75407
Parallelize syntax tree parsing
azinneera dd9799d
Refactor module dependency resolution
azinneera 68d97fd
Support compiling multi-module packages
azinneera 74b167c
Address review suggestions
azinneera c2e148a
Rebase with upstream/main
azinneera 118c081
Optimize cycle extraction in DependencyGraph to O(1)
azinneera d00acad
Fix errors after rebasing with upstream
azinneera eba75f5
Rename compiler phase functions
azinneera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,3 +32,6 @@ go.work.sum | |
| corpus/**/*.json | ||
| parser/testdata/**/*.json | ||
| /corpus/parser/* | ||
|
|
||
| # IDE specific files | ||
| .idea/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package projects | ||
|
|
||
| import ( | ||
| "maps" | ||
| "slices" | ||
| "sync" | ||
| ) | ||
|
|
||
| // DependencyGraph represents a directed graph of dependencies between nodes. | ||
| // It supports topological sorting, cycle detection, and dependency traversal. | ||
| type DependencyGraph[T comparable] struct { | ||
| rootNode *T | ||
| dependencies map[T]map[T]struct{} | ||
| topologicallySorted []T | ||
| cyclicDependencies [][]T | ||
| sortOnce sync.Once | ||
| } | ||
|
|
||
| // nodes returns all nodes in the graph. | ||
| func (g *DependencyGraph[T]) nodes() []T { | ||
| return slices.Collect(maps.Keys(g.dependencies)) | ||
| } | ||
|
|
||
| // DirectDependencies returns the direct dependencies of the given node. | ||
| // Returns nil if the node does not exist in the graph. | ||
| func (g *DependencyGraph[T]) DirectDependencies(node T) []T { | ||
| deps, ok := g.dependencies[node] | ||
| if !ok { | ||
| return nil | ||
| } | ||
| return slices.Collect(maps.Keys(deps)) | ||
| } | ||
|
|
||
| // ToTopologicallySortedList returns nodes in dependency order (dependencies first). | ||
| // The result is computed lazily and cached for subsequent calls. | ||
| func (g *DependencyGraph[T]) ToTopologicallySortedList() []T { | ||
| g.ensureSorted() | ||
| return slices.Clone(g.topologicallySorted) | ||
| } | ||
|
|
||
| // FindCycles detects and returns any cycles in the graph. | ||
| // Each cycle is represented as a slice of nodes forming the cycle. | ||
| // Returns nil if no cycles exist. | ||
| func (g *DependencyGraph[T]) FindCycles() [][]T { | ||
| g.ensureSorted() | ||
| if len(g.cyclicDependencies) == 0 { | ||
| return nil | ||
| } | ||
| result := make([][]T, len(g.cyclicDependencies)) | ||
| for i, cycle := range g.cyclicDependencies { | ||
| result[i] = slices.Clone(cycle) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func (g *DependencyGraph[T]) ensureSorted() { | ||
| g.sortOnce.Do(func() { | ||
| g.topologicallySorted, g.cyclicDependencies = g.computeTopologicalSort() | ||
| }) | ||
| } | ||
|
|
||
| // computeTopologicalSort performs DFS-based topological sort on the graph. | ||
| // Returns nodes in dependency order (dependencies before dependents) | ||
| // and any cycles detected. | ||
| func (g *DependencyGraph[T]) computeTopologicalSort() ([]T, [][]T) { | ||
warunalakshitha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| nodes := g.nodes() | ||
| visited := make(map[T]bool, len(nodes)) | ||
| stackPos := make(map[T]int, len(nodes)) | ||
| var stack []T | ||
| sorted := make([]T, 0, len(nodes)) | ||
| var cycles [][]T | ||
|
|
||
| var visit func(vertex T) | ||
| visit = func(vertex T) { | ||
| stackPos[vertex] = len(stack) | ||
| stack = append(stack, vertex) | ||
|
|
||
| for dep := range g.dependencies[vertex] { | ||
| if pos, inStack := stackPos[dep]; inStack { | ||
| // Found a cycle - extract it from the stack | ||
| cycles = append(cycles, slices.Clone(stack[pos:])) | ||
| } else if !visited[dep] { | ||
| visit(dep) | ||
| } | ||
| } | ||
| // Post-order: add to sorted list after processing all dependencies | ||
| sorted = append(sorted, vertex) | ||
| visited[vertex] = true | ||
| delete(stackPos, vertex) | ||
| stack = stack[:len(stack)-1] | ||
| } | ||
|
|
||
| for _, node := range nodes { | ||
| if !visited[node] { | ||
| visit(node) | ||
| } | ||
| } | ||
|
|
||
| return sorted, cycles | ||
| } | ||
|
|
||
| type dependencyGraphBuilder[T comparable] struct { | ||
| rootNode *T | ||
| dependencies map[T]map[T]struct{} | ||
| } | ||
|
|
||
| func newDependencyGraphBuilder[T comparable]() *dependencyGraphBuilder[T] { | ||
| return &dependencyGraphBuilder[T]{ | ||
| dependencies: make(map[T]map[T]struct{}), | ||
| } | ||
| } | ||
|
|
||
| func (b *dependencyGraphBuilder[T]) addNode(node T) *dependencyGraphBuilder[T] { | ||
| b.ensureNode(node) | ||
| return b | ||
| } | ||
|
|
||
| func (b *dependencyGraphBuilder[T]) addDependency(from, to T) *dependencyGraphBuilder[T] { | ||
| // Both nodes are added to the graph if they don't exist. | ||
| b.ensureNode(from) | ||
| b.ensureNode(to) | ||
| b.dependencies[from][to] = struct{}{} | ||
| return b | ||
| } | ||
|
|
||
| // build creates the immutable DependencyGraph from the builder's current state. | ||
| // The builder can continue to be used after build is called. | ||
| func (b *dependencyGraphBuilder[T]) build() *DependencyGraph[T] { | ||
| cloned := make(map[T]map[T]struct{}, len(b.dependencies)) | ||
| for k, v := range b.dependencies { | ||
| cloned[k] = maps.Clone(v) | ||
| } | ||
|
|
||
| var rootCopy *T | ||
| if b.rootNode != nil { | ||
| r := *b.rootNode | ||
| rootCopy = &r | ||
| } | ||
|
|
||
| return &DependencyGraph[T]{ | ||
| rootNode: rootCopy, | ||
| dependencies: cloned, | ||
| } | ||
| } | ||
|
|
||
| func (b *dependencyGraphBuilder[T]) ensureNode(node T) { | ||
| if _, ok := b.dependencies[node]; !ok { | ||
| b.dependencies[node] = make(map[T]struct{}) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package projects | ||
|
|
||
| // This file exports internal types for testing purposes only. | ||
| // It is only compiled during test runs. | ||
|
|
||
| // TopologicallySortedModuleNames returns module names in dependency order for testing. | ||
| func (r *PackageResolution) TopologicallySortedModuleNames() []string { | ||
| names := make([]string, len(r.topologicallySortedModuleList)) | ||
| for i, modCtx := range r.topologicallySortedModuleList { | ||
| names[i] = modCtx.getModuleName().String() | ||
| } | ||
| return names | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.