OILS / doc / error-catalog.md View on Github | oils.pub

507 lines, 360 significant
1---
2default_highlighter: oils-sh
3---
4
5Oils Error Catalog, With Hints
6==================
7
8This doc lists errors from Oils (both [OSH]($xref) and [YSH]($xref)), with
9hints to help you fix them.
10
11Each error is associated with a code like `OILS-ERR-42`, a string that search
12engines should find.
13
14<!--
15Later, we could have a URL shortener, like https://oils.err/42
16-->
17
18<div id="toc">
19</div>
20
21## How to Contribute
22
23If you see an error that you don't understand:
24
251. Ask a question on `#oil-help` on [Zulip]($xref:zulip). What's the problem,
26 and what's the solution?
271. Then `grep` the source code for the confusing error message. Tag it with a
28 string like `OILS-ERR-43`, picking a new number according to the conventions
29 below.
301. Add a tagged section below, with hints and explanations.
31 - Quote the error message. You may want copy and paste from the output of
32 `doc/error-catalog.sh`, or
33 `test/{parse,runtime,ysh-parse,ysh-runtime}-errors.sh`. Add an HTML
34 comment `<!-- -->` about that.
35 - Link to relevant sections in the [**Oils Reference**](ref/index.html).
361. Optionally, add your name to the acknowledgements list at the end of this
37 doc.
38
39Note that error messages are **hard** to write, because a single error could
40result from many different user **intentions**!
41
42### To Preview this Doc
43
44Right now I use this command:
45
46 build/doc.sh split-and-render doc/error-catalog.md
47
48Then paste this into your browser:
49
50 file:///home/andy/git/oilshell/oil/_release/VERSION/doc/error-catalog.html
51
52(Replace with your home dir)
53
54## Parse Errors - Rejected Input
55
56Roughly speaking, a parse error means that text input was **rejected**, so the
57shell didn't try to do anything.
58
59Examples:
60
61 echo ) # Shell syntax error
62
63 type -z # -z flag not accepted
64
65These error codes start at `10`.
66
67### OILS-ERR-10
68
69<!--
70Generated with:
71test/ysh-parse-errors.sh test-func-var-checker
72-->
73
74```
75 setvar x = true
76 ^
77[ -c flag ]:3: setvar couldn't find matching 'var x' (OILS-ERR-10)
78```
79
80- Did you forget to declare the name with the [var](ref/chap-ysh-cmd.html#var)
81 keyword?
82- Did you mean to use the [setglobal](ref/chap-ysh-cmd.html#setglobal)
83 keyword?
84
85Related help topics:
86
87- [setvar](ref/chap-ysh-cmd.html#setvar)
88
89### OILS-ERR-11
90
91<!--
92Generated with:
93test/ysh-parse-errors.sh ysh_c_strings (this may move)
94-->
95
96```
97 echo $'\z'
98 ^
99[ -c flag ]:1: Invalid char escape in C-style string literal (OILS-ERR-11)
100```
101
102- Did you mean `$'\\z'`? Backslashes must be escaped in `$''` and `u''` and
103 `b''` strings.
104- Did you mean something like `$'\n'`? Only valid escapes are accepted in YSH.
105
106Related help topics:
107
108- [osh-string](ref/chap-word-lang.html#osh-string) (word language)
109- [ysh-string](ref/chap-expr-lang.html#ysh-string) (expression language)
110
111### OILS-ERR-12
112
113<!--
114Generated with:
115test/ysh-parse-errors.sh ysh_dq_strings (this may move)
116-->
117
118```
119 echo "\z"
120 ^
121[ -c flag ]:1: Invalid char escape in double quoted string (OILS-ERR-12)
122```
123
124- Did you mean `"\\z"`? Backslashes must be escaped in double-quoted strings.
125- Did you mean something like `"\$"`? Only valid escapes are accepted in YSH.
126- Did you to use single quotes, like `u'\n'` rather than `u"\n"`?
127
128Related help topics:
129
130- [osh-string](ref/chap-word-lang.html#osh-string) (word language)
131- [ysh-string](ref/chap-expr-lang.html#ysh-string) (expression language)
132
133### OILS-ERR-13
134
135<!--
136Generated with:
137test/ysh-parse-errors.sh ysh_bare_words (this may move)
138-->
139
140```
141 echo \z
142 ^~
143[ -c flag ]:1: Invalid char escape in unquoted word (OILS-ERR-13)
144```
145
146- Did you mean `\\z`? Backslashes must be escaped in unquoted words.
147- Did you mean something like `\$`? Only valid escapes are accepted in YSH.
148
149### OILS-ERR-14
150
151<!--
152Generated with:
153test/ysh-parse-errors.sh test-parse-dparen
154-->
155
156```
157 if ((1 > 0 && 43 > 42)); then echo yes; fi
158 ^~
159[ -c flag ]:1: Bash (( not allowed in YSH (parse_dparen, see OILS-ERR-14 for wart)
160```
161
162Two likely causes:
163
164- Do you need to rewrite bash arithmetic as YSH arithmetic (which is
165 Python-like)?
166- Do you need to work around an [unfortunate wart](warts.html#two-left-parens-should-be-separated-by-space) in YSH?
167
168Examples:
169
170 if (1 > 0 and 43 > 42) { # YSH-style
171 echo yes
172 }
173
174 if ( (x + 1) < n) { # space between ( ( avoids ((
175 echo yes
176 }
177
178### OILS-ERR-15
179
180```
181 if (a || b && c) {
182 ^~
183[ -c flag ]:2: Use 'or' in expression mode (OILS-ERR-15)
184```
185
186Expression mode uses `not or and`, rather than `! || &&`. See [Command vs.
187Expression Mode](command-vs-expression-mode.html) for details.
188
189
190No:
191
192 if (!a || b && c) {
193 echo no
194 }
195
196Yes:
197
198 if (not a or b and c) {
199 echo yes
200 }
201
202
203Command mode is the opposite; it uses `! || &&`, rather than `not or and`:
204
205No:
206
207 # Command mode
208 if not test --dir a or test --dir b and test --dir c {
209 echo no
210 }
211
212Yes:
213
214 # Command mode
215 if ! test --dir a || test --dir b && test --dir c {
216 echo yes
217 }
218
219### OILS-ERR-16
220
221```
222 for x in (1 .. 5) {
223 ^~
224[ -c flag ]:1: Use ..< for half-open range, or ..= for closed range (OILS-ERR-16)
225```
226
227<!--
228Similar to
229test/ysh-parse-errors.sh test-expr-range
230-->
231
232There are two ways to construct a [Range](ref/chap-expr-lang#range). The `..<`
233operator is for half-open ranges and the `..=` operator is for closed ranges:
234
235 for i in (0 ..< 3) {
236 echo $i
237 }
238 => 0
239 => 1
240 => 2
241
242 for i in (0 ..= 3) {
243 echo $i
244 }
245 => 0
246 => 1
247 => 2
248 => 3
249
250## Runtime Errors - Traditional Shell
251
252These errors may occur in shells like [bash]($xref) and [zsh]($xref).
253
254They're numbered starting from `100`. (If we have more than 90 parse errors,
255we can start a new section, like `300`.)
256
257### OILS-ERR-100
258
259<!--
260Generated with:
261test/runtime-errors.sh test-command-not-found
262-->
263
264```
265 findz
266 ^~~~~
267[ -c flag ]:1: Command 'findz' not found (OILS-ERR-100)
268```
269
270The shell tried to execute an external command, but couldn't.
271
272- Did you misspell a command name?
273- Did you misspell a shell function or a YSH `proc`?
274- Is the file in your `$PATH`? The `PATH` variable is a colon-separated list
275 of directories, where executable files may live.
276- Is `findz` file executable bit set? (`chmod +x`)
277
278### OILS-ERR-101
279
280<!--
281Generated with:
282test/runtime-errors.sh test-assoc-array
283-->
284
285Let's look at **three** instances of this error.
286
287```
288 declare -A assoc; assoc[x]=1
289 ^~~~~~
290[ -c flag ]:1: fatal: Assoc array keys must be strings: $x 'x' "$x" etc. (OILS-ERR-101)
291```
292
293- Is `x` a string? Then add quotes: `assoc['x']=1`
294- Is `x` a variable? Then write: `assoc[$x]=1`
295
296---
297
298Same idea here:
299
300```
301 declare -A assoc; echo ${assoc[x]}
302 ^
303[ -c flag ]:1: fatal: Assoc array keys must be strings: $x 'x' "$x" etc. (OILS-ERR-101)
304```
305
306- Is `x` a string? Then add quotes: `${assoc['x']}`
307- Is `x` a variable? Then write: `${assoc[$x]}`
308
309---
310
311The third example is **tricky** because `unset` takes a **string**. There's an
312extra level of parsing, which:
313
314- Implies an extra level of quoting
315- Causes OSH to display the following **nested** error message
316
317```
318 assoc[k]
319 ^
320[ dynamic LHS word at line 1 of [ -c flag ] ]:1
321
322 declare -A assoc; key=k; unset "assoc[$key]"
323 ^
324[ -c flag ]:1: fatal: Assoc array keys must be strings: $x 'x' "$x" etc. (OILS-ERR-101)
325```
326
327To fix it, consider using **single quotes**:
328
329 unset 'assoc[$key]'
330
331---
332
333- This is the error in [Parsing Bash is
334 Undecidable](https://www.oilshell.org/blog/2016/10/20.html) (2016)
335- Also mentioned in [Known Differences](known-differences.html)
336
337
338### OILS-ERR-102
339
340```
341 var cmd = ^(seq 3)
342 ^~~
343[ stdin ]:1: Command 'seq' not found in pure mode (OILS-ERR-102)
344```
345
346The shell tried to execute a command in pure mode, but couldn't.
347
348In pure mode, only user-defined procs and a few builtin commands can be the "first word".
349
350- Did you misspell a proc name?
351- Are you trying to run an external command? Such commands aren't allowed in
352 pure mode.
353
354## Runtime Errors - Oils and YSH
355
356These errors don't occur in shells like [bash]($xref) and [zsh]($xref).
357
358They may involve Python-like **expressions** and **typed data**.
359
360They're numbered starting from `200`.
361
362### OILS-ERR-200
363
364<!--
365Generated with:
366test/runtime-errors.sh test-external_cmd_typed_args
367-->
368
369```
370 cat ("myfile")
371 ^
372[ -c flag ]:1: fatal: 'cat' appears to be external. External commands don't accept typed args (OILS-ERR-200)
373```
374
375- Builtin commands and user-defined procs may accept [typed
376 args](ref/chap-cmd-lang.html#typed-arg), but external commands never do.
377- Did you misspell a [YSH proc](ref/chap-cmd-lang.html#proc-def)? If a name is
378 not found, YSH assumes it's an external command.
379- Did you forget to source a file that contains the proc or shell function you
380 wanted to run?
381
382### OILS-ERR-201
383
384<!--
385Generated with:
386test/runtime-errors.sh test-arith_ops_str
387-->
388
389```
390 = "age: " + "100"
391 ^
392[ -c flag ]:1: fatal: Binary operator expected numbers, got Str and Str (OILS-ERR-201)
393
394 = 100 + myvar
395 ^
396[ -c flag ]:2: fatal: Binary operator expected numbers, got Int and Str (OILS-ERR-201)
397```
398
399- Did you mean to use `++` to concatenate strings/lists?
400- The arithmetic operators [can coerce string operands to
401 numbers](ref/chap-expr-lang.html#ysh-arith). However, if you are operating on
402 user provided input, it may be a better idea to first parse that input with
403 [`int()`](ref/chap-builtin-func.html#int) or
404 [`float()`](ref/chap-builtin-func.html#float).
405
406### OILS-ERR-202
407
408<!--
409Generated with:
410test/ysh-runtime-errors.sh test-float-equality
411-->
412
413```
414 pp (42.0 === x)
415 ^~~
416[ -c flag ]:3: fatal: Equality isn't defined on Float values (OILS-ERR-202)
417```
418
419Floating point numbers shouldn't be tested for equality. Alternatives:
420
421 = abs(42.0 - x) < 0.1
422 = floatEquals(42.0, x)
423
424### OILS-ERR-203
425
426<!--
427Generated with:
428test/ysh-runtime-errors.sh test-cannot-stringify-list
429-->
430
431```
432 var mylist = [1,2,3]; write $[mylist]
433 ^~
434[ -c flag ]:1: fatal: Expr sub got a List, which can't be stringified (OILS-ERR-203)
435```
436
437- Did you mean to use `@mylist` instead of `$mylist`?
438- Did you mean to use `@[myfunc()]` instead of `$[myfunc()]`?
439- Did you mean `$[join(mylist)]`?
440
441Or:
442
443- Do you have an element that can't be stringified in a list, like `['good',
444 {bad: true}]`?
445
446### OILS-ERR-204
447
448<!--
449Generated with:
450test/ysh-runtime-errors.sh test-purity
451-->
452
453```
454 x=$(date)
455 ^~
456impure.sh:1: fatal: Command subs aren't allowed in pure mode (OILS-ERR-204)
457```
458
459In **pure mode**, the shell can't do I/O. It's intended for config file
460evaluation and pure functions.
461
462- Did you mean to use `--eval` instead of `--eval-pure`?
463- Did you mean to use a `proc`, rather than a `func`?
464
465
466<!-- TODO -->
467
468## Runtime Errors: `strict:all`
469
470### OILS-ERR-300
471
472```
473 if ! ls | wc -l; then echo failed; fi
474 ^
475[ -c flag ]:1: fatal: Command conditionals should only have one status, not Pipeline (strict_errexit, OILS-ERR-300)
476```
477
478Compound commands can't be used as conditionals because it's ambiguous.
479
480It confuses true/false with pass/fail. What if part of the pipeline fails?
481What if `ls` doesn't exist?
482
483This YSH idiom is more explicit:
484
485 try {
486 ls | wc -l
487 }
488 if failed {
489 echo failed
490 }
491
492## Appendix
493
494### Kinds of Errors from Oils
495
496- Runtime errors (status 1) - the shell tried to do something, but failed.
497 - Example: `echo hi > /does/not/exist`
498- Parse errors and builtin usage errors (status 2) - input rejected, so the
499 shell didn't try to do anything.
500- Uncaught I/O errors (status 2)
501- Expression errors (status 3)
502- User errors from the `error` builtin (status 10 is default)
503
504### Contributors
505
506(If you updated this doc, feel free to add your name to the end of this list.)
507