Skip to content

Conversation

@lrandersson
Copy link
Contributor

@lrandersson lrandersson commented Oct 30, 2025

Description

Checklist - did you ...

  • Add a file to the news directory (using the template) for the next release's release notes?
  • Add / update necessary tests?
  • Add / update outdated documentation?

@lrandersson lrandersson requested a review from a team as a code owner October 30, 2025 14:30
@conda-bot conda-bot added the cla-signed [bot] added once the contributor has signed the CLA label Oct 30, 2025
@github-project-automation github-project-automation bot moved this to 🆕 New in 🔎 Review Oct 30, 2025
assert output == [
'"cmd.exe"',
'/C',
'""echo" "Hello World""'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't this generated incorrectly? When I tried the generated command:

C:\Windows\System32>"cmd.exe" /C ""echo" "Hello World""
'"echo"' is not recognized as an internal or external command,
operable program or batch file.

What should be generated is the following:

C:\Windows\System32>"cmd.exe" /C "echo "Hello World""
"Hello World"

I have not fixed it in this PR but something I wanted to bring up, for now the test only verifies the incorrect behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would agree with you, echo should not be surrounded by quotes.

Copy link
Contributor Author

@lrandersson lrandersson Oct 31, 2025

Choose a reason for hiding this comment

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

I've fixed the issue in commit a36c3c1. I wonder however if can remove the function ensure_pad or change it. It is only used inside the method WinLex.quote_args, and only with the input argument pad = '"' (which is different from the default _). I see also that a copy of the same function exists inside the legacy code, but I would suggest:

  1. Rename to ensure_quoted or add_quotes_if_needed.
  2. Remove the pad keyword argument since we only use it with ".
  3. Leave the legacy function as is.
    However, not sure if this is a class imported by other menuinst users, if so, we could deprecate it.

Copy link
Contributor

Choose a reason for hiding this comment

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

A lot of users still use v1 shortcuts, so the legacy part needs to stay for now. I would also say to leave it as is until we have a deprecation plan.

I'm not sure if ensure_pad is the right place to make that change. Creating a special case there is a little opaque. Wouldn't quote_args be the better place, i.e., only call ensure_pad if there is a space in the string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you. I've made some fixes now.


# Don't touch shell meta tokens; quoting would change meaning.
# Example: echo %FOO%> output.txt
if cls._has_shell_meta(s):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was added because tests broke from my initial changes (in particular it was the or '%' in s in quote_string that caused it:
https://github.com/conda/menuinst/actions/runs/19366298809

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm trying to understand the impact of this. That would mean that echo %FOO%> output.txt would be quoted differently than echo %FOO% > output.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are a lot of more changes now but it works now based on the testing. I've done some additional testing locally with other workflows where menuinst is used.
There are a lot more changes now compared to before, but it's ready for another round of review.

Copy link
Contributor

@marcoesters marcoesters left a comment

Choose a reason for hiding this comment

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

This looks quite extensive. Just minor code suggestions and some remaining questions.

script = self._write_script()
if self.metadata["terminal"]:
command = ["cmd", "/D", "/K", f'"{script}"']
command = ["cmd", "/D", "/K", str(script)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Will removing the quotes be a problem for paths with spaces? I don't see a test covering that example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The quotes are actually not removed, it's just that we let WinLex.quote_args do the work instead of manually quoting it here, to have more of a single source of quoting. Note that we return return WinLex.quote_args(command) instead of return command in this PR.

Comment on lines +173 to +174
Return True if input refers to cmd.exe or %COMSPEC%. Here, 'refers' implies variants of
cmd, cmd.exe, <some path>/cmd and accounting for quoted input.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe call it _is_cmd_exe then? _is_cmd could also be interpreted as "is (a) command"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, fixed!

len(args) > 2
and ("CMD.EXE" in args[0].upper() or "%COMSPEC%" in args[0].upper())
and cls._is_cmd(args[0])
and (args[1].upper() == "/K" or args[1].upper() == "/C")
Copy link
Contributor

Choose a reason for hiding this comment

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

You are using quote_args now in a function that prepends the script with cmd.exe /D /K, so args[1] will be /D even though /K is added. Will this cause any issues? Does the check need to be more robust to parse all flags?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I made a more generic fix here 1acbcfc, I also added another test and parametrized it to verify it works for both /C and /K.

return s

# Don't add quotes for minus or leading space
if s and s[0] in ("-", " "):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if s and s[0] in ("-", " "):
if s[0] in ("-", " "):

You already return if s is an empty string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch 1acbcfc

return f'"{s}"'

if " " in s:
return '"%s"' % s
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return '"%s"' % s
return f'"{s}"'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same commit 1acbcfc

"""
if not name:
return name
if not name or name[0] == name[-1] == pad:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if not name or name[0] == name[-1] == pad:
if name[0] == name[-1] == pad:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same commit 1acbcfc



def test_quote_args_1():
"""Verify the input arguments are quoted."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""Verify the input arguments are quoted."""
"""Verify that input arguments are quoted."""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same commit 1acbcfc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed [bot] added once the contributor has signed the CLA

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

Filetype association in Windows registry requires double-quotes to properly handle files with spaces

3 participants