Windows 10 Environment Variables: why does variable replacement not work?

2

On Windows 10 Pro:

I just downloaded and installed (i.e. unpacked) the latest Maven version into a directory in preparation of an upcoming development project. Then - as instructed - I defined me two environment variables:

M2=%M2_HOME%\bin
M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-3.6.0

But for some strange reason the former %-placeholder 'M2' is not processed, i.e. "set" just yields:

>set
...
M2=%M2_HOME%\bin
M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-3.6.0
...

I have OK-ed and closed the env-var dialog and the system settings dialog. I restarted a new cmd-shell, I even killed and restarted Windows Explorer (i.e. the desktop), which should definitely trigger a re-read and re-substitution of those variables, and then restarted cmd from that desktop again but M2 remains at the unsubstituted %M2_HOME%\bin-value.

I also checked in the registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\M2 is defined as REG_EXPAND_SZ, so it should be evaluated and %-vars should be substituted, but obviously here they are not.

If I assign the very same value manually on the command line it is of course properly substituted as one would expect, but not by default via the env-var definition shown above.

So, what am I missing here? Why is this M2_HOME as part of the M2-value not properly substituted?

Edit: added this output on JosefZ's request:

C:\Users\mmo>reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -v M2*

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    M2    REG_EXPAND_SZ    %M2_HOME%\bin
    M2_HOME    REG_EXPAND_SZ    %ProgramFiles%\Apache Software Foundation\apache-maven-3.6.0

End of search: 2 match(es) found.

C:\Users\mmo>reg query HKCU\Environment -v M2*

End of search: 0 match(es) found.

C:\Users\mmo>

mmo

Posted 2019-02-05T17:45:10.280

Reputation: 139

2What about simply reordering the vars, when M2_HOME isn't set how could it be substituted in the first command? – LotPings – 2019-02-05T18:53:34.880

Please show us output from reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -v M2* as well as from reg query HKCU\Environment -v M2* (copy&paste from a cmd window.) – JosefZ – 2019-02-05T19:42:51.787

@JosefZ: I added the result to my initial question above as the formatting gets garbled in these comments. – mmo – 2019-02-06T06:51:39.817

2@LotPings: not possible - they are automatically ordered alphabetically by the env-var dialog. And the order DOES seem to be an issue! I defined me a few more examples that refer to other env-vars further down (TestX=%TestY%\test3, TestY=%TestZ%\test2, TestZ=%JAVA_HOME%\test1) and those are NOT correctly resolved, to! If I revert the order things work. So - it's indeed the order! IMHO that's a bug but I doubt I can convince MS on that... – mmo – 2019-02-06T07:05:40.257

1@LotPings: Could you please make that into an answer? Then I could give you credits. One apparently can't mark a comment as accepted answer. – mmo – 2019-02-06T07:23:59.537

@mmo: Elsewhere it's called "eager evaluation" or "eager substitution" and it's probably what the programmers were used to from batch scripts. Yes, lazy/delayed expansion would be more suitable here due to it being impossible to manually reorder the envvars, but I'm not entirely sure it counts as a bug. Probably an enhancement request... – user1686 – 2019-02-06T10:01:28.923

@LotPings: Registry keys or values have no explicit order in general, and Windows doesn't implement anything custom for HKCU\Environment specifically either. Either the program has to sort them alphabetically or accept unpredictable order. – user1686 – 2019-02-06T10:02:50.760

@grawity: I would expect, that the variable substitution checks whether the value of a referenced variable contains another %...%-var. And if so, then it should first evaluate/substitute that one, etc. (i.e. recursively - of course without going into possible endless loops - but that's a different story). – mmo – 2019-02-06T16:08:17.247

@grawity: I didn't understand your comment "Either the program has to sort them ...". What program do you mean here? The application? The command shell? I'ld plead that neither should have to worry about env-var's value substitution, let alone the order to be applied for that! That should be part of the "system" initialization (I guess, the Windows Explorer's initialization or the user session's initialization or whatever wraps the user's desktop and shells and provides these environment variables). – mmo – 2019-02-06T16:12:40.897

No answers