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

(PUP-11348) Use ENV directly on Windows #9052

Merged
merged 6 commits into from
Apr 18, 2023

Conversation

joshcooper
Copy link
Contributor

@joshcooper joshcooper commented Apr 13, 2023

Ruby 3 no longer uses the current code page when getting and setting environment variables. Instead it always calls wide functions like GetEnvironmentVariableW and transcodes the value from UTF-16LE to UTF-8. And it does the reverse when setting environment variables. As a result, we don't need our Windows specific code for accessing environment variables.

  • Deprecates Puppet::Util.{get_env,get_environment,clear_environment,set_env,merge_environment}
  • Call ENV related methods directly
  • Add more boundary cases for valid env keys and values

Tested Windows (x86, x64 and Japanese) in https://jenkins-platform.delivery.puppetlabs.net/view/puppet-agent/view/ad-hoc/job/platform_puppet-agent-extra_puppet-agent-integration-suite_adhoc-ad_hoc/1103/

@joshcooper joshcooper force-pushed the remove_env_monkeypatches branch 4 times, most recently from 9fed43d to bb14c80 Compare April 13, 2023 23:59

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Move tests for unicode key & value and nil values from run_mode_spec.rb to
util_spec.rb.

We're already testing that env variables passed to `withenv` are not set when
the method returns.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
The test was confined to only run on Ruby < 3.0, but the minimum required
version is 3.1, so delete the test.

Ruby 3+ correctly uses wide Win32 APIs to access environment variables and
transform them between UTF-16LE and UTF-8 regardless of the current code page.
So it is possible to get/set variables whose unicode code points don't map to
the current code page and that is already tested in util_spec.rb.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Don't call deprecated environment methods in Puppet::Util

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Always resetting the environment is faster the compare-then-set in cases where
the environment was changed and not.

```
$ cat bench.rb
require 'benchmark/ips'

Benchmark.ips do |x|
  unchanged = ENV.to_hash
  changed   = ENV.to_hash.merge!("foo" => "bar")

  x.report("test-set unchanged") { ENV.replace(unchanged) if ENV.to_hash != unchanged }
  x.report("test-set changed")   { ENV.replace(unchanged) if ENV.to_hash != changed }
  x.report("set unchanged")      { ENV.replace(unchanged) }
  x.report("set changed")        { ENV.replace(changed) }

  x.compare!
end

$ ruby --version
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
$ ruby bench.rb
      Warming up --------------------------------------
    test-set unchanged     4.711k i/100ms
      test-set changed     2.691k i/100ms
        set unchanged     5.639k i/100ms
          set changed     5.480k i/100ms
  Calculating -------------------------------------
    test-set unchanged     46.809k (± 1.1%) i/s -    235.550k in   5.032833s
      test-set changed     26.749k (± 0.6%) i/s -    134.550k in   5.030353s
        set unchanged     55.451k (± 0.9%) i/s -    281.950k in   5.085048s
          set changed     54.355k (± 0.8%) i/s -    274.000k in   5.041297s

  Comparison:
        set unchanged:    55451.4 i/s
          set changed:    54354.6 i/s - 1.02x  slower
    test-set unchanged:    46808.7 i/s - 1.18x  slower
      test-set changed:    26748.7 i/s - 2.07x  slower
```
@joshcooper joshcooper force-pushed the remove_env_monkeypatches branch from bb14c80 to c4428d9 Compare April 14, 2023 17:14
@joshcooper joshcooper changed the title (PUP-11348) Remove ENV monkeypatches (PUP-11348) Use ENV directly on Windows Apr 14, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Ruby 3 no longer relies on the current code page when reading/writing env
vars[1]. Instead it uses wide Win32 APIs and transcodes them to UTF-8.

Ruby's implementation also excludes env var that start with `=`

ruby/ruby@ca76337

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@joshcooper joshcooper force-pushed the remove_env_monkeypatches branch from c4428d9 to 6fcaef2 Compare April 14, 2023 17:41
@joshcooper joshcooper marked this pull request as ready for review April 14, 2023 21:20
@joshcooper joshcooper requested a review from a team as a code owner April 14, 2023 21:20
@tvpartytonight tvpartytonight merged commit 3f37b69 into puppetlabs:main Apr 18, 2023
@joshcooper joshcooper deleted the remove_env_monkeypatches branch April 18, 2023 02:51
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.

None yet

2 participants