Skip to content

Conversation

@satococoa
Copy link
Owner

@satococoa satococoa commented Nov 5, 2025

Summary

  • add runtime detection of module version via build info when ldflags not provided
  • ensure go install binaries report release version instead of dev
  • add tests covering build info fallback logic

Testing

  • go tool task dev

Summary by CodeRabbit

  • New Features

    • Application now automatically embeds and displays actual build version information when available, instead of always showing "dev".
  • Tests

    • Added comprehensive tests to validate version initialization behavior during builds.

Copilot AI review requested due to automatic review settings November 5, 2025 14:14
@coderabbitai
Copy link

coderabbitai bot commented Nov 5, 2025

Walkthrough

The changes introduce a version initialization system that extracts build version information from Go build metadata. A new defaultVersion constant is added, an initVersion() function reads build information at startup to set the application version when available, and corresponding tests validate the initialization behavior.

Changes

Cohort / File(s) Summary
Version initialization setup
cmd/wtp/main.go
Introduces defaultVersion constant set to "dev", initializes version variable to use the constant, and adds initVersion() call at program startup
Test updates
cmd/wtp/main_test.go
Updates TestVersionInfo to compare against defaultVersion constant instead of hard-coded "dev" string
Version info implementation
cmd/wtp/version_info.go
New file adds readBuildInfo helper variable and initVersion() function that conditionally reads Go build metadata via debug.ReadBuildInfo and assigns the build version to the global version variable when appropriate
Version info tests
cmd/wtp/version_info_test.go
New test file with three test functions: TestInitVersionUsesBuildInfoWhenDev, TestInitVersionIgnoresDevelVersion, and TestInitVersionRespectsPresetVersion to validate version initialization behavior with mocked build info

Sequence Diagram

sequenceDiagram
    participant main as main()
    participant init as initVersion()
    participant buildInfo as debug.ReadBuildInfo()
    
    main->>init: Call initVersion()
    alt version == defaultVersion
        init->>buildInfo: Read build information
        alt Build info available
            buildInfo-->>init: Return build info
            alt info.Main.Version not empty<br/>and not "(devel)"
                init->>init: Set version to info.Main.Version
            else Version is empty or "(devel)"
                init->>init: Keep defaultVersion
            end
        else Build info unavailable
            buildInfo-->>init: Return error/nil
            init->>init: Keep defaultVersion
        end
    else version != defaultVersion
        init->>init: Exit early, preserve preset version
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • The version initialization logic in version_info.go requires careful review to ensure build info reading is safe and conditional logic correctly prioritizes preset versions
  • The test file uses mocking patterns that should be verified for correctness and proper cleanup
  • Interaction between initVersion() call timing in main() and the global version variable state needs verification

Possibly related PRs

  • chore: automate version management #41: Modifies version initialization in cmd/wtp/main.go with defaultVersion constant introduction, indicating potential coordination or related work on version management.

Poem

🐰 A constant is born, so steadfast and true,
Build info now flows through the version anew,
Tests guard the logic with mocks held tight,
The dev version dances in morning light!
hop hop 🌟

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: show module version for go install builds' accurately describes the main change: enabling version detection for go install builds via build info.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/go-install-version

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces runtime version detection using Go's build info when the version is not set via ldflags. The implementation allows the application to automatically determine its version from the module metadata when running in development mode.

  • Adds initVersion() function to detect version from debug.ReadBuildInfo() when version equals the default value
  • Extracts the default version string into a defaultVersion constant for consistency
  • Includes comprehensive test coverage for version initialization scenarios

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
cmd/wtp/main.go Introduces defaultVersion constant and calls initVersion() at startup
cmd/wtp/version_info.go Implements version detection logic using build info
cmd/wtp/version_info_test.go Adds tests for version initialization behavior with mocked build info
cmd/wtp/main_test.go Updates test to reference defaultVersion constant instead of hardcoded "dev" string

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
cmd/wtp/version_info.go (1)

9-24: Logic is sound. Consider simplifying the nil check.

The version initialization logic correctly implements the priority order (preset > build info > default) and handles edge cases like "(devel)". The implementation is robust.

On Line 15, the info == nil check is redundant since ok == false already implies info will be nil per Go's convention for boolean returns.

Apply this diff to simplify the condition:

 	info, ok := readBuildInfo()
-	if !ok || info == nil {
+	if !ok {
 		return
 	}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ab87c75 and 4501ae3.

📒 Files selected for processing (4)
  • cmd/wtp/main.go (1 hunks)
  • cmd/wtp/main_test.go (1 hunks)
  • cmd/wtp/version_info.go (1 hunks)
  • cmd/wtp/version_info_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: E2E Tests (macos-latest)
🔇 Additional comments (5)
cmd/wtp/main_test.go (1)

50-51: LGTM! Correct refactoring to use the new constant.

The test now references defaultVersion instead of the hardcoded "dev" string, improving consistency with the constant introduced in main.go.

cmd/wtp/main.go (2)

15-18: LGTM! Good introduction of the default version constant.

The defaultVersion constant makes the default value explicit, testable, and reusable across the codebase.


24-24: LGTM! initVersion() correctly placed at program startup.

The call is appropriately positioned before app initialization, ensuring version information is available when needed.

cmd/wtp/version_info.go (1)

7-7: LGTM! Good testability pattern.

The readBuildInfo variable enables clean mocking in tests while maintaining production behavior.

cmd/wtp/version_info_test.go (1)

10-77: LGTM! Comprehensive test coverage with proper cleanup.

The three tests effectively cover all scenarios:

  • Using build info when version is default
  • Ignoring "(devel)" from build info
  • Respecting preset versions

Each test properly saves and restores state using t.Cleanup, ensuring test isolation.

@satococoa satococoa merged commit 7678ba7 into main Nov 5, 2025
13 checks passed
@satococoa satococoa deleted the fix/go-install-version branch November 5, 2025 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants