AcademySoftwareFoundation / rez

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

Aliases on windows (cmd) persist outside of the rez environment #1411

Open hughetop opened 1 year ago

hughetop commented 1 year ago

While testing on windows I noticed that the "doskey" macros created with the alias command persist outside of the resulting rez environment. So if you have a package.py file like:

def commands():
    alias('blender', r'"C:\Program Files\Blender Foundation\Blender 3.1\blender.exe"')

Then after doing rez-env blender I can run blender and it will launch Blender. Awesome! The problem is that after exiting the rez environment, the 'blender' alias still exists. This was causing some weird problems when testing with multiple versions of software and multiple softwares. Ideally when the rez environment is exited, it should "unalias" any aliases. On windows, this would be doing doskey blender= to remove the macro.

Environment

To Reproduce

  1. create a package on windows with an alias in commands
  2. go into that rez environment using cmd
  3. exit
  4. run that alias

Expected behavior The command should not exist since the rez environment was exited.

Actual behavior It runs the alias, which can error due to the environment being invalid.

hughetop commented 1 year ago

Another interesting side effect about the alias command with cmd (doskey) is that they aren't there in a ResolvedContext. Ex: if the blender package.py uses the alias function in commands:

rez-env rez python-3.9
> python
>>> from rez.resolved_context import ResolvedContext
>>> context = ResolvedContext(['blender'])
>>> context.which('blender')
>>> # None

However, if the package.py commands function is this instead:

def commands():
    env.PATH.prepend(r'C:\Program Files\Blender Foundation\Blender 3.1')

then context.which('blender') works correctly. So I've been adding directories to $PATH instead of using the alias function.

Edit: this workaround works for now, but I prefer the explicit "this command runs this exact executable" of the alias function, rather than "look for something in the %PATH% and run it, yolo" of adding to the PATH.

hughetop commented 1 year ago

Forgot to update this, I have since switched over to using .bat files in a package /bin directory for custom executables. For software like python or Blender I still prepend it's directory to $PATH.

johhnry commented 1 year ago

Hi @hughetop,

I found that to remove a macro with doskey, you can do the following:

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/doskey#code-try-11

A solution could to have a hook on the shell process exit and then unregister the aliases we defined earlier?

hughetop commented 1 year ago

Yeah, I noticed how to delete the macro entry, but I don't know if that' something rez would handle or if all our packages would do that. I'm not using aliases with cmd anymore though, so this hasn't been a priority for me.

johhnry commented 1 year ago

yes I don't know either, I needed to run a Python script in the bin directory so I went with a cmd script that calls python.exe ... and added that to the path