Skip to content

Commit

Permalink
Bug fix: Maintain symlinked path if present for better IDE processing
Browse files Browse the repository at this point in the history
If the repo is in a symlinked directory, then sometimes ESLint and other internal systems of IDEs fails due to the path mismatch between the `pwd` and `realpath`. This fixes that issue by using the symlinked path rather than the absolute path.
  • Loading branch information
D-Pow committed May 1, 2022
1 parent 4e96375 commit 42f26a5
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions config/utils/Files.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ const Paths = (() => {
);
}

/*
* Fix paths to use the path of the current process' directory if it's within a symlinked path.
* Otherwise, IDEs may have trouble with automatic resolutions for ESLint, file paths, etc.
*
* Short summary:
* - process.cwd() == `npm prefix` - Doesn't preserve symlinks
* - process.env.PWD == `pwd` - Preserves symlinks
* - fs.realpathSync(process.env.PWD) == `realpath $(pwd)` - Doesn't preserve symlinks
*/
const realPath = pathMappings.ROOT.ABS; // Root dir without preserving symlinks
const currentProcessPath = process.env.PWD; // Root dir preserving symlinks + current process' path (e.g. if in a nested directory from the root)
const currentProcessNestedPath = fs.realpathSync(currentProcessPath).replace(realPath, ''); // Only the nested directory, used for diffing any (non-)symlink-preserving path (nested or not) with the repo root in order to convert PWD to the root dir
const rootDirMaintainingSymlinks = currentProcessPath.replace(currentProcessNestedPath, ''); // Root dir preserving symlinks without the nested directory
pathMappings.ROOT.ABS = rootDirMaintainingSymlinks;

function setAbsPaths(pathConfig, prevRelPath) {
if (prevRelPath) {
pathConfig.REL = `${prevRelPath}/${pathConfig.REL}`;
Expand Down

0 comments on commit 42f26a5

Please sign in to comment.