Skip to content

Conversation

@hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented Aug 7, 2025

Summary of changes

Changes introduced in this pull request:

  • add go linter CI check
  • fix lint errors

Reference issue to close (if applicable)

Closes

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

Summary by CodeRabbit

  • New Features

    • Introduced automated Go code linting via a new workflow and Makefile command.
    • Added a configuration file for Go linting standards.
    • Implemented a new Go FFI layer for interoperability with Rust, including C-compatible types, interface definitions, and exported functions for cross-language integration.
  • Bug Fixes

    • Improved error handling when setting Go environment variables.
  • Chores

    • Updated file paths for generated Go bindings.
    • Removed .gitignore for generated files, allowing them to be tracked.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 7, 2025

Walkthrough

This change introduces a Go FFI layer for interoperability with Rust-generated C code, including new Go interfaces, exported C-callable functions, and utilities for type conversion and memory management. It also adds a Go linter workflow, a linter configuration file, a Makefile target for linting, and updates to environment variable error handling and build output paths.

Changes

Cohort / File(s) Change Summary
Go FFI Layer Addition
interop-tests/src/tests/go_app/ffi_gen.go
Adds a comprehensive Go FFI layer supporting Rust-generated C bindings, including new interfaces (GoKadNode, GoBitswapNode), exported functions, type conversion utilities, generic list mappers, and memory management helpers.
Go Linting CI Integration
.github/workflows/go-lint.yml, .golangci.yml, Makefile
Introduces a GitHub Actions workflow for Go linting, a linter configuration file, and a new Makefile target to run the linter on specific directories.
Environment Variable Error Handling
f3-sidecar/ffi_impl.go, interop-tests/src/tests/go_app/common.go
Modifies setGoDebugEnv to handle and check errors returned by os.Setenv, ensuring explicit error handling when setting environment variables.
Build Output Path Update
interop-tests/build.rs
Changes the Rust-to-Go code generation output path from gen.go to ffi_gen.go.
.gitignore File Removal
interop-tests/src/tests/go_app/.gitignore
Removes .gitignore that previously ignored gen.go, making this file no longer explicitly ignored by Git.

Sequence Diagram(s)

sequenceDiagram
    participant Rust
    participant C
    participant Go_FFI (ffi_gen.go)
    participant Go_Impl

    Rust->>C: Expose interface via C FFI
    C->>Go_FFI: Call exported Go function (e.g., CGoKadNode_run)
    Go_FFI->>Go_Impl: Call Go interface method (e.g., GoKadNode.run)
    Go_Impl-->>Go_FFI: Return result
    Go_FFI->>C: Return result (with type conversion)
    C->>Rust: Return result
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c10d9bc and 26b7ac0.

📒 Files selected for processing (4)
  • .golangci.yml (1 hunks)
  • Makefile (1 hunks)
  • interop-tests/build.rs (1 hunks)
  • interop-tests/src/tests/go_app/ffi_gen.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • interop-tests/build.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • .golangci.yml
  • Makefile
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: hanabi1224
PR: ChainSafe/forest#5886
File: Makefile:1-1
Timestamp: 2025-08-07T13:12:23.359Z
Learning: golangci-lint version v2.3.1 exists and was released on August 2, 2025. It is currently the latest stable version and can be used in Makefiles and CI configurations. The version follows the v2.x.x numbering scheme, not v1.x.x as I previously incorrectly stated.
Learnt from: hanabi1224
PR: ChainSafe/forest#5886
File: interop-tests/src/tests/go_app/gen.go:88-90
Timestamp: 2025-08-07T13:39:36.941Z
Learning: Skip reviewing auto-generated files marked with "Generated by rust2go. Please DO NOT edit this C part manually." as these should not be manually edited and any issues should be addressed in the code generation tool or source templates instead.
Learnt from: hanabi1224
PR: ChainSafe/forest#5886
File: interop-tests/src/tests/go_app/gen.go:29-29
Timestamp: 2025-08-07T13:39:15.080Z
Learning: Auto-generated files like those created by rust2go (indicated by "Generated by rust2go. Please DO NOT edit this C part manually." comment) in the Forest project should be skipped during code review as they are not intended for manual editing.
Learnt from: hanabi1224
PR: ChainSafe/forest#5886
File: Makefile:1-1
Timestamp: 2025-08-07T13:12:23.359Z
Learning: golangci-lint version v2.3.1 exists and is a valid release that can be used in Makefiles and CI configurations. It was released after v2.3.0.
📚 Learning: 2025-08-07T13:39:36.941Z
Learnt from: hanabi1224
PR: ChainSafe/forest#5886
File: interop-tests/src/tests/go_app/gen.go:88-90
Timestamp: 2025-08-07T13:39:36.941Z
Learning: Skip reviewing auto-generated files marked with "Generated by rust2go. Please DO NOT edit this C part manually." as these should not be manually edited and any issues should be addressed in the code generation tool or source templates instead.

