Skip to content

Commit 42f26a5

Browse files
committed
Bug fix: Maintain symlinked path if present for better IDE processing
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.
1 parent 4e96375 commit 42f26a5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

config/utils/Files.js

+15
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ const Paths = (() => {
8181
);
8282
}
8383

84+
/*
85+
* Fix paths to use the path of the current process' directory if it's within a symlinked path.
86+
* Otherwise, IDEs may have trouble with automatic resolutions for ESLint, file paths, etc.
87+
*
88+
* Short summary:
89+
* - process.cwd() == `npm prefix` - Doesn't preserve symlinks
90+
* - process.env.PWD == `pwd` - Preserves symlinks
91+
* - fs.realpathSync(process.env.PWD) == `realpath $(pwd)` - Doesn't preserve symlinks
92+
*/
93+
const realPath = pathMappings.ROOT.ABS; // Root dir without preserving symlinks
94+
const currentProcessPath = process.env.PWD; // Root dir preserving symlinks + current process' path (e.g. if in a nested directory from the root)
95+
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
96+
const rootDirMaintainingSymlinks = currentProcessPath.replace(currentProcessNestedPath, ''); // Root dir preserving symlinks without the nested directory
97+
pathMappings.ROOT.ABS = rootDirMaintainingSymlinks;
98+
8499
function setAbsPaths(pathConfig, prevRelPath) {
85100
if (prevRelPath) {
86101
pathConfig.REL = `${prevRelPath}/${pathConfig.REL}`;

0 commit comments

Comments
 (0)