Repository: prettier/prettier (JavaScript)
Task: Feature (2018-06-11 16:20:55+00:00)

Issue:
Support relative paths for plugin and pluginSearchDir in config files
Context: https://github.com/prettier/prettier/pull/4192#discussion_r186809767


Discussion:

Patch:
diff --git a/src/config/resolve-config.js b/src/config/resolve-config.js
index 229caf3d5fdd..c8f0f43433ab 100644
--- a/src/config/resolve-config.js
+++ b/src/config/resolve-config.js
@@ -48,6 +48,17 @@ function _resolveConfig(filePath, opts, sync) {
       mergeOverrides(Object.assign({}, result), filePath)
     );
 
+    ["plugins", "pluginSearchDirs"].forEach(optionName => {
+      if (Array.isArray(merged[optionName])) {
+        merged[optionName] = merged[optionName].map(
+          value =>
+            typeof value === "string" && value.startsWith(".") // relative path
+              ? path.resolve(path.dirname(result.filepath), value)
+              : value
+        );
+      }
+    });
+
     if (!result && !editorConfigured) {
       return null;
     }


Test patch:
diff --git a/tests_integration/__tests__/config-resolution.js b/tests_integration/__tests__/config-resolution.js
index 25a610e533a9..d78a55eb84ae 100644
--- a/tests_integration/__tests__/config-resolution.js
+++ b/tests_integration/__tests__/config-resolution.js
@@ -226,3 +226,12 @@ test("API resolveConfig.sync removes $schema option", () => {
     tabWidth: 42
   });
 });
+
+test("API resolveConfig resolves relative path values based on config filepath", () => {
+  const currentDir = path.join(__dirname, "../cli/config/resolve-relative");
+  const parentDir = path.resolve(currentDir, "..");
+  expect(prettier.resolveConfig.sync(`${currentDir}/index.js`)).toMatchObject({
+    plugins: [path.join(parentDir, "path-to-plugin")],
+    pluginSearchDirs: [path.join(parentDir, "path-to-plugin-search-dir")]
+  });
+});
diff --git a/tests_integration/cli/config/resolve-relative/.prettierrc b/tests_integration/cli/config/resolve-relative/.prettierrc
new file mode 100644
index 000000000000..8d56196422e4
--- /dev/null
+++ b/tests_integration/cli/config/resolve-relative/.prettierrc
@@ -0,0 +1,4 @@
+{
+    "plugins": ["../path-to-plugin"],
+    "pluginSearchDirs": ["../path-to-plugin-search-dir"]
+}