Spectroscopy has emerged as an essential technology, particularly in decentralized utilization within point-of-care devices. These applications demand compact, costeffective designs with reduced complexity compared with traditional laboratory equipment. Achieving compactness often involves minimizing the number of components, necessitating that each remaining component fulfills multiple functions to optimize performance. However, this approach can lead to significant aberrations due to constructive compromises. Nevertheless, the known phase errors enable correction, often achieved directly through diffractive elements. Diffractive compensation of aberrations is commonly conducted through interference lithography, exploiting holographic techniques to produce gratings without explicit knowledge of the interference structure. Alternatively, mechanical manufacturing techniques offer the possibility of producing blazed gratings with greater efficiency. However, diffractive correction using mechanically fabricated gratings requires a precise understanding of individual groove trajectories, presenting an ongoing challenge. We employed ultraprecision (UP) mechanical manufacturing techniques to create aberration-corrected diffraction gratings for spectroscopic applications. To enable the machining of freeform trajectories, facilitating versatile fabrication of both planar and concave imaging blazed gratings, a modified five-axis UP machinery is employed. To correct the known wavefront errors of the exemplary use cases, a nonlinear phase function was applied and a numerical method was developed to derive trajectories from the phase errors and translate them into machine code. The use cases are a blazed imaging planar Littrow grating and concave Rowland gratings, showcasing corrected astigmatic wavefront deviation. The theoretical and experimental results are compared and discussed.