Applied to files:

  • interop-tests/src/tests/go_app/ffi_gen.go
📚 Learning: 2025-08-07T13:39:15.080Z
Learnt from: hanabi1224
PR: ChainSafe/forest#5886
File: interop-tests/src/tests/go_app/gen.go:29-29
Timestamp: 2025-08-07T13:39:15.080Z
Learning: Auto-generated files like those created by rust2go (indicated by "Generated by rust2go. Please DO NOT edit this C part manually." comment) in the Forest project should be skipped during code review as they are not intended for manual editing.

Applied to files:

  • interop-tests/src/tests/go_app/ffi_gen.go
⏰ 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). (9)
  • GitHub Check: tests-release
  • GitHub Check: tests
  • GitHub Check: All lint checks
  • GitHub Check: Build forest binaries on Linux AMD64
  • GitHub Check: Build MacOS
  • GitHub Check: cargo-publish-dry-run
  • GitHub Check: Build Ubuntu
  • GitHub Check: Analyze (rust)
  • GitHub Check: Analyze (go)
🔇 Additional comments (1)
interop-tests/src/tests/go_app/ffi_gen.go (1)

3-20: Auto-generated file – skipping review

File is generated by rust2go ("Please DO NOT edit this C part manually"). Per team guidance we skip manual review.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch hm/go-linter

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

…in permissions

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@hanabi1224 hanabi1224 marked this pull request as ready for review August 7, 2025 12:12
@hanabi1224 hanabi1224 requested a review from a team as a code owner August 7, 2025 12:12
@hanabi1224 hanabi1224 requested review from LesnyRumcajs and sudo-shashank and removed request for a team August 7, 2025 12:12
sudo-shashank
sudo-shashank previously approved these changes Aug 7, 2025
Copy link
Contributor

@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: 2

🧹 Nitpick comments (3)
.golangci.yml (1)

1-3: Consider expanding the linter configuration for better coverage.

While the basic configuration is functional, consider adding more specific settings such as:

  • Specific linters to enable/disable based on project needs
  • Custom rules or severity levels
  • File exclusions if needed
  • Timeout settings for large codebases

Example enhanced configuration:

 version: "2"
 linters:
-  default: standard
+  enable:
+    - gofmt
+    - golint
+    - govet
+    - ineffassign
+    - misspell
+    - errcheck
+run:
+  timeout: 5m
+  skip-dirs:
+    - vendor
interop-tests/src/tests/go_app/gen.go (2)

48-56: Complex unsafe callback mechanism requires careful handling

This function uses low-level unsafe operations and assembly calls through asmcall.CallFuncG0P2. While the runtime.KeepAlive calls are correctly placed to prevent premature garbage collection, this pattern is fragile and difficult to maintain.

Since this is generated code, ensure the generation tool (rust2go) is kept up-to-date and well-tested, as bugs in this layer could cause memory corruption or crashes.


135-158: Variable shadows built-in function

Line 152 declares a variable named len which shadows the built-in len function. While this works, it can cause confusion and potential bugs.

-			len := unsafe.Sizeof(child)
-			copy(children, unsafe.Slice((*byte)(unsafe.Pointer(&child)), len))
-			children = children[len:]
+			size := unsafe.Sizeof(child)
+			copy(children, unsafe.Slice((*byte)(unsafe.Pointer(&child)), size))
+			children = children[size:]
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b997fd5 and 379633d.

