OILS / doc / error-catalog.md View on Github | oilshell.org

347 lines, 246 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 `test/{parse,runtime,ysh-parse,ysh-runtime}-errors.sh`. Add an HTML
33 comment `<!-- -->` about that.
34 - Link to relevant sections in the [**Oils Reference**](ref/index.html).
351. Optionally, add your name to the acknowledgements list at the end of this
36 doc.
37
38Note that error messages are **hard** to write, because a single error could
39result from many different user **intentions**!
40
41### To Preview this Doc
42
43Right now I use this command:
44
45 build/doc.sh split-and-render doc/error-catalog.md
46
47Then paste this into your browser:
48
49 file:///home/andy/git/oilshell/oil/_release/VERSION/doc/error-catalog.html
50
51(Replace with your home dir)
52
53## Parse Errors - Rejected Input
54
55Roughly speaking, a parse error means that text input was **rejected**, so the
56shell didn't try to do anything.
57
58Examples:
59
60 echo ) # Shell syntax error
61
62 type -z # -z flag not accepted
63
64These error codes start at `10`.
65
66### OILS-ERR-10
67
68<!--
69Generated with:
70test/ysh-parse-errors.sh test-func-var-checker
71-->
72
73```
74 setvar x = true
75 ^
76[ -c flag ]:3: setvar couldn't find matching 'var x' (OILS-ERR-10)
77```
78
79- Did you forget to declare the name with the [var](ref/chap-cmd-lang.html#var)
80 keyword?
81- Did you mean to use the [setglobal](ref/chap-cmd-lang.html#setglobal)
82 keyword?
83
84Related help topics:
85
86- [setvar](ref/chap-cmd-lang.html#setvar)
87
88### OILS-ERR-11
89
90<!--
91Generated with:
92test/ysh-parse-errors.sh ysh_c_strings (this may move)
93-->
94
95```
96 echo $'\z'
97 ^
98[ -c flag ]:1: Invalid char escape in C-style string literal (OILS-ERR-11)
99```
100
101- Did you mean `$'\\z'`? Backslashes must be escaped in `$''` and `u''` and
102 `b''` strings.
103- Did you mean something like `$'\n'`? Only valid escapes are accepted in YSH.
104
105Related help topics:
106
107- [osh-string](ref/chap-word-lang.html#osh-string) (word language)
108- [ysh-string](ref/chap-expr-lang.html#ysh-string) (expression language)
109
110### OILS-ERR-12
111
112<!--
113Generated with:
114test/ysh-parse-errors.sh ysh_dq_strings (this may move)
115-->
116
117```
118 echo "\z"
119 ^
120[ -c flag ]:1: Invalid char escape in double quoted string (OILS-ERR-12)
121```
122
123- Did you mean `"\\z"`? Backslashes must be escaped in double-quoted strings.
124- Did you mean something like `"\$"`? Only valid escapes are accepted in YSH.
125
126Related help topics:
127
128- [osh-string](ref/chap-word-lang.html#osh-string) (word language)
129- [ysh-string](ref/chap-expr-lang.html#ysh-string) (expression language)
130
131### OILS-ERR-13
132
133<!--
134Generated with:
135test/ysh-parse-errors.sh ysh_bare_words (this may move)
136-->
137
138```
139 echo \z
140 ^~
141[ -c flag ]:1: Invalid char escape in unquoted word (OILS-ERR-13)
142```
143
144- Did you mean `\\z`? Backslashes must be escaped in unquoted words.
145- Did you mean something like `\$`? Only valid escapes are accepted in YSH.
146
147### OILS-ERR-14
148
149<!--
150Generated with:
151test/ysh-parse-errors.sh test-parse-dparen
152-->
153
154```
155 if ((1 > 0 && 43 > 42)); then echo yes; fi
156 ^~
157[ -c flag ]:1: Bash (( not allowed in YSH (parse_dparen, see OILS-ERR-14 for wart)
158```
159
160Two likely causes:
161
162- Do you need to rewrite bash arithmetic as YSH arithmetic (which is
163 Python-like)?
164- Do you need to work around an [unfortunate wart](warts.html#two-left-parens-should-be-separated-by-space) in YSH?
165
166Examples:
167
168 if (1 > 0 and 43 > 42) { # YSH-style
169 echo yes
170 }
171
172 if ( (x + 1) < n) { # space between ( ( avoids ((
173 echo yes
174 }
175
176## Runtime Errors - Traditional Shell
177
178These errors may occur in shells like [bash]($xref) and [zsh]($xref).
179
180They're numbered starting from `100`. (If we have more than 90 parse errors,
181we can start a new section, like `300`.)
182
183### OILS-ERR-100
184
185<!--
186Generated with:
187test/runtime-errors.sh test-command-not-found
188-->
189
190```
191 findz
192 ^~~~~
193[ -c flag ]:1: 'findz' not found (OILS-ERR-100)
194```
195
196- Did you misspell a command name?
197- Did you misspell a shell function or a YSH `proc`?
198- Is the file in your `$PATH`? The `PATH` variable is a colon-separated list
199 of directories, where executable files may live.
200- Is `findz` file executable bit set? (`chmod +x`)
201
202### OILS-ERR-101
203
204<!--
205Generated with:
206test/runtime-errors.sh test-assoc-array
207-->
208
209Let's look at **three** instances of this error.
210
211```
212 declare -A assoc; assoc[x]=1
213 ^~~~~~
214[ -c flag ]:1: fatal: Assoc array keys must be strings: $x 'x' "$x" etc. (OILS-ERR-101)
215```
216
217- Is `x` a string? Then add quotes: `assoc['x']=1`
218- Is `x` a variable? Then write: `assoc[$x]=1`
219
220---
221
222Same idea here:
223
224```
225 declare -A assoc; echo ${assoc[x]}
226 ^
227[ -c flag ]:1: fatal: Assoc array keys must be strings: $x 'x' "$x" etc. (OILS-ERR-101)
228```
229
230- Is `x` a string? Then add quotes: `${assoc['x']}`
231- Is `x` a variable? Then write: `${assoc[$x]}`
232
233---
234
235The third example is **tricky** because `unset` takes a **string**. There's an
236extra level of parsing, which:
237
238- Implies an extra level of quoting
239- Causes OSH to display the following **nested** error message
240
241```
242 assoc[k]
243 ^
244[ dynamic LHS word at line 1 of [ -c flag ] ]:1
245
246 declare -A assoc; key=k; unset "assoc[$key]"
247 ^
248[ -c flag ]:1: fatal: Assoc array keys must be strings: $x 'x' "$x" etc. (OILS-ERR-101)
249```
250
251To fix it, consider using **single quotes**:
252
253 unset 'assoc[$key]'
254
255---
256
257- This is the error in [Parsing Bash is
258 Undecidable](https://www.oilshell.org/blog/2016/10/20.html) (2016)
259- Also mentioned in [Known Differences](known-differences.html)
260
261
262## Runtime Errors - Oils and YSH
263
264These errors don't occur in shells like [bash]($xref) and [zsh]($xref).
265
266They may involve Python-like **expressions** and **typed data**.
267
268They're numbered starting from `200`.
269
270### OILS-ERR-200
271
272<!--
273Generated with:
274test/runtime-errors.sh test-external_cmd_typed_args
275-->
276
277```
278 cat ("myfile")
279 ^
280[ -c flag ]:1: fatal: 'cat' appears to be external. External commands don't accept typed args (OILS-ERR-200)
281```
282
283- Builtin commands and user-defined procs may accept [typed
284 args](ref/chap-cmd-lang.html#typed-arg), but external commands never do.
285- Did you misspell a [YSH proc](ref/chap-cmd-lang.html#proc-def)? If a name is
286 not found, YSH assumes it's an external command.
287- Did you forget to source a file that contains the proc or shell function you
288 wanted to run?
289
290### OILS-ERR-201
291
292<!--
293Generated with:
294test/runtime-errors.sh test-arith_ops_str
295-->
296
297```
298 = "age: " + "100"
299 ^
300[ -c flag ]:1: fatal: Binary operator expected numbers, got Str and Str (OILS-ERR-201)
301
302 = 100 + myvar
303 ^
304[ -c flag ]:2: fatal: Binary operator expected numbers, got Int and Str (OILS-ERR-201)
305```
306
307- Did you mean to use `++` to concatenate strings/lists?
308- The arithmetic operators [can coerce string operands to
309 numbers](ref/chap-expr-lang.html#ysh-arith). However, if you are operating on
310 user provided input, it may be a better idea to first parse that input with
311 [`int()`](ref/chap-builtin-func.html#int) or
312 [`float()`](ref/chap-builtin-func.html#float).
313
314### OILS-ERR-202
315
316<!--
317Generated with:
318test/ysh-runtime-errors.sh test-float-equality
319-->
320
321```
322 pp (42.0 === x)
323 ^~~
324[ -c flag ]:3: fatal: Equality isn't defined on Float values (OILS-ERR-202)
325```
326
327Floating point numbers shouldn't be tested for equality. Alternatives:
328
329 = abs(42.0 - x) < 0.1
330 = floatEquals(42.0, x)
331
332## Appendix
333
334### Kinds of Errors from Oils
335
336- Runtime errors (status 1) - the shell tried to do something, but failed.
337 - Example: `echo hi > /does/not/exist`
338- Parse errors and builtin usage errors (status 2) - input rejected, so the
339 shell didn't try to do anything.
340- Uncaught I/O errors (status 2)
341- Expression errors (status 3)
342- User errors from the `error` builtin (status 10 is default)
343
344### Contributors
345
346(If you updated this doc, feel free to add your name to the end of this list.)
347