Repository: mantoni/eslint_d.js
Pull request #305 (2024-08-01 11:58:21)

fix: not working when paths in config contains spaces

L2jLiga: > Thank you! Can you please look into the failing tests?

Sure, fixed, tests are green on local machine

Patch:
diff --git a/lib/forwarder.js b/lib/forwarder.js
index c64ae5c..e5a1a8d 100644
--- a/lib/forwarder.js
+++ b/lib/forwarder.js
@@ -22,10 +22,10 @@ export async function forwardToDaemon(resolver, config) {
   const args = [
     config.token,
     stdout ? stdout.level : 0,
-    JSON.stringify(process.cwd()),
-    JSON.stringify(process.argv)
+    process.cwd(),
+    process.argv
   ];
-  socket.write(args.join(' '));
+  socket.write(JSON.stringify(args));
   if (text) {
     socket.write('\n');
     socket.write(text);
diff --git a/lib/resolver.js b/lib/resolver.js
index e19d2e3..08576c5 100644
--- a/lib/resolver.js
+++ b/lib/resolver.js
@@ -1,4 +1,5 @@
 import { createRequire } from 'node:module';
+import { dirname } from 'node:path';
 
 /**
  * @typedef {Object} Resolver
@@ -16,7 +17,7 @@ export function createResolver() {
   let bundled = false;
   let path;
   try {
-    path = require.resolve('eslint', { paths: [process.cwd()] });
+    path = require.resolve('eslint/package.json', { paths: [process.cwd()] });
   } catch (err) {
     if (local === 'ignore') {
       return 'ignore';
@@ -26,11 +27,11 @@ export function createResolver() {
       return 'fail';
     }
     // Fallback to bundled eslint
-    path = require.resolve('eslint');
+    path = require.resolve('eslint/package.json');
     bundled = true;
   }
   return {
-    base: path.substring(0, path.lastIndexOf('/eslint/') + 7),
+    base: dirname(path),
     bundled,
     require
   };
diff --git a/lib/service.js b/lib/service.js
index 5df0e50..cfbff4e 100644
--- a/lib/service.js
+++ b/lib/service.js
@@ -39,20 +39,20 @@ export function createService(resolver, token) {
           text = content.slice(newline + 1);
           content = content.slice(0, newline);
         }
-        const [request_token, color_level, cwd, argv] = content.split(' ');
+        const [request_token, color_level, cwd, argv] = JSON.parse(content);
         if (request_token !== token) {
           con.end();
           return;
         }
 
         chalk.level = color_level;
-        process.chdir(JSON.parse(cwd));
+        process.chdir(cwd);
 
         process.stdout.write = (chunk) => con.write(chunk);
         process.stderr.write = (chunk) => con.write(chunk);
         let code = 1;
         try {
-          code = await eslint.execute(JSON.parse(argv), text, true);
+          code = await eslint.execute(argv, text, true);
         } catch (e) {
           con.write(String(e));
         } finally {
diff --git a/package-lock.json b/package-lock.json
index b72acbc..7810b46 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -24,6 +24,9 @@
         "mocha": "^10.7.0",
         "prettier": "^3.3.3",
         "typescript": "^5.5.4"
+      },
+      "engines": {
+        "node": ">=18"
       }
     },
     "node_modules/@babel/parser": {


Test patch:
diff --git a/lib/forwarder.test.js b/lib/forwarder.test.js
index cc4c5f8..c97c998 100644
--- a/lib/forwarder.test.js
+++ b/lib/forwarder.test.js
@@ -45,7 +45,7 @@ describe('lib/forwarder', () => {
 
       assert.calledOnceWith(
         socket.write,
-        `token ${supportsColor.stdout?.['level'] || 0} "the/cwd" ["node","eslint_d"]`
+        `["token",${supportsColor.stdout?.['level'] || 0},"the/cwd",["node","eslint_d"]]`
       );
       assert.calledOnce(socket.end);
     });
diff --git a/lib/resolver.test.js b/lib/resolver.test.js
index 001d046..0e586a8 100644
--- a/lib/resolver.test.js
+++ b/lib/resolver.test.js
@@ -64,7 +64,7 @@ describe('lib/resolver', () => {
       assert.calledOnceWith(
         console.error,
         match(
-          "eslint_d: Failed to resolve eslint - Error: Cannot find module 'eslint'"
+          "eslint_d: Failed to resolve eslint - Error: Cannot find module 'eslint/package.json'"
         )
       );
     });
diff --git a/lib/service.test.js b/lib/service.test.js
index 62daf9c..4650174 100644
--- a/lib/service.test.js
+++ b/lib/service.test.js
@@ -44,8 +44,8 @@ describe('lib/service', () => {
      */
     function send(request_token, color_level, cwd, argv, text) {
       const chunks = [
-        `${request_token} ${color_level} `,
-        `${JSON.stringify(cwd)} ${JSON.stringify(argv)}`
+        `["${request_token}",${color_level},`,
+        `${JSON.stringify(cwd)},${JSON.stringify(argv)}]`
       ];
       if (text !== undefined) {
         chunks.push(`\n${text}`);
@@ -77,7 +77,7 @@ describe('lib/service', () => {
       send(token, '3', '/', []);
 
       await eslint_promise.resolve(0);
-      assert.equals(chalk.level, '3');
+      assert.equals(chalk.level, 3);
       assert.calledOnceWith(process.chdir, '/');
     });