-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shellcheck wrap variables #376
Conversation
Now is just:
We are working for the 2 issues in 2 different tickets, so it will be their turn soon. |
@@ -91,7 +91,11 @@ impl SyntaxModule<ParserMetadata> for IfCondition { | |||
impl TranslateModule for IfCondition { | |||
fn translate(&self, meta: &mut TranslateMetadata) -> String { | |||
let mut result = vec![]; | |||
result.push(format!("if [ {} != 0 ]; then", self.expr.translate(meta))); | |||
if self.expr.translate(meta).starts_with("\"") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if this is something that we want to handle here.
Probably to fix the various cases in this PR is enough that the translate method automatically wrap a variable so this check is not needed.
@Ph0enixKM I am right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added that check because it was generating if [ ""$__AF_file_exist1_v0__28_8"" != 0 ]; thenif [ ""$__AF_file_exist1_v0__28_8"" != 0 ]; then
stuff like that with a double "
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think its best to remove this check. if some part of code translates a thing unquoted, it should be fixed. if we add checks like this all over this place, it will be a giant mess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's my point but we can generate a bash like this so a check somewhere to not read them is needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so just do it like this without a check?
result.push(format!("if [ {} != 0 ]; then", self.expr.translate(meta)));
assuming that it should be translated quoted. if it's not translated quoted, then its an issue with the translator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that #283 this PR implemented in some cases to wrap the variable but not everywhere so I am fixing in this PR in the wrong place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do that check. Please find the root cause of what adds the quotes and fix it instead. If you need any help, let me know
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to find where it is happening, looking at the rest of changes I did in this PR there is somewhere where we escape but not like in all the cases.
@@ -82,11 +82,11 @@ impl TranslateModule for IfChain { | |||
let mut is_first = true; | |||
for (cond, block) in self.cond_blocks.iter() { | |||
if is_first { | |||
result.push(format!("if [ {} != 0 ]; then", cond.translate(meta))); | |||
result.push(format!("if [ \"{}\" != 0 ]; then", cond.translate(meta))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't look like this too. Unless there is a big reason why all the conditions should be wrapped in quotes
result.push(block.translate(meta)); | ||
is_first = false; | ||
} else { | ||
result.push(format!("elif [ {} != 0 ]; then", cond.translate(meta))); | ||
result.push(format!("elif [ \"{}\" != 0 ]; then", cond.translate(meta))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one too
@@ -54,7 +54,7 @@ impl TranslateModule for Ternary { | |||
let cond = self.cond.translate(meta); | |||
let true_expr = self.true_expr.translate(meta); | |||
let false_expr = self.false_expr.translate(meta); | |||
meta.gen_subprocess(&format!("if [ {} != 0 ]; then echo {}; else echo {}; fi", cond, true_expr, false_expr)) | |||
meta.gen_subprocess(&format!("if [ \"{}\" != 0 ]; then echo {}; else echo {}; fi", cond, true_expr, false_expr)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here...
@@ -50,7 +50,7 @@ pub fn translate_computation(meta: &TranslateMetadata, operation: ArithOp, left: | |||
ArithOp::Or => "||" | |||
}; | |||
let math_lib_flag = if math_lib_flag { "-l" } else { "" }; | |||
meta.gen_subprocess(&format!("echo {left} '{op}' {right} | bc {math_lib_flag} | sed '{sed_regex}'")) | |||
meta.gen_subprocess(&format!("echo \"{left}\" '{op}' \"{right}\" | bc {math_lib_flag} | sed '{sed_regex}'")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we just need to check for the inner variables if they are wrapped in string quotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I think that can resolve a lot of issues if we do at the top of the flow and not when it is time to print them
@Mte90 to fix the shell check warnings I propose you to go get the minimal Amber code needed, generate bash from it and then try to understand the root cause of this warning. Perhaps the function calls are not wrapped in double quotes or maybe just the variables? |
Taking as example the date_compare.ab test we have this issues:
We fix some of those issues (also in other tests) but I am still working on some of the warning here.