puppet-lint
puppet-lint copied to clipboard
Linter: ${} inside single quote strings
This is a little janky and will work better given #584 which makes splicing in lots of tokens possible, but it would be nice if '${foo}' raised a warning and could be rewritten automagically since it's just wrong.
It is not just wrong, it might be a snippet of shell code single quoted on purpose.
Yeah this is an issue, our module sets settings that call an internal environment variable, I don't mind it as a warning, but the thing is literal, even escaping the $ it still flags it because of the brackets {}. It's intentional that we're putting it in single quotes because its not a puppet variable and we don't want Puppet parsing it as a variable.
Can one of you provide a reproduction, what you expect, and the actual results you received? I want to make sure we are clear on the problem and expectation.
You can see where it's spitting an error for a variable, even when I escape the $ sign. I have my git pre-commit hook check it through Puppet Lint before it commits, and if it throws an error, it will not commit, so I end up having to take the linter check out of the pre-commit just to get it to pass, or turning off the variable check entirely. I don't mind a warning, as a "are you sure you meant to do this?", but it doesn't mean it should fail or that it's not intentional/good code.

Ah. It sounds like you are asking for two things then:
- The check
single_quote_string_with_variablesto be dropped fromerrortowarninglevel - The check
single_quote_string_with_variablesto parse\$as not a variable
Is that correct?
Yes, at least the second one or add a flag to demote it to a warning, and keep as error by default, for those of us who have those rare cases we need to put $variable in a single quoted string because we need it to be literally that.
Ex:
puppet-lint --warning-single_quote_string_with_variables-check
or something like that to have it be a warning condition instead of an error.
If I do $ORACLE_JDBC_DRIVER_PATH it doesn't flag, but if it has brackets, it ignores the fact the $ is escaped.

You see line 157 disappeared from the report, so it respects the escaped $ if there are no brackets.
To be clear, if I run a puppet parser validate on the same file, it passes. It's only Puppet Lint flagging it as an error that prevents me from committing it. I want Puppet Lint to check my files for all the other things it catches and there might be cases where I did accidentally put a Puppet variable in a single quoted string so it'd be good to be notified, which is why I don't want to turn off the check entirely.
@digital-shokunin thanks for the great detail! We can definitely work off that information.
In the meantime, can you try this flag with warning for the value? It's not perfect, but I think this may help you until this is patched. --error-level LEVEL The level of error to return (warning, error or all).
Was this issue resolved?
If I'm reading the old examples correctly, the behavior hasn't changed.
No error is reported if the variable name is not within curly braces -- regardless whether the $ sign is escaped:
$ cat gh/manifests/issue598_unescaped_without_curly.pp
## https://github.com/rodjek/puppet-lint/issues/598
class gh::issue598_unescaped_without_curly {
file { '/tmp/issue598':
ensure => present,
content => 'echo $HOME',
}
}
$ puppet-lint gh/manifests/issue598_unescaped_without_curly.pp; echo $?
0
$ cat gh/manifests/issue598_escaped_without_curly.pp
## https://github.com/rodjek/puppet-lint/issues/598
class gh::issue598_escaped_without_curly {
file { '/tmp/issue598':
ensure => present,
content => 'echo \$HOME',
}
}
$ puppet-lint gh/manifests/issue598_escaped_without_curly.pp; eho $?
0
$
But an error is reported if the variable name is within curly braces -- again regardless whether the $ sign is escaped:
$ cat gh/manifests/issue598_unescaped_with_curly.pp
## https://github.com/rodjek/puppet-lint/issues/598
class gh::issue598_unescaped_with_curly {
file { '/tmp/issue598':
ensure => present,
content => 'echo ${HOME}',
}
}
$ puppet-lint gh/manifests/issue598_unescaped_with_curly.pp; echo $?
ERROR: single quoted string containing a variable found on line 5
1
$ cat gh/manifests/issue598_escaped_with_curly.pp
## https://github.com/rodjek/puppet-lint/issues/598
class gh::issue598_escaped_with_curly {
file { '/tmp/issue598':
ensure => present,
content => 'echo \${HOME}',
}
}
$ puppet-lint gh/manifests/issue598_escaped_with_curly.pp; echo $?
ERROR: single quoted string containing a variable found on line 5
1