📒 Files selected for processing (7)
  • .github/workflows/go-lint.yml (1 hunks)
  • .golangci.yml (1 hunks)
  • Makefile (2 hunks)
  • f3-sidecar/ffi_impl.go (1 hunks)
  • interop-tests/src/tests/go_app/.gitignore (0 hunks)
  • interop-tests/src/tests/go_app/common.go (1 hunks)
  • interop-tests/src/tests/go_app/gen.go (1 hunks)
💤 Files with no reviewable changes (1)
  • interop-tests/src/tests/go_app/.gitignore
⏰ 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). (8)
  • GitHub Check: Build forest binaries on Linux AMD64
  • GitHub Check: tests-release
  • GitHub Check: tests
  • GitHub Check: Build Ubuntu
  • GitHub Check: cargo-publish-dry-run
  • GitHub Check: All lint checks
  • GitHub Check: Analyze (go)
  • GitHub Check: Analyze (rust)
🔇 Additional comments (8)
interop-tests/src/tests/go_app/common.go (1)

16-17: LGTM! Excellent improvement to error handling.

The explicit error handling for os.Setenv is a great improvement. Previously, setting the GODEBUG environment variable could fail silently, but now any errors will be caught and handled appropriately via the checkError function.

f3-sidecar/ffi_impl.go (1)

60-61: LGTM! Consistent error handling improvement.

The explicit error handling for os.Setenv matches the improvement made in the test application and follows Go best practices. This ensures that environment variable setup failures are properly detected rather than silently ignored.

Makefile (2)

82-84: LGTM! Well-structured Go linting target.

The lint-go target is well-designed with:

  • Version pinning for reproducibility
  • Reasonable timeout and concurrency settings
  • Proper directory targeting for the Go code locations

The approach of using go run with version specification ensures consistent tooling across environments.


1-1: Golangci-lint version up to date

Version v2.3.1 is the latest stable release as of August 2025. No change needed.

.github/workflows/go-lint.yml (3)

2-3: LGTM! Proper permissions configuration.

The workflow correctly sets read-only permissions for contents, which addresses security best practices and the previous security scanning comment.


6-8: LGTM! Good concurrency control.

The concurrency configuration properly cancels in-progress runs for feature branches while allowing main branch runs to complete, which optimizes CI resource usage.


26-29: LGTM! Excellent Go setup and integration.

The workflow properly:

  • Uses the latest actions versions (v4, v5)
  • Configures Go version from go.work file for consistency
  • Integrates cleanly with the Makefile target

This provides a robust and maintainable linting pipeline.

interop-tests/src/tests/go_app/gen.go (1)

29-35: No nil-pointer risk: globals are initialized in init()

Both kad_impl.go and bitswap_impl.go define func init() that assigns non-nil implementations to GoKadNodeImpl and GoBitswapNodeImpl before any exported C functions are callable. No additional nil checks are needed.

Comment on lines 1 to 27
package main

/*
// Generated by rust2go. Please DO NOT edit this C part manually.
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct ListRef {
const void *ptr;
uintptr_t len;
} ListRef;
typedef struct StringRef {
const uint8_t *ptr;
uintptr_t len;
} StringRef;
*/
import "C"
import (
"runtime"
"unsafe"

"github.com/ihciah/rust2go/asmcall"
)
Copy link
Contributor

@coderabbitai coderabbitai bot Aug 7, 2025

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Consider excluding generated files from version control and linting

This file is marked as generated by rust2go with an explicit "DO NOT edit" warning. Generated files should typically be:

  1. Excluded from version control (regenerated as needed)
  2. Excluded from linting to avoid noise from auto-generated code

The AI summary indicates a .gitignore entry for gen.go was removed. Consider keeping generated files out of version control and adding this path to .golangci.yml exclusions.


🏁 Script executed:

#!/bin/bash
# Check if other generated files are tracked and if linting exclusions are configured

# Check for other generated files in the repository
echo "=== Searching for other generated files ==="
rg -l "Generated by|DO NOT edit|Code generated" --type go

# Check if gen.go is excluded from linting in .golangci.yml
echo -e "\n=== Checking .golangci.yml for exclusions ==="
if [ -f ".golangci.yml" ]; then
    cat .golangci.yml | grep -A 10 -B 2 "skip-files\|skip-dirs\|exclude"
fi

