Given a finitely generated projective module $M$ over a ring $R$ with exactly two idempotents $0,1$ ($X=\operatorname{Spec} R$ is connected). We have a coherent $\mathcal{O}_X$-module $\widetilde{M}$ and the rank of $\widetilde{M}$ at $\mathfrak{p}\in X$ is defined by $$\dim_{\kappa({\mathfrak{p}})}M_{\mathfrak{p}}\otimes_{R_\mathfrak{p}}\kappa({\mathfrak{p}})$$
Question.
- Why is this dimension constant?
- For an arbitrary ring $R$, why is it locally constant (is it constant on connected components)?
My attempts. Since we have $$M_{\mathfrak{p}}\otimes_{R_\mathfrak{p}}\kappa({\mathfrak{p}})\cong M\otimes_{R}R_{\mathfrak{p}}\otimes_{R_\mathfrak{p}}\kappa(\mathfrak{p})\cong M\otimes_{R}\kappa(\mathfrak{p})$$ When $R$ is an integral domain, we do have all $\kappa({\mathfrak{p}})$ are isomorphic to the fractional field at the generic point. Hence the rank is constant. Hence I think a possible generalization is, for arbitrary ring $R$, the rank of $M$ should be constant on irreducible components of $\operatorname{Spec} R$.
Updates. As KReiser pointed out, my attempt above is NOT correct. Now I am even more confused, as irreducible spaces are connected and we cannot have constant value there.
Any help is appreciated. Just in case, I have read Mr. Brandenburg's answer in this post Rank of projective module where he did not prove his claim.
The best proof of this is via the fact that if $R$ is a commutative ring and $M$ is a finitely-generated $R$-module which is projective, then there exist $f_1,\cdots,f_n\in R$ with $M_{f_i}$ free and $(f_1,\cdots,f_n)=R$, among other things. The proof is not hard, but there are plenty of sub-results to prove and use, and it has already been written up many times, and I always get impatient with proving it for someone else. For a good source, see Serre's original paper Modules projectifs et espaces fibres a fibre vectorielle, Stacks tag 00NV, and many others. It's quite nice to take a journey through this and I recommend you do so at some point!
Here is a reasonably quick proof modulo some results from algebra if you're willing to accept a restriction to rings $R$ with finitely many minimal primes (i.e. noetherian rings):
Algebra facts.
Lemma. Let $R$ be a commutative ring and let $M$ be a finitely-generated projective module. Then $M_P$ is a finitely-generated projective module for any prime ideal $P\subset R$.
Proof. Let $R^n\to M$ be a surjection (which exists by the finitely-generated hypothesis) with kernel $K$, so that $R^n\cong M\oplus K$ by fact 1. As localization is exact, we have $R^n_P\cong M_P\oplus K_P$, which implies $M_P$ is projective by fact 2. $\blacksquare$
Now let's prove the result. For any primes $P\subset Q \subset R$, we have $M_P\cong R_P^{n_P}$ and $M_Q\cong R_Q^{n_Q}$ for some nonnegative integers $n_P,n_Q$ by the lemma, giving $\operatorname{rank}_P M=n_P$ and $\operatorname{rank}_Q M=n_Q$. On the other hand, since localization is transitive, we have $R_P^{n_P} = M_P = (M_Q)_{P_Q} = (R_Q^{n_Q})_{P_Q} = R_P^{n_Q}$, giving $n_Q=n_P$ for any inclusion of primes $P\subset Q$. Now since $R$ has only the trivial idempotents, $\operatorname{Spec} R$ is connected, which means for any primes $P,P'\subset R$ there is a chain of inclusions of prime ideals $P\subset P_0 \supset P_1 \subset \cdots \supset P_n \subset P'$ connecting $P$ and $P'$, which by the above shows that $n_P = n_{P'}$. Such a chain exists by jumping from irreducible component to irreducible component: if there were a partition of irreducible components in to two sets where you cannot jump from one to the other, this would prove $\operatorname{Spec} R$ is disconnected.