Skip to content
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

parse arguments in tasks.json #7895

Merged
merged 15 commits into from Sep 9, 2021
Merged

parse arguments in tasks.json #7895

merged 15 commits into from Sep 9, 2021

Conversation

elahehrashedi
Copy link
Contributor

bug fix: #7891
also partially related to: #6773

@sean-mcmanus the part related to showing errors and warnings might need some modification.

@elahehrashedi elahehrashedi requested a review from a team July 30, 2021 04:23
// Quote a file path if it includes space.
export function quoteFilePaths(path: string): string {
// Check if the path is not already quoted.
if (!/".*?"/.test(path) && !/'.*?'/.test(path)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Linux: Cannot use pkg-config arguments in task.json #7891 relates to use of backtick quoting (which is only supported on mac and linux), but I don't see any handling of backtick quoting here.
  • It looks like you are using a pattern to check if the entire path is surrounded by quotes. Quoted portions may be contained anywhere within an argument. If there is already quoting within an arg, placing quotes around the whole arg would requiring escaping those internal quotes. Beware if detecting internal quotes within an arg, as they should not be considered as quoting if escaped (\")
  • It's unnecessary to detect if the arg contains a path, as args that contain paths are no different from any other arg contents with regards to whether or not they require quoting. i.e. macros/defines may have spaces. (So, this function should be something like quoteArgument). Since it's perfectly acceptable to quote an argument that does not contain spaces, consider quoting ALL arguments (if not already fully quoted), and escaping any quoting within it. That might be easier than detecting if quoting is necessary. Backticks might be different - I suspect that must always fully quote an arg and cannot be escaped.
  • Single-quotes are supported only on Linux. They behave as literals on Windows, so shouldn't be checked for on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on our previous discussion:
Assumption: Backtick quoting must be around the entire argument. (Whereas single-quote and double-quote quoting can start in the middle of an argument. For example: -DTEST="TEST TEST", or -DTEST='TEST TEST' on Linux/Mac

The algorithm:

  1. Trim the arg to remove leading and trailing spaces.
  2. (On Linux/Mac) If completely enclosed within backtick quoting, leave it alone.
  3. Check if the string contains any escaped double-quote characters. If so, leave the arg alone, as the user is doing the quoting themselves. Adding additional quoting would change the behavior. (i.e. -DTEST="TEST TEST" is different from "-DTEST="TEST TEST"")
  4. On Linux or Mac, check if the string contains any unescaped single-quote characters. If so, leave the arg alone. (same reason as above).
  5. Scan for any spaces not preceded by a slash. Only if there is an un-escaped space is quoting needed.
  6. If any un-escaped space is found, remove the "" from any escaped spaces ("\ ", in case there were some as they will no longer be valid if quoted), and put double-quotes around the whole thing.

@sean-mcmanus
Copy link
Collaborator

What do you mean by "@sean-mcmanus the part related to showing errors and warnings might need some modification."? Can you test the scenarios to make sure they all work okay still?

@elahehrashedi
Copy link
Contributor Author

elahehrashedi commented Aug 13, 2021

@sean-mcmanus one scenario is not working fine. In a test project, GCC is throwing an error, but error is empty and stderr && !stdout is true (i.e. stderr has the error message in it and stdout doesn't have any content). But the current code identifies that as a warning. other scenarios were fine.

@sean-mcmanus
Copy link
Collaborator

@sean-mcmanus one scenario is not working fine. In a test project, GCC is throwing an error, but error is empty and stderr && !stdout is true (i.e. stderr has the error message in it and stdout doesn't have any content). But the current code identifies that as a warning. other scenarios were fine.

Can you change the code to correctly detect the error?

export function normalizeArg(arg: string): string {
arg = arg.trimLeft().trimRight();
// Check if the arg is enclosed in backtick,
// or includes escaped double-quotes,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really hard problem, actually... I think we really need a test case for this, so we can plug in various different scenarios to ensure they are supported.

Also, contrary to what I may have said (wrong) in a previous conversation, I think you want to check for any "unescaped" double-quotes here. We should ignore any arg with un-escaped quotes in it, as adding quotes around that (even if escaping them internally) changes the arg... For example:

$ g++ -E -dD -xc++ -DTESTDEFINE="asd asd" /dev/null | grep TESTDEFINE
#define TESTDEFINE asd asd

would become:

$ g++ -E -dD -xc++ "-DTESTDEFINE=\"asd asd\"" /dev/null | grep TESTDEFINE
#define TESTDEFINE "asd asd"

@sean-mcmanus
Copy link
Collaborator

@elahehrashedi Is this ready for code review now?

@elahehrashedi elahehrashedi merged commit 7516eef into main Sep 9, 2021
@elahehrashedi elahehrashedi deleted the elrashed/args branch September 9, 2021 20:37
@github-actions github-actions bot locked and limited conversation to collaborators Oct 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Linux: Cannot use pkg-config arguments in task.json
3 participants