Skip to content

Fix Path,EMSDK and EM_CACHE not being set permanently when using --global#515

Merged
sbc100 merged 2 commits into
emscripten-core:masterfrom
kudaba:fix_win_env_global
Oct 9, 2020
Merged

Fix Path,EMSDK and EM_CACHE not being set permanently when using --global#515
sbc100 merged 2 commits into
emscripten-core:masterfrom
kudaba:fix_win_env_global

Conversation

@kudaba
Copy link
Copy Markdown
Contributor

@kudaba kudaba commented Jun 4, 2020

Going through the initial setup of emscripten the first time, the first thing I wanted to do was set it up globally to use in visual studio and found that some of the environment variables are not getting applied properly. There are still additional issues with environment variables however since EMSDK_NODE and EMSDK_PYTHON and PATH will get installed system wide while EM_CACHE and EMSDK will only get installed to user.

FYI: I also ran into some reported issues during my initial setup that didn't block me in the end:

  • pywin32 was missing, so I needed to pip install it, maybe add a not in the windows setup documentation.
  • ERROR: The system was unable to find the specified registry key or value. which are not actual issues but should probably be suppressed (trying to delete registry key that doesn't exist). Happens on every activate call.
  • (1159, 'PostMessage', 'The message can be used only with synchronous operations.') Only happens when system registry keys are not set, it doesn't cause them to fail though.

Edit:
So, a big update here. While the code remains to be able to set user environment variables in windows it really only runs in two modes: current session locals or system wide environment variables. I removed the code that was trying to delete the variables and causing errors since it doesn't really do anything.

I removed setx and the option to permanently activate from construct_env, but it's still used as a fallback to set registry variables where it already had the 1024 check in place.

I fixed the PostMessage error that was happening by using SendMessageWithTimeout instead. It only sends the message if a registry value actually changed. I also confirmed that the message is being received by other applications.

The one caveat that still exists is that you need to run emsdk activate before running emsdk activate --global so that it will use the correct python version.

@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Jun 4, 2020

Regarding pywin32. This should now be included in the version of python that is downloaded by emsdk. At least as of #487.. can you confirm?

@kudaba
Copy link
Copy Markdown
Contributor Author

kudaba commented Jun 4, 2020

Python is mostly working as designed. The confusion happened because I went through the setup in a non-administrator console window, then when --global failed I switched to administrator to rerun only emsdk activate --global but since it was a new console window the environment variables were gone and it was trying to use my local python instead of the built-in one. I didn't realize (since it was my first time) that you should run emsdk_env before doing anything.

@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Jun 14, 2020

Seems like a good change to me. @juj?

Comment thread emsdk.py Outdated
@kudaba kudaba force-pushed the fix_win_env_global branch from 307c527 to 8d4f4d0 Compare June 17, 2020 19:19
@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Jun 17, 2020

This change is a lot bigger now :( Can it be simplified or broken down into smaller chunks? I'm having trouble groking all the moving parts there.

Comment thread emsdk.py Outdated
@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Jun 17, 2020

If there are part of this change that are cleanups or re-structuring only perhaps you could submit those separately?

Also, perhaps the PR description could be update to reflect exactly what this change is supposed to do? Or is it up-to-date already?

@kudaba
Copy link
Copy Markdown
Contributor Author

kudaba commented Jun 17, 2020

Updated the PR with the info of all the changes. I could go through an restructure it, unfortunately I won't have the time to get back to this for a while.

@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Jun 23, 2020

How does this relate to #468 ?

@kudaba
Copy link
Copy Markdown
Contributor Author

kudaba commented Jun 23, 2020

There will be merge conflicts but they should be able to co-exists.

@aminya
Copy link
Copy Markdown
Contributor

aminya commented Sep 30, 2020

Can we get this rebased? This will probably fix the windows tests.

FYI, --global is renamed to --system. Now, we have a separate --permanent flag.

@kudaba kudaba force-pushed the fix_win_env_global branch 4 times, most recently from 997806d to e8b9a36 Compare September 30, 2020 17:14
Comment thread emsdk.py Outdated
assert False

errlog(key + ' = ' + value)
errlog('')
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this added an extra empty line to the output?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah, just to make formatting a bit nicer

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

But why do you say this is nicer? Right no I see the following output on my machine:

sbc.sbc-glaptop (~/dev/wasm/emsdk) ((53c184e...)) $ . ./emsdk_env.sh 
Adding directories to PATH:
PATH += /home/sbc/dev/wasm/emsdk
PATH += /home/sbc/dev/wasm/emsdk/upstream/emscripten
PATH += /home/sbc/dev/wasm/emsdk/node/12.18.1_64bit/bin

Setting environment variables:
EMSDK = /home/sbc/dev/wasm/emsdk
EM_CONFIG = /home/sbc/dev/wasm/emsdk/.emscripten
EM_CACHE = /home/sbc/dev/wasm/emsdk/upstream/emscripten/cache
EMSDK_NODE = /home/sbc/dev/wasm/emsdk/node/12.18.1_64bit/bin/node
sbc.sbc-glaptop (~/dev/wasm/emsdk) ((53c184e...)) $

To me an extra empty line at the end would look pointless/messy.. but maybe that is just me?

Comment thread emsdk.py
Comment thread emsdk.py Outdated
Comment thread emsdk.py Outdated
Comment thread emsdk.py Outdated
Comment thread emsdk.py Outdated
Copy link
Copy Markdown
Contributor

@aminya aminya left a comment

Choose a reason for hiding this comment

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

I tested this. It works!

I recommend enabling the tests:
https://github.com/kudaba/emsdk/blob/ca1e15fb6a66c4d62e2a72a27cbff371b4e2a09f/.circleci/config.yml#L95

          command: |
-            try {
               scripts/test_permanent.ps1
-            } catch {
-             Write-Host "`n Error Message: [$($_.Exception.Message)"] -ForegroundColor Red
-              Write-Host "`n Line Number: "$_.InvocationInfo.ScriptLineNumber -ForegroundColor Red
-            }

@aminya aminya mentioned this pull request Oct 7, 2020
@aminya
Copy link
Copy Markdown
Contributor

aminya commented Oct 7, 2020

I am happy to say that I tested this in combination with #635, and all the scripts/test_permanent.ps1 tests finally pass!

We should definitely enable them as I showed in #515 (review)

After #635 is merged there is a small conflict that needs to be fixed. I removed the following lines in that PR, so after accepting your changes here, just remove these lines:

prev_path = os.environ['PATH']

@kudaba kudaba force-pushed the fix_win_env_global branch 7 times, most recently from fabf720 to 082581c Compare October 7, 2020 21:07
…or --permanent

Switch PostMessage with SendMessageWithTimeout and only send one if environment variables actually changed
Enable and fix permanent unit tests
@kudaba kudaba force-pushed the fix_win_env_global branch from 082581c to 08a4405 Compare October 7, 2020 22:16
@kudaba
Copy link
Copy Markdown
Contributor Author

kudaba commented Oct 7, 2020

Sorry for all the force pushes, it took me a while to get local testing working.

I fixed up the exception handling with the registry and had to fix up the permanent test script a bit as well since it assumed emsdk was being installed to a folder called emsdk.

I did notice that the registry handling is a lot slower now, but since it's generally a one time thing it's probably not anything worth worrying about.

@aminya
Copy link
Copy Markdown
Contributor

aminya commented Oct 7, 2020

Awesome!

I did not notice any performance difference. Anyways, I think stability is more important. 😃

@aminya
Copy link
Copy Markdown
Contributor

aminya commented Oct 8, 2020

@sbc100 Can we roll this in?

Copy link
Copy Markdown
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

I think we still have the problem that we will clobber any local changes that the user has in their PATH with a fresh copy from the registry... but that is separate issue I think.

Comment thread emsdk.py
Comment thread emsdk.py
'Environment', # lParam: Specifically environment variables changed
SMTO_BLOCK, # fuFlags: Wait for message to be sent or timeout
100) # uTimeout: 100ms
except Exception as e:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we be more specific?

