Skip to content

Commit 0d8803f

Browse files
RyanZimCopilot
andauthored
BREAKING: exit on all types of configuration errors in watch mode (#527)
* BREAKING: exit on all types of configuration errors in watch mode * Test errors in watch mode * Remove hanging single quote Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 79f50cf commit 0d8803f

2 files changed

Lines changed: 68 additions & 55 deletions

File tree

index.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ async function buildCliConfig() {
4545
try {
4646
return (await import(plugin)).default()
4747
} catch (e) {
48-
const msg = e.message || `Cannot find module '${plugin}'`
48+
const msg = e.message || `Unknown error in '${plugin}'`
4949
let prefix = msg.includes(plugin) ? '' : ` (${plugin})`
5050
if (e.name && e.name !== 'Error') prefix += `: ${e.name}`
51-
return error(`Plugin Error${prefix}: ${msg}'`)
51+
error(`Plugin Error${prefix}: ${msg}`)
5252
}
5353
}),
5454
)
@@ -85,8 +85,6 @@ buildCliConfig()
8585
.then(() => {
8686
if (argv.watch && !(argv.output || argv.replace || argv.dir)) {
8787
error('Cannot write to stdout in watch mode')
88-
// Need to explicitly exit here, since error() doesn't exit in watch mode
89-
process.exit(1)
9088
}
9189

9290
if (input && input.length) {
@@ -158,15 +156,14 @@ buildCliConfig()
158156
return files([...new Set(recompile)])
159157
.then((results) => watcher.add(dependencies(results)))
160158
.then(printMessage)
161-
.catch(error)
159+
.catch((err) => {
160+
// Watch mode shouldn't exit on file processing error
161+
error(err, argv.watch)
162+
})
162163
})
163164
}
164165
})
165-
.catch((err) => {
166-
error(err)
167-
168-
process.exit(1)
169-
})
166+
.catch(error)
170167

171168
function rc(ctx, path) {
172169
if (argv.use) return Promise.resolve(cliConfig)
@@ -331,7 +328,7 @@ function printVerbose(message) {
331328
if (argv.verbose) console.warn(message)
332329
}
333330

334-
function error(err) {
331+
function error(err, dontExit) {
335332
// Seperate error from logging output
336333
if (argv.verbose) console.error()
337334

@@ -342,8 +339,7 @@ function error(err) {
342339
} else {
343340
console.error(err)
344341
}
345-
// Watch mode shouldn't exit on error
346-
if (argv.watch) return
342+
if (dontExit) return
347343
process.exit(1)
348344
}
349345

test/error.js

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,68 @@ import test from 'ava'
33
import tmp from './helpers/tmp.js'
44
import cli from './helpers/cli.js'
55

6-
test('multiple input files && --output', (t) => {
7-
return cli(['test/fixtures/*.css', '-o', tmp()]).then(({ error, code }) => {
6+
// ensure that configuration errors are thrown in watch mode as well as normal mode
7+
;[false, true].forEach((watch) => {
8+
const prefix = watch ? 'watch mode: ' : ''
9+
const additionalArgs = watch ? ['--watch'] : []
10+
11+
test(`${prefix}multiple input files && --output`, (t) => {
12+
return cli(['test/fixtures/*.css', '-o', tmp(), ...additionalArgs]).then(
13+
({ error, code }) => {
14+
t.is(code, 1, 'expected non-zero error code')
15+
t.regex(error.toString(), /Input Error: Must use --dir or --replace/)
16+
},
17+
)
18+
})
19+
20+
test(`${prefix}plugin not found`, (t) => {
21+
return cli([
22+
'test/fixtures/a.css',
23+
'-u',
24+
'postcss-plugin',
25+
'-o',
26+
tmp(),
27+
...additionalArgs,
28+
]).then(({ error, code }) => {
29+
t.is(code, 1, 'expected non-zero error code')
30+
t.regex(
31+
error.toString(),
32+
/Plugin Error: Cannot find package 'postcss-plugin'/,
33+
)
34+
})
35+
})
36+
37+
test(`${prefix}plugin throws on require`, (t) => {
38+
return cli([
39+
'test/fixtures/a.css',
40+
'-u',
41+
'./test/fixtures/_bad-plugin.js',
42+
'-o',
43+
tmp(),
44+
...additionalArgs,
45+
]).then(({ error, code }) => {
46+
t.is(code, 1, 'expected non-zero error code')
47+
t.regex(error.toString(), /Plugin Error \(.*bad-plugin.js\): This fails/)
48+
})
49+
})
50+
51+
test(`${prefix}fails on invalid explicit config`, async (t) => {
52+
const output = tmp('output-ignore.css')
53+
54+
const { stderr, code } = await cli([
55+
'test/fixtures/a.css',
56+
'-o',
57+
output,
58+
'--config',
59+
'/foo/bar',
60+
...additionalArgs,
61+
])
862
t.is(code, 1, 'expected non-zero error code')
9-
t.regex(error.toString(), /Input Error: Must use --dir or --replace/)
63+
t.regex(stderr, /No PostCSS Config found/)
1064
})
1165
})
1266

67+
// These errors cannot occur in watch mode; watch mode does not support stdout
1368
test('multiple input files && writing to stdout', (t) => {
1469
return cli(['test/fixtures/*.css']).then(({ error, code }) => {
1570
t.is(code, 1, 'expected non-zero error code')
@@ -27,31 +82,7 @@ test('--map && writing to stdout', (t) => {
2782
})
2883
})
2984

30-
test('plugin not found', (t) => {
31-
return cli(['test/fixtures/a.css', '-u', 'postcss-plugin', '-o', tmp()]).then(
32-
({ error, code }) => {
33-
t.is(code, 1, 'expected non-zero error code')
34-
t.regex(
35-
error.toString(),
36-
/Plugin Error: Cannot find package 'postcss-plugin'/,
37-
)
38-
},
39-
)
40-
})
41-
42-
test('plugin throws on require', (t) => {
43-
return cli([
44-
'test/fixtures/a.css',
45-
'-u',
46-
'./test/fixtures/_bad-plugin.js',
47-
'-o',
48-
tmp(),
49-
]).then(({ error, code }) => {
50-
t.is(code, 1, 'expected non-zero error code')
51-
t.regex(error.toString(), /Plugin Error \(.*bad-plugin.js\): This fails/)
52-
})
53-
})
54-
85+
// Watch mode does not exit on CssSyntaxError, this is tested in ./watch.js
5586
test('CssSyntaxError', (t) => {
5687
return cli(['test/fixtures/a.css', '--parser', 'sugarss', '-o', tmp()]).then(
5788
({ error, code }) => {
@@ -63,17 +94,3 @@ test('CssSyntaxError', (t) => {
6394
},
6495
)
6596
})
66-
67-
test('fails on invalid explicit config', async (t) => {
68-
const output = tmp('output-ignore.css')
69-
70-
const { stderr, code } = await cli([
71-
'test/fixtures/a.css',
72-
'-o',
73-
output,
74-
'--config',
75-
'/foo/bar',
76-
])
77-
t.is(code, 1, 'expected non-zero error code')
78-
t.regex(stderr, /No PostCSS Config found/)
79-
})

0 commit comments

Comments
 (0)