# Check if there's a generation script or Makefile target
echo -e "\n=== Checking for generation commands ==="
if [ -f "Makefile" ]; then
    grep -n "gen\.go\|rust2go" Makefile
fi

Length of output: 578


Exclude generated Go files from version control and linting

We discovered two tracked generated files without any lint-exclusion rules:

  • interop-tests/src/tests/go_app/gen.go
  • f3-sidecar/ffi_gen.go

And .golangci.yml contains no skip-files, skip-dirs, or exclude entries for them.

Action items:

  • Add these paths (or a regex) to .gitignore so they aren’t accidentally committed in the future.
  • Configure GolangCI to skip linting on all *_gen.go files.

Suggested diff:

# .gitignore
+# Ignore Rust2Go-generated Go code
+**/*_gen.go

# .golangci.yml
 run:
+  skip-files:
+    - '.*_gen\.go$'

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In interop-tests/src/tests/go_app/gen.go lines 1 to 27, the file is a generated
Go file but is currently tracked in version control and not excluded from
linting. To fix this, add the path interop-tests/src/tests/go_app/gen.go or a
suitable pattern matching generated files to the .gitignore file to prevent
accidental commits. Additionally, update the .golangci.yml configuration to
include a skip-files or exclude rule that matches all *_gen.go files, ensuring
these generated files are excluded from linting.

Copy link
Member

Choose a reason for hiding this comment

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

@hanabi1224 I think this comment makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@LesnyRumcajs Fixed. (The suggested change does not match the config syntax though)

Comment on lines 88 to 100
func newString(s_ref C.StringRef) string {
return unsafe.String((*byte)(unsafe.Pointer(s_ref.ptr)), s_ref.len)
}
func refString(s *string, _ *[]byte) C.StringRef {
return C.StringRef{
ptr: (*C.uint8_t)(unsafe.StringData(*s)),
len: C.uintptr_t(len(*s)),
}
}

func ownString(s_ref C.StringRef) string {
return string(unsafe.Slice((*byte)(unsafe.Pointer(s_ref.ptr)), int(s_ref.len)))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Memory safety concern with string conversion functions

The newString function (line 89) creates a Go string that aliases the C memory without copying, while ownString (line 99) creates a proper copy. Using newString in the exported functions (lines 44, 73, 79) could lead to use-after-free if the C side deallocates the memory while Go still holds references.

Consider using ownString instead of newString in the exported functions for safety, unless you're certain the C memory remains valid for the entire duration of the Go function calls.

🤖 Prompt for AI Agents
In interop-tests/src/tests/go_app/gen.go around lines 88 to 100, the newString
function returns a Go string referencing C memory without copying, risking
use-after-free if C deallocates it. To fix this, replace calls to newString in
the exported functions at lines 44, 73, and 79 with ownString, which safely
copies the C memory into a Go string, ensuring memory safety.

@@ -0,0 +1,29 @@
name: Go code linters
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to invoke this exclusively for changes in the Go source code directories + go.work etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, fixed.

LesnyRumcajs
LesnyRumcajs previously approved these changes Aug 7, 2025
@hanabi1224 hanabi1224 enabled auto-merge August 7, 2025 12:51
Makefile Outdated
@@ -1,3 +1,5 @@
GOLANGCI_LINT_VERSION=v2.3.1
Copy link
Member

Choose a reason for hiding this comment

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

does it have to be global for the makefile? can it be just part of the go run... command?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's easier to upgrade when it's at the top, what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but it's not used anywhere else. It adds unnecessary noise for anyone NOT upgrading it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, fixed.

@LesnyRumcajs
Copy link
Member

no green checkmark!

Copy link
Member

Choose a reason for hiding this comment

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

is this not generated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is generated.

Copy link
Member

Choose a reason for hiding this comment

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

So, could we not add it to the source?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Go linter does not work without it. Generating it requires running rust build

@hanabi1224 hanabi1224 added this pull request to the merge queue Aug 11, 2025
Merged via the queue into main with commit 59ca796 Aug 11, 2025
44 checks passed
@hanabi1224 hanabi1224 deleted the hm/go-linter branch August 11, 2025 12:29
@coderabbitai coderabbitai bot mentioned this pull request Oct 8, 2025
4 tasks
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.

4 participants