Copy link
Copy Markdown
Contributor

@aminya aminya Oct 9, 2020

Choose a reason for hiding this comment

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

@kudaba has added comments for these. The original code did not have any description
https://github.com/emscripten-core/emsdk/pull/515/files#diff-7ecf1f48f2952ff372972476842c8882L323

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I mean more specific in the type of exceptions we catch here? Can it be just OSError perhaps?

Copy link
Copy Markdown
Contributor

@aminya aminya Oct 9, 2020

Choose a reason for hiding this comment

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

I don't think it hurts to catch all the exceptions. We are not concerned with the performance of this program which should run on a powerful desktop processor. The stability is more important, IMO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure how error handling or exceptions are working with the interop between python and win32 so this is just me being a bit extra cautious.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Catching specific exceptions is not about performance but about code quality and maintainability. In general we want to minimize the size of the try block and be as specific as possible in the exceptions we catch.

From pep8: When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause:

One problem with catching everything is that it can hide all kinds of programmer errors. e.g. simple things like accessing out of bounds which the except block is almost never supposed to handle.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Anyway, unrelated to this PR so we don't need to block on this.

Comment thread emsdk.py Outdated
assert False

errlog(key + ' = ' + value)
errlog('')
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

But why do you say this is nicer? Right no I see the following output on my machine:

