codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

Rust/Axum: a handler on a line after `.route(` is never linked to its route

Open mg-mg-mg opened this issue 2 months ago • 1 comments

Version: 1.6.2 (also current main, 6560052)

Summary

.route("/a", get(handlers::f)) on one line links the route node to f. The rustfmt-wrapped form, with .route( on its own line and get(handlers::f) on a later line, never links, even when f is unique in the project. Rustfmt wraps any .route( call that exceeds the line width, so in a real Axum server most routes are affected. In one production router, 31 of 364 handler references resolved.

Reproduction

Run with CODEGRAPH=/path/to/codegraph bash repro.sh (creates and deletes a temporary project):

set -euo pipefail
CODEGRAPH="${CODEGRAPH:-codegraph}"
export DO_NOT_TRACK=1 CODEGRAPH_TELEMETRY=0 CODEGRAPH_NO_UPDATE_CHECK=1
proj="$(mktemp -d)"
trap 'rm -rf "$proj"' EXIT
index_project() { (cd "$proj" && git init -q && "$CODEGRAPH" init . >/dev/null 2>&1); }
callers_of() { (cd "$proj" && "$CODEGRAPH" callers "$1" 2>&1); }

mkdir -p "$proj/src/handlers"
cat >"$proj/Cargo.toml" <<'T'
[package]
name = "repro"
version = "0.1.0"
edition = "2021"
T
cat >"$proj/src/handlers/mod.rs" <<'T'
pub async fn single_line_handler() {}
pub async fn multi_line_handler() {}
T
cat >"$proj/src/main.rs" <<'T'
mod handlers;
use axum::{routing::get, Router};

pub fn app() -> Router {
    Router::new()
        .route("/single", get(handlers::single_line_handler))
        .route(
            "/multi",
            get(handlers::multi_line_handler),
        )
}
T
index_project
echo "== callers single_line_handler (expected: app)"; callers_of single_line_handler
echo "== callers multi_line_handler (expected: app)"; callers_of multi_line_handler

Actual

== callers single_line_handler (expected: app)
route       GET /single [references]
  src/main.rs:6
== callers multi_line_handler (expected: app)
ℹ No callers found for "multi_line_handler"

Expected

GET /multi (src/main.rs:7) references multi_line_handler.

Likely cause

In dist/resolution/frameworks/rust.js (Axum block), the route node and its handler reference both use line = the line of .route(. Resolution then passes through gateRustScope → isRustNameInScope (name-matcher), which reads only ref.line and looks for the handler name (bare or path-qualified) on that line. On a wrapped call that line is just .route(, so the name is never found and the resolved edge is dropped. Recording the reference at the line where the handler expression appears (offset of mh.index within methodBody) should fix it. The Actix builder block may have the same pattern.

mg-mg-mg avatar Oct 03 '26 15:10 mg-mg-mg

Thanks @mg-mg-mg for the report, and for the diagnosis. It was exactly right.

Fixed on main in #2359, which takes #2329 by @danusha2345 as written. The handler reference is now recorded on the line and column where the handler's name is written, not on the .route( line, so the scope check finds it however rustfmt wraps the call. That also fixes single-line routes whose handler name appears elsewhere on the line, like .route("/login", post(handlers::user::login)). As you suspected, the Actix builder forms had the same problem, and they're fixed too. Two related fixes are included: a get( inside a closure handler (cache.get(key)) is no longer read as a route, and an Actix web::resource(...) no longer takes the next route's handler.

On your script, callers multi_line_handler now lists GET /multi at src/main.rs:7. On real projects, crates.io went from 1 to 5 linked routes and atuin from 7 to 12, with 2 wrong targets corrected.

This ships in the next release. Re-index after upgrading (codegraph index) to pick it up. You and @danusha2345 are credited in the changelog.

colbymchenry avatar Oct 05 '26 22:10 colbymchenry