This guide provides ways to test the Golang analyzer functionality without making API calls, enabling faster development and debugging.
# Run all Golang analyzer tests
go test ./analyzer/stacks/golang/ -v
# Or use the convenience script
./scripts/test-golang-analyzer.sh
# Test main package detection (fixes the "configs" vs "cmd/web-server" issue)
go test ./analyzer/stacks/golang/ -run TestFindMainPackage -v
# Test the specific web server structure that was failing
go test ./analyzer/stacks/golang/ -run TestWebServerProjectStructure -v
# Test full LLB generation pipeline
go test ./analyzer/stacks/golang/ -run TestIntegrationLLBGeneration -v
# Test CGO detection and handling
go test ./analyzer/stacks/golang/ -run TestCGODetection -v
golang_test.go
)TestGolang()
- Name and basic operationsTestAnalyze()
- Detects Go projects vs non-Go projectsTestFindMainPackage()
- Finds correct main package in various structuresTestAnalyzeGoProject()
- Full project analysis including modules, ports, dependenciesTestGenerateLLB()
- Basic LLB generationTestWebServerProjectStructure()
- Specific test for the failing caseintegration_test.go
)TestIntegrationLLBGeneration()
- Complete end-to-end LLB generationTestCGODetection()
- Tests CGO environment variable handlingProblem: System was detecting configs
as main package instead of cmd/web-server
Solution: Enhanced findMainPackage()
to:
main
function in Go files, not just any .go
filesconfigs
cmd/
subdirectoriesTest: TestWebServerProjectStructure()
validates this specific case
Problem: CGO_ENABLED=0 GOOS=linux ...
was being treated as a command instead of environment variables
Solution: Separated environment variables from shell command using llb.AddEnv()
Test: Build commands in integration tests validate proper environment handling
Problem: /app
directory might not exist when copy operations run
Solution: Explicitly create /app
directory before copying files
Test: Integration tests validate the full build pipeline
# Create a test project that mimics the failing structure
mkdir -p /tmp/test-golang-project/{cmd/web-server,configs,pkg/mhttp}
# Create go.mod
echo "module test-web-server
go 1.21" > /tmp/test-golang-project/go.mod
# Create main file
echo "package main
func main() {}" > /tmp/test-golang-project/cmd/web-server/main.go
# Create config file (should NOT be detected as main)
echo "package configs
var Config = map[string]string{}" > /tmp/test-golang-project/configs/server.go
# Test analysis
cd /home/ray/byop/byop-engine
go run -c "
import './analyzer/stacks/golang'
g := &golang.Golang{}
result := g.findMainPackage('/tmp/test-golang-project')
fmt.Printf('Main package: %s\n', result)
"
# Test LLB generation without BuildKit
go test ./analyzer/stacks/golang/ -run TestGenerateLLB -v
# The test will show:
# - LLB definition size in bytes
# - Validation that it's proper JSON
# - Basic structure validation
golang.go
go test ./analyzer/stacks/golang/ -v
Once unit tests pass, you can validate with the actual API: