feat: support shell-style # comments#182
Merged
Merged
Conversation
Recognize `#` as a comment delimiter at word boundaries (start of a line/command or following whitespace or a metacharacter), matching POSIX shell behavior. Everything from the `#` to the end of the line is ignored. A `#` in the middle of a word (`foo#bar`) or inside quotes (`'# x'`, `"# x"`) remains a literal character. Closes denoland/deno#27644
dsherret
reviewed
Jun 4, 2026
| loop { | ||
| let (rest, _) = skip_whitespace(current)?; | ||
| let rest = skip_comment(rest); | ||
| if rest == current { |
Contributor
There was a problem hiding this comment.
Nitpick: This could instead do rest.len() == current.len() for a faster comparison
Member
Author
There was a problem hiding this comment.
Good call — switched to a length comparison in 7323235.
The `or(tag(">"), tag(">|"))` ordering let `>` match the prefix of
`>|`, leaving the `|` to be misparsed as a pipe. Try `>|` first so the
clobber operator parses as an overwrite redirect (noclobber is not
modeled, so `>|` behaves identically to `>`).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements shell-style
#comments in task commands, requested indenoland/deno#27644. Previously a command like
echo foo # commentexecuted the entire string literally instead of stopping at
#.A
#now begins a comment wherever a shell would recognize one: at thestart of a line or command, or following whitespace or a metacharacter.
Everything from the
#to the end of the line is ignored. A#in themiddle of a word (
foo#bar) or inside single/double quotes ('# x',"# x") stays a literal character, so existing commands are unaffected.The implementation keeps comment handling at the parser's existing word
boundaries.
skip_inline_whitespacenow also consumes a trailing#-comment up to the next newline, and a new comment-awareskip_whitespace_and_commentsreplaces the plain whitespace skip at theremaining boundary positions: the start of a sequential list, after a
newline separator, after
&&/||, after|/|&, and at the start of asubshell. This covers trailing comments, whole comment lines interspersed
with commands, and comments following operator continuations and inside
subshells.
Note the
\#"Unsupported reserved word" error mentioned in the issue nolonger applies, since real comment support removes the need to escape
#.While adding comment support surfaced two adjacent redirect-parsing bugs,
both are fixed here:
echo foo >,cat <, ora comment beginning right after the operator as in
echo foo > #c) nowfails with
Expected redirect path.instead of silently producing aredirect with an empty target word that only errored later at runtime.
>|(clobber) now parses. Theor(tag(">"), tag(">|"))ordering let>match the prefix first, leaving the|to be misparsed as a pipe;>|is now tried first and, since noclobber is not modeled, behavesidentically to
>.Tests are added at both layers: parser unit tests asserting the parsed
AST for each case (comments, the missing-path error, and
>|), and anintegration test verifying end-to-end stdout for the comment cases.