Skip to content
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

workaround for require.resolve in prettier-vscode #7951

Merged
merged 1 commit into from Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/common/load-plugins.js
Expand Up @@ -8,6 +8,7 @@ const path = require("path");
const thirdParty = require("./third-party");
const internalPlugins = require("./internal-plugins");
const mem = require("mem");
const resolve = require("./resolve");

const memoizedLoad = mem(load, { cacheKey: JSON.stringify });
const memoizedSearch = mem(findPluginsInNodeModules);
Expand Down Expand Up @@ -42,14 +43,10 @@ function load(plugins, pluginSearchDirs) {
let requirePath;
try {
// try local files
requirePath = eval("require").resolve(
path.resolve(process.cwd(), pluginName)
);
requirePath = resolve(path.resolve(process.cwd(), pluginName));
} catch (_) {
// try node modules
requirePath = eval("require").resolve(pluginName, {
paths: [process.cwd()],
});
requirePath = resolve(pluginName, { paths: [process.cwd()] });
}

return {
Expand Down Expand Up @@ -85,9 +82,7 @@ function load(plugins, pluginSearchDirs) {

return memoizedSearch(nodeModulesDir).map((pluginName) => ({
name: pluginName,
requirePath: eval("require").resolve(pluginName, {
paths: [resolvedPluginSearchDir],
}),
requirePath: resolve(pluginName, { paths: [resolvedPluginSearchDir] }),
}));
})
.reduce((a, b) => a.concat(b), []);
Expand Down
12 changes: 12 additions & 0 deletions src/common/resolve.js
@@ -0,0 +1,12 @@
"use strict";

let { resolve } = eval("require");

// In the VS Code extension `require` is overridden and `require.resolve` doesn't support the 2nd argument.
if (resolve.length === 1) {
const Module = eval("require")("module");
const createRequire = Module.createRequire || Module.createRequireFromPath;
resolve = createRequire(__dirname).resolve;
}
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess unnecessary to do another length check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean.

Copy link
Sponsor Member

@fisker fisker Apr 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean do we need check resolve.length again and throw an error if length still 1, but I guess it's unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.


module.exports = resolve;
5 changes: 2 additions & 3 deletions src/config/resolve-config.js
Expand Up @@ -7,6 +7,7 @@ const mem = require("mem");

const resolveEditorConfig = require("./resolve-config-editorconfig");
const loadToml = require("../utils/load-toml");
const resolve = require("../common/resolve");

const getExplorerMemoized = mem(
(opts) => {
Expand All @@ -18,9 +19,7 @@ const getExplorerMemoized = mem(
if (typeof result.config === "string") {
const dir = path.dirname(result.filepath);
try {
const modulePath = eval("require").resolve(result.config, {
paths: [dir],
});
const modulePath = resolve(result.config, { paths: [dir] });
result.config = eval("require")(modulePath);
} catch (error) {
// Original message contains `__filename`, can't pass tests
Expand Down