AcademySoftwareFoundation / rez

An integrated package configuration, build and deployment system for software
https://rez.readthedocs.io
Apache License 2.0
921 stars 329 forks source link

Alias in powershell error #1728

Open pmacdonald-falcons opened 3 months ago

pmacdonald-falcons commented 3 months ago

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

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 commented 3 months ago

I wonder if we can update this

https://github.com/AcademySoftwareFoundation/rez/blob/f17e88dad283826a5914e3aade4fbc9a010cdba2/src/rezplugins/shell/_utils/powershell_base.py#L295

from

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

to

cmd = "function %s { & %s @args }" % (key, value)
pmacdonald-falcons commented 3 months ago

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

JeanChristopheMorinPerso commented 2 months ago

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 commented 2 months ago

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 https://github.com/AcademySoftwareFoundation/rez/pull/1747.