sbc.sbc-glaptop (~/dev/wasm/emsdk) ((53c184e...)) $ . ./emsdk_env.sh 
Adding directories to PATH:
PATH += /home/sbc/dev/wasm/emsdk
PATH += /home/sbc/dev/wasm/emsdk/upstream/emscripten
PATH += /home/sbc/dev/wasm/emsdk/node/12.18.1_64bit/bin

Setting environment variables:
EMSDK = /home/sbc/dev/wasm/emsdk
EM_CONFIG = /home/sbc/dev/wasm/emsdk/.emscripten
EM_CACHE = /home/sbc/dev/wasm/emsdk/upstream/emscripten/cache
EMSDK_NODE = /home/sbc/dev/wasm/emsdk/node/12.18.1_64bit/bin/node
sbc.sbc-glaptop (~/dev/wasm/emsdk) ((53c184e...)) $

To me an extra empty line at the end would look pointless/messy.. but maybe that is just me?

Comment thread emsdk.py
* Fixed up comment
* Removed redundant newline
* Removed unneeded try/catch block in registry get variable
@kudaba
Copy link
Copy Markdown
Contributor Author

kudaba commented Oct 9, 2020

I think we still have the problem that we will clobber any local changes that the user has in their PATH with a fresh copy from the registry... but that is separate issue I think.

Actually I think this will be fine. It adds emsdk paths to the local path environment variable and the registry setting separately so it should be additive in both cases. I had to do that since just setting the registry setting was not actually changing the currently set environment variable.

@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Oct 9, 2020

I think we still have the problem that we will clobber any local changes that the user has in their PATH with a fresh copy from the registry... but that is separate issue I think.

Actually I think this will be fine. It adds emsdk paths to the local path environment variable and the registry setting separately so it should be additive in both cases. I had to do that since just setting the registry setting was not actually changing the currently set environment variable.

The problem is not about addings to the current env. I believe the problem is that we always take the registry value as the initial PATH to add to (we've add the concept of fallback here but I think in practice it won't fire). This means that you always end up with "registry value + extras" rather than "current shell value + extras".

Basically we need to calculate two different paths which can be different:

  1. What path should be added the registery.
  2. What path should be set in the current shell

Again, its not something we need to fix as part of this PR, I guess its bug we have always had.

@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Oct 9, 2020

I think this change is ready to land now.

Can you craft as suitable message for the squashed commit? I would use the PR description but I'm not sure which parts are still relevant?

@kudaba
Copy link
Copy Markdown
Contributor Author

kudaba commented Oct 9, 2020

The first commit has the most accurate messaging I think:

Fix environment variables to be consistently set when using --system or --permanent
Switch PostMessage with SendMessageWithTimeout and only send one if environment variables actually changed
Enable and fix permanent unit tests

Or did you mean you want me to squash the review feeback commit back down into the original (I had been doing it this whole time until the last bit of feedback).

