New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import-local may not work correctly in a Yarn workspace #7
Comments
Thanks for filing this @elliotdavies! I just ran into the exact same issue when trying to run jest's executable at a given version from a nested directory in a monorepo. Instead, as you described above, it seems that |
I'm running into this. What would the consequences be if we made this change to 'use strict';
const path = require('path');
const resolveCwd = require('resolve-cwd');
const pkgDir = require('pkg-dir');
module.exports = filename => {
const globalDir = pkgDir.sync(path.dirname(filename));
const relativePath = path.relative(globalDir, filename);
const pkg = require(path.join(globalDir, 'package.json'));
const localFile = resolveCwd.silent(path.join(pkg.name, relativePath));
const filenameInLocalNodeModules = isFilenameInLocalNodeModules(filename, localFile);
// Use `path.relative()` to detect local package installation,
// because __filename's case is inconsistent on Windows
// Can use `===` when targeting Node.js 8
// See https://github.com/nodejs/node/issues/6624
return !filenameInLocalNodeModules && localFile && path.relative(localFile, filename) !== '' && require(localFile);
};
function isFilenameInLocalNodeModules(filename, localFile) {
if (localFile !== filename) {
return true
}
const localNodeModules = path.join(process.cwd(), 'node_modules');
return !path.relative(localNodeModules, filename).startsWith('..')
} That changes fixes my issue, but I don't know if it creates other problems. |
When using Yarn workspaces, such as in a monorepo setup,
node_modules
may be split across several locations like so:In this scenario an installed dependency may exist at the root
node_modules
or in one of the nested ones, depending on whether Yarn chooses to hoist it. The dependency may even exist in both places at different version numbers, as illustrated withdep
above.On this line,
import-local
effectively assumes that the directory it's running from is the sole local directory:import-local/index.js
Line 10 in 101c451
In a workspace however,
import-local
could be running from any of thenode_modules
folders. If for exampleimport-local
is installed at the workspace root and gets invoked bydep@v2
,import-local
will end up finding and invokingdep@v1
, because that's in the same directory.I would say this is a bug because neither version of
dep
was globally installed, and so I would expectimport-local
to return false-y.The text was updated successfully, but these errors were encountered: