diff --git a/cli/run/batchWarnings.ts b/cli/run/batchWarnings.ts index 36d3afc41f2..1fe3d03ae30 100644 --- a/cli/run/batchWarnings.ts +++ b/cli/run/batchWarnings.ts @@ -189,8 +189,9 @@ const deferredHandlers: { for (const warning of items) { if (warning.url && warning.url !== lastUrl) info((lastUrl = warning.url)); - if (warning.id) { - let loc = relativeId(warning.id); + const id = warning.id || warning.loc?.file; + if (id) { + let loc = relativeId(id); if (warning.loc) { loc += `: (${warning.loc.line}:${warning.loc.column})`; } diff --git a/test/cli/samples/warn-plugin-loc/_config.js b/test/cli/samples/warn-plugin-loc/_config.js new file mode 100644 index 00000000000..8b8f52bb4e9 --- /dev/null +++ b/test/cli/samples/warn-plugin-loc/_config.js @@ -0,0 +1,17 @@ +const { assertIncludes } = require('../../../utils.js'); + +module.exports = { + description: 'correctly adds locations to plugin warnings', + command: 'rollup -c', + stderr: stderr => { + assertIncludes( + stderr, + '(!) Plugin test: Warning with file and id\n' + + 'file1: (1:2)\n' + + '(!) Plugin test: Warning with file\n' + + 'file2: (2:3)\n' + + '(!) Plugin test: Warning with id\n' + + 'file-id3: (3:4)\n' + ); + } +}; diff --git a/test/cli/samples/warn-plugin-loc/main.js b/test/cli/samples/warn-plugin-loc/main.js new file mode 100644 index 00000000000..983961490bb --- /dev/null +++ b/test/cli/samples/warn-plugin-loc/main.js @@ -0,0 +1 @@ +console.log("everyday I'm throwing"); diff --git a/test/cli/samples/warn-plugin-loc/rollup.config.js b/test/cli/samples/warn-plugin-loc/rollup.config.js new file mode 100644 index 00000000000..78f47655e95 --- /dev/null +++ b/test/cli/samples/warn-plugin-loc/rollup.config.js @@ -0,0 +1,29 @@ +const path = require('path'); + +module.exports = { + input: 'main.js', + output: { + format: 'cjs' + }, + plugins: [ + { + name: 'test', + buildStart() { + this.warn({ + message: 'Warning with file and id', + file: path.join(__dirname, 'file-id1'), + loc: { file: path.join(__dirname, 'file1'), line: 1, column: 2 } + }); + this.warn({ + message: 'Warning with file', + loc: { file: path.join(__dirname, 'file2'), line: 2, column: 3 } + }); + this.warn({ + message: 'Warning with id', + id: path.join(__dirname, 'file-id3'), + loc: { line: 3, column: 4 } + }); + } + } + ] +};