@sbc100 sbc100 merged commit 4fe6e49 into emscripten-core:master Oct 9, 2020
@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Oct 9, 2020

Thanks!

vargaz pushed a commit to vargaz/emsdk that referenced this pull request Nov 22, 2023
… 20230922.1 (emscripten-core#515)

runtime.linux-arm64.Microsoft.NETCore.Runtime.JIT.Tools , runtime.linux-arm64.Microsoft.NETCore.Runtime.Mono.LLVM.Sdk , runtime.linux-arm64.Microsoft.NETCore.Runtime.Mono.LLVM.Tools , runtime.linux-arm64.Microsoft.NETCore.Runtime.ObjWriter , runtime.linux-arm64.Microsoft.NETCore.Runtime.Wasm.LLVM.Transport , runtime.linux-musl-arm64.Microsoft.NETCore.Runtime.JIT.Tools , runtime.linux-musl-arm64.Microsoft.NETCore.Runtime.Mono.LLVM.Sdk , runtime.linux-musl-arm64.Microsoft.NETCore.Runtime.Mono.LLVM.Tools , runtime.linux-musl-arm64.Microsoft.NETCore.Runtime.ObjWriter , runtime.linux-musl-arm64.Microsoft.NETCore.Runtime.Wasm.LLVM.Transport , runtime.linux-musl-x64.Microsoft.NETCore.Runtime.JIT.Tools , runtime.linux-musl-x64.Microsoft.NETCore.Runtime.Mono.LLVM.Sdk , runtime.linux-musl-x64.Microsoft.NETCore.Runtime.Mono.LLVM.Tools , runtime.linux-musl-x64.Microsoft.NETCore.Runtime.ObjWriter , runtime.linux-musl-x64.Microsoft.NETCore.Runtime.Wasm.LLVM.Transport , runtime.linux-x64.Microsoft.NETCore.Runtime.JIT.Tools , runtime.linux-x64.Microsoft.NETCore.Runtime.Mono.LLVM.Sdk , runtime.linux-x64.Microsoft.NETCore.Runtime.Mono.LLVM.Tools , runtime.linux-x64.Microsoft.NETCore.Runtime.ObjWriter , runtime.linux-x64.Microsoft.NETCore.Runtime.Wasm.LLVM.Transport , runtime.osx-arm64.Microsoft.NETCore.Runtime.JIT.Tools , runtime.osx-arm64.Microsoft.NETCore.Runtime.Mono.LLVM.Sdk , runtime.osx-arm64.Microsoft.NETCore.Runtime.Mono.LLVM.Tools , runtime.osx-arm64.Microsoft.NETCore.Runtime.ObjWriter , runtime.osx-arm64.Microsoft.NETCore.Runtime.Wasm.LLVM.Transport , runtime.osx-x64.Microsoft.NETCore.Runtime.JIT.Tools , runtime.osx-x64.Microsoft.NETCore.Runtime.Mono.LLVM.Sdk , runtime.osx-x64.Microsoft.NETCore.Runtime.Mono.LLVM.Tools , runtime.osx-x64.Microsoft.NETCore.Runtime.ObjWriter , runtime.osx-x64.Microsoft.NETCore.Runtime.Wasm.LLVM.Transport , runtime.win-arm64.Microsoft.NETCore.Runtime.JIT.Tools , runtime.win-arm64.Microsoft.NETCore.Runtime.ObjWriter , runtime.win-arm64.Microsoft.NETCore.Runtime.Wasm.LLVM.Transport , runtime.win-x64.Microsoft.NETCore.Runtime.JIT.Tools , runtime.win-x64.Microsoft.NETCore.Runtime.Mono.LLVM.Sdk , runtime.win-x64.Microsoft.NETCore.Runtime.Mono.LLVM.Tools , runtime.win-x64.Microsoft.NETCore.Runtime.ObjWriter , runtime.win-x64.Microsoft.NETCore.Runtime.Wasm.LLVM.Transport
 From Version 16.0.5-alpha.1.23452.1 -> To Version 16.0.5-alpha.1.23472.1

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants