Skip to content

absolute paths in moduleDirectories are invalid in Windows OS #5396

@warren-bank

Description

@warren-bank

https://github.com/facebook/jest
https://github.com/facebook/jest/tree/master/packages/jest-resolve
https://github.com/facebook/jest/blob/master/packages/jest-resolve/src/node_modules_paths.js


error:

  • absolute paths in moduleDirectories are invalid in Windows OS
  • results in CommonJS modules not being found
  • import statements throw an Error
  • all tests fail

evidence:

file:
packages/jest-resolve/src/node_modules_paths.js

line: 54

insert code:
console.log('dirs:', JSON.stringify(dirs, null, 2))


patch v1:

file:
packages/jest-resolve/src/node_modules_paths.js

line: 50

original code:
return path.join(prefix, aPath, moduleDir);

modified code:
return path.isAbsolute(moduleDir) ? moduleDir : path.join(prefix, aPath, moduleDir);

effects:

  • dirs.length will not change
  • dirs will contain paths.length duplicates of each absolute path in modules
  • absolute paths will be valid
  • absolute paths for Windows OS will no-longer be broken

patch v2:

file:
packages/jest-resolve/src/node_modules_paths.js

lines: 47 - 53

original code:

  const dirs = paths.reduce((dirs, aPath) => {
    return dirs.concat(
      modules.map(moduleDir => {
        return path.join(prefix, aPath, moduleDir);
      }),
    );
  }, []);

modified code:

  const dirs = paths
    .reduce((dirs, aPath) => {
      return dirs.concat(
        modules.map(moduleDir => {
          return path.isAbsolute(moduleDir) ? ((aPath === basedirAbs) ? moduleDir : null) : path.join(prefix, aPath, moduleDir);
        }),
      );
    }, [])
    .filter(dir => (dir !== null))
  ;

effects:

  • each absolute path in modules is only returned for a particular iteration of paths.reduce()
  • other iterations inject a null value
  • dirs is then filtered to remove these null values
  • dirs.length will be reduced, which could slightly boost performance
  • absolute paths will be valid
  • absolute paths for Windows OS will no-longer be broken

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions