From 7ef066b48a890352c92fa49f7984e941ce990cbe Mon Sep 17 00:00:00 2001
From: Andreas Abel <andreas.abel@ifi.lmu.de>
Date: Wed, 31 Jan 2024 05:10:06 +0100
Subject: [PATCH] Correct way to get name of config file from cabal --help

---
 dist/index.js        | 35 +++++++++++++++++++++++++----------
 lib/setup-haskell.js | 35 +++++++++++++++++++++++++----------
 package.json         |  2 +-
 src/setup-haskell.ts | 36 ++++++++++++++++++++++++++----------
 tsconfig.json        |  4 ++--
 5 files changed, 79 insertions(+), 33 deletions(-)

diff --git a/dist/index.js b/dist/index.js
index 89b7a99..97d28a2 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -14040,16 +14040,31 @@ async function cabalConfig() {
         silent: true,
         listeners: { stdout: append, stderr: append }
     });
-    // The last line of the cabal help text is printing the config file, e.g.:
-    //
-    // > You can edit the cabal configuration file to set defaults:
-    // >   <<HOME>>/.cabal/config
-    //
-    // So trimming the last line will give us the name of the config file.
-    //
-    // Needless to say this is very brittle, but we secure this by a test
-    // in Cabal's testsuite:  https://github.com/haskell/cabal/pull/9614
-    return out.toString().trim().split('\n').slice(-1)[0].trim();
+    return configFileFromHelpText(out.toString());
+}
+// The end of the cabal help text is printing the config file, e.g.:
+//
+// > You can edit the cabal configuration file to set defaults:
+// >   <<HOME>>/.cabal/config
+// > This file will be generated with sensible defaults if you run 'cabal update'.
+//
+// The last line here is only printed if the file does not exist yet.
+//
+// So trimming last following "You can edit..." will give us the name of the config file.
+//
+// Needless to say this is very brittle, but we secure this by a test
+// in Cabal's testsuite:  https://github.com/haskell/cabal/pull/9614
+//
+function configFileFromHelpText(txt) {
+    const marker = 'You can edit the cabal configuration file to set defaults:';
+    const lines = txt.split('\n').map(line => line.trim());
+    const foundIndex = lines.findLastIndex(line => line === marker);
+    if (foundIndex !== -1 && foundIndex + 1 < lines.length) {
+        return lines[foundIndex + 1];
+    }
+    else {
+        return '';
+    }
 }
 async function run(inputs) {
     try {
diff --git a/lib/setup-haskell.js b/lib/setup-haskell.js
index db71ead..489cae4 100644
--- a/lib/setup-haskell.js
+++ b/lib/setup-haskell.js
@@ -42,16 +42,31 @@ async function cabalConfig() {
         silent: true,
         listeners: { stdout: append, stderr: append }
     });
-    // The last line of the cabal help text is printing the config file, e.g.:
-    //
-    // > You can edit the cabal configuration file to set defaults:
-    // >   <<HOME>>/.cabal/config
-    //
-    // So trimming the last line will give us the name of the config file.
-    //
-    // Needless to say this is very brittle, but we secure this by a test
-    // in Cabal's testsuite:  https://github.com/haskell/cabal/pull/9614
-    return out.toString().trim().split('\n').slice(-1)[0].trim();
+    return configFileFromHelpText(out.toString());
+}
+// The end of the cabal help text is printing the config file, e.g.:
+//
+// > You can edit the cabal configuration file to set defaults:
+// >   <<HOME>>/.cabal/config
+// > This file will be generated with sensible defaults if you run 'cabal update'.
+//
+// The last line here is only printed if the file does not exist yet.
+//
+// So trimming last following "You can edit..." will give us the name of the config file.
+//
+// Needless to say this is very brittle, but we secure this by a test
+// in Cabal's testsuite:  https://github.com/haskell/cabal/pull/9614
+//
+function configFileFromHelpText(txt) {
+    const marker = 'You can edit the cabal configuration file to set defaults:';
+    const lines = txt.split('\n').map(line => line.trim());
+    const foundIndex = lines.findLastIndex(line => line === marker);
+    if (foundIndex !== -1 && foundIndex + 1 < lines.length) {
+        return lines[foundIndex + 1];
+    }
+    else {
+        return '';
+    }
 }
 async function run(inputs) {
     try {
diff --git a/package.json b/package.json
index 2520842..dfd292a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "setup-haskell",
-  "version": "2.3.6",
+  "version": "2.6.1",
   "private": true,
   "description": "setup haskell action",
   "main": "lib/setup-haskell",
diff --git a/src/setup-haskell.ts b/src/setup-haskell.ts
index b8d0134..4c84bfe 100644
--- a/src/setup-haskell.ts
+++ b/src/setup-haskell.ts
@@ -16,16 +16,32 @@ async function cabalConfig(): Promise<string> {
     silent: true,
     listeners: {stdout: append, stderr: append}
   });
-  // The last line of the cabal help text is printing the config file, e.g.:
-  //
-  // > You can edit the cabal configuration file to set defaults:
-  // >   <<HOME>>/.cabal/config
-  //
-  // So trimming the last line will give us the name of the config file.
-  //
-  // Needless to say this is very brittle, but we secure this by a test
-  // in Cabal's testsuite:  https://github.com/haskell/cabal/pull/9614
-  return out.toString().trim().split('\n').slice(-1)[0].trim();
+  return configFileFromHelpText(out.toString());
+}
+
+// The end of the cabal help text is printing the config file, e.g.:
+//
+// > You can edit the cabal configuration file to set defaults:
+// >   <<HOME>>/.cabal/config
+// > This file will be generated with sensible defaults if you run 'cabal update'.
+//
+// The last line here is only printed if the file does not exist yet.
+//
+// So trimming last following "You can edit..." will give us the name of the config file.
+//
+// Needless to say this is very brittle, but we secure this by a test
+// in Cabal's testsuite:  https://github.com/haskell/cabal/pull/9614
+//
+function configFileFromHelpText(txt: string): string {
+  const marker = 'You can edit the cabal configuration file to set defaults:';
+  const lines = txt.split('\n').map(line => line.trim());
+  const foundIndex = lines.findLastIndex(line => line === marker);
+
+  if (foundIndex !== -1 && foundIndex + 1 < lines.length) {
+    return lines[foundIndex + 1];
+  } else {
+    return '';
+  }
 }
 
 export default async function run(
diff --git a/tsconfig.json b/tsconfig.json
index 884961f..d95a52c 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,7 +1,7 @@
 {
   "compilerOptions": {
-    "target": "es2021",
-    "lib": ["ES2021"],
+    "target": "esnext",
+    "lib": ["ESNEXT"],
     "module": "commonjs",
     "incremental": true,
     "strict": true,