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

Alias in powershell error #1728

Open
pmacdonald-falcons opened this issue Apr 17, 2024 · 4 comments
Open

Alias in powershell error #1728

pmacdonald-falcons opened this issue Apr 17, 2024 · 4 comments
Labels
bug os:windows Windows-specific shell Shell related issues

Comments

@pmacdonald-falcons
Copy link

If I set an alias in my package.py as follows :

wrapper_path = "{root}/bin/launcher.bat"
app_dir = r"c:/Program Files/Shotgun"
app_path = os.path.join(app_dir, "Shotgun.exe")
alias('sg', '"{}" "{}" "{}"'.format(wrapper_path, 'sg', app_path))

The alias errors when entering the rez env for the package.

At C:\Users\pmacdonald\AppData\Local\Temp\rez_context_pr3jy1mp\context.ps1:46 char:90
+ ... donald\packages\sg_desktop\2024.4.1-tl.1/bin/launcher.bat" sg "c:/Pro ...
+                                                                ~~
Unexpected token 'sg' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Before you ask, this approach to creating an alias, which points to a wrapper launcher batch script, allows me to store the alias used to launch the application in an environment variable, which I can then later use to launch the same application via the same rez alias when jobs are executed on deadline. There may be better approaches to this, which may make this issue a moot point.

The batch script "launcher.bat" is as follows:

set REZ_USED_ALIAS=%1

for /f "tokens=1,* delims= " %%a in ("%*") do set ALL_BUT_FIRST=%%b

%ALL_BUT_FIRST%

It takes the first argument and stores it in REZ_USED_ALIAS, then executes the remaining arguments to launch the application.

Environment

  • OS Windows 10
  • Rez version Rez 3.0.0 from c:\rez\Lib\site-packages\rez (python 3.11)
  • Rez python version Rez 3.0.0 from c:\rez\Lib\site-packages\rez (python 3.11)

To Reproduce

  1. Create a package with the alias as defined in the description above.
  2. Build package. rez env -ci
  3. enter the env rez env sg_desktop

Expected behavior
I asked chatgpt for a working alias, and it came up with this, which works :

function sg {
    & ".\bin\launcher.bat" sg "c:\Program Files\Shotgun\Shotgun.exe" @args
}

This may be a breaking change for rez to implement as a new way of forming aliases under powershell, but it certainly appears to work for me if I create this alias manually in ps.

Actual behavior
Looking at the source of the error, rez appears to be defining the alias as :

function sg() { "c:\users\pmacdonald\packages\sg_desktop\2024.4.1-tl.1/bin/launcher.bat" sg "c:/Program Files/Shotgun\Shotgun.exe" @args }

This alias raises the error indicated in the description above.

@pmacdonald-falcons
Copy link
Author

pmacdonald-falcons commented Apr 17, 2024

I wonder if we can update this

def alias(self, key, value):

from

cmd = "function %s() { %s @args }" % (key, value)

to

cmd = "function %s { & %s @args }" % (key, value)

@pmacdonald-falcons
Copy link
Author

I can confirm that replacing the line above does indeed work.
I have no idea whether this may break existing alias definitions though!

@JeanChristopheMorinPerso
Copy link
Member

I think replacing %s() with %s (removing ()) is unnecessary since they both mean "the function doesn't take any parameter). See https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.4.

As for the &, in this case it's the call operator.

I would be curious to know if replacing sg with "sg" would fix the issue...

@triley13
Copy link

So actually this is a little outdated because we did, in fact, replace sg with "sg"'. I also tried with no quotes and I receive a similar error. We did everything in our org to sign our CLA #1747.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug os:windows Windows-specific shell Shell related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants