Determining divergence and gradient using chain rule Suppose $\vec{r}=x\hat{i}+y\hat{j}+z\hat{k}$, and $r=|r|$. Also, let $f$ be a scalar function of $r$ and let $\vec{A}$ be a vector function of $r$. We wish to determine $\nabla f$ and $\nabla.\vec{A}$.
It seems to be obvious that the chain rule is the way to go here:
$$\nabla f= \dfrac{\partial f}{\partial x}\hat{i}+\dfrac{\partial f}{\partial y}\hat{j}+\dfrac{\partial f}{\partial z}\hat{k} $$
$$= \dfrac{\partial f}{\partial r}\dfrac{\partial r}{\partial x}\hat{i}+\dfrac{\partial f}{\partial r}\dfrac{\partial r}{\partial y}\hat{j}+\dfrac{\partial f}{\partial r}\dfrac{\partial r}{\partial z}\hat{k} $$
$$= \dfrac{\partial f}{\partial r}(\dfrac{\partial r}{\partial x}\hat{i}+ \dfrac{\partial r}{\partial y}\hat{j}+\dfrac{\partial r}{\partial z}\hat{k}) $$
$$= \dfrac{\partial f}{\partial r}\hat{r} $$
Which is correct. However,
$$\nabla.\vec{A}=\dfrac{\partial \vec{A}}{\partial x} .\hat{i}+\dfrac{\partial \vec{A}}{\partial y} .\hat{j}+\dfrac{\partial \vec{A}}{\partial z} .\hat{k}$$
$$=\dfrac{\partial \vec{A}}{\partial r}\dfrac{\partial r}{\partial x} .\hat{i} + \dfrac{\partial \vec{A}}{\partial r}\dfrac{\partial r}{\partial y} .\hat{j}  + \dfrac{\partial \vec{A}}{\partial r}\dfrac{\partial r}{\partial z} .\hat{k}$$
$$=\dfrac{\partial \vec{A}}{\partial r}.(\dfrac{\partial r}{\partial x} \hat{i} + \dfrac{\partial r}{\partial y} \hat{j}+ \dfrac{\partial r}{\partial z} \hat{k})$$
$$=\dfrac{\partial \vec{A}}{\partial r}.\hat{r}$$
This expressions turns out to be incorrect.
The only scope for error seems to be in the usage of the chain rule, however if it(my usage of the chain rule) worked for the gradient, why did it fail here ?
 A: Ok, so here's the problem, you didn't account for the unit vectors changing.
$$ \nabla \cdot \vec{A} = \left[\hat{x}\cdot \frac{\partial}{\partial x}  + \hat{y} \cdot \frac{\partial}{\partial y} + \hat{z} \cdot\frac{\partial}{\partial z} \right]  \vec{A}$$
Now, let's consider the dot product with $\hat{x}$:
$$ \hat{x} \cdot \frac{\partial}{\partial x} (\vec{A}) \tag{1}$$
We can write the differential operator as:
$$ \frac{\partial}{\partial x} = \frac{\partial r}{\partial x} \frac{\partial }{\partial r}  + \frac{\partial \theta}{\partial x} \frac{\partial }{\partial \theta}$$
Hence from (1), we get:
$$ \hat{x} \cdot ( \frac{\partial \vec{A}}{\partial r} + \frac{\partial \theta}{\partial x} \frac{\partial \vec{A} }{\partial \theta})$$
Particularly speaking, the term of interest is $ \frac{\partial \vec{A} }{\partial \theta}$, this we can write as(*);
$$ \frac{\partial \vec{A} }{\partial \theta} = \frac{\partial |A(r)| \hat{r} }{\partial \theta} = |A| \frac{\partial \hat{r}}{\partial \theta} = |A|\hat{\theta}$$
Even though we the function is not directly dependent on $\theta$ or $\phi$ the unit vectors are and hence contribute to derivative. Hope this helped!
For a reference on the derivative of unit vector with coordinates see wiki
*: I assumed $\vec{A} = |\vec{A}| \hat{r}$
