Repository: mesonbuild/meson

Issue:
Duplicate kwargs warning doesn't print filename or lineno
```meson
project('duplicate kwarg', 'c')
a = library('liba', 'a.c')
b = library('libb', 'b.c')
executable('main', 'main.c', link_with: a, link_with: b)
```
Only the last` link_with:` has effect.

I guess this should be reported as an error.


Discussion:
This error (and the current warning) do not identify the meson.build file and line where the duplicate keywords is.  This is no fun with a large meson.build.
This was fixed by https://github.com/mesonbuild/meson/pull/1629, but we still don't print the filename and the line no.

Resolving patch:
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -1935,7 +1935,7 @@ def func_message(self, node, args, kwargs):
     @noKwargs
     def func_warning(self, node, args, kwargs):
         argstr = self.get_message_string_arg(node)
-        mlog.warning(argstr)
+        mlog.warning('%s in file %s, line %d' % (argstr, os.path.join(node.subdir, 'meson.build'), node.lineno))
 
     @noKwargs
     def func_error(self, node, args, kwargs):
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py
--- a/mesonbuild/mparser.py
+++ b/mesonbuild/mparser.py
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import re
+import os, re
 from .mesonlib import MesonException
 from . import mlog
 
@@ -368,7 +368,7 @@ def append(self, statement):
 
     def set_kwarg(self, name, value):
         if name in self.kwargs:
-            mlog.warning('Keyword argument "%s" defined multiple times. This will be a an error in future Meson releases.' % name)
+            mlog.warning('Keyword argument "%s" defined multiple times in file %s, line %d. This will be an error in future Meson releases.' % (name, os.path.join(self.subdir, 'meson.build'), self.lineno))
         self.kwargs[name] = value
 
     def num_args(self):
diff --git a/run_unittests.py b/run_unittests.py
--- a/run_unittests.py
+++ b/run_unittests.py
@@ -1707,6 +1707,14 @@ def test_subproject_promotion(self):
         self.init(workdir)
         self.build()
 
+    def test_warning_location(self):
+        tdir = os.path.join(self.unit_test_dir, '20 warning location')
+        out = self.init(tdir)
+        self.assertRegex(out, r'WARNING: Keyword argument "link_with" defined multiple times in file meson.build, line 4')
+        self.assertRegex(out, r'WARNING: Keyword argument "link_with" defined multiple times in file sub' + re.escape(os.path.sep) + r'meson.build, line 3')
+        self.assertRegex(out, r'WARNING: a warning of some sort in file meson.build, line 6')
+        self.assertRegex(out, r'WARNING: subdir warning in file sub' + re.escape(os.path.sep) + r'meson.build, line 4')
+
 
 class FailureTests(BasePlatformTests):
     '''


Test patch:
diff --git a/test cases/unit/20 warning location/a.c b/test cases/unit/20 warning location/a.c
new file mode 100644
diff --git a/test cases/unit/20 warning location/b.c b/test cases/unit/20 warning location/b.c
new file mode 100644
diff --git a/test cases/unit/20 warning location/main.c b/test cases/unit/20 warning location/main.c
new file mode 100644
diff --git a/test cases/unit/20 warning location/meson.build b/test cases/unit/20 warning location/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/unit/20 warning location/meson.build	
@@ -0,0 +1,6 @@
+project('duplicate kwarg', 'c')
+a = library('liba', 'a.c')
+b = library('libb', 'b.c')
+executable('main', 'main.c', link_with: a, link_with: b)
+subdir('sub')
+warning('a warning of some sort')
diff --git a/test cases/unit/20 warning location/sub/c.c b/test cases/unit/20 warning location/sub/c.c
new file mode 100644
diff --git a/test cases/unit/20 warning location/sub/d.c b/test cases/unit/20 warning location/sub/d.c
new file mode 100644
diff --git a/test cases/unit/20 warning location/sub/meson.build b/test cases/unit/20 warning location/sub/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/unit/20 warning location/sub/meson.build	
@@ -0,0 +1,4 @@
+c = library('libc', 'c.c')
+d = library('libd', 'd.c')
+executable('sub', 'sub.c', link_with: c, link_with: d)
+warning('subdir warning')
diff --git a/test cases/unit/20 warning location/sub/sub.c b/test cases/unit/20 warning location/sub/sub.c
new file mode 100644