title: Tcl package require loads the wrong package version?
body: <p>I'm trying to deploy a beta version of a package and want existing code to run the stable release.</p>
  
  <p>The built-in package versioning in Tcl 8.5/8.6 should work for that purpose. But I ran into the following very odd behavior, where the order in which pkgIndex.tcl files are loaded will affect which package is eventually loaded. I can get an unstable package to load even when "package prefer" is "stable".</p>
  
  <p>I have tried this in multiple Tcl releases, 8.5 as well as 8.6, and in different Tcl enabled tools. All behave similarly.</p>
  
  <p>This following code works as I expect:</p>
  
  <pre><code>% package prefer
  stable
  
  % package ifneeded p 1.1 {package provide p 1.1}
  % package ifneeded p 1.2 {package provide p 1.2}
  % package ifneeded p 1.3 {package provide p 1.3}
  % package ifneeded p 1.4a1 {package provide p 1.4a1}
  % package versions p
  1.1 1.2 1.3 1.4a1
  % package require p
  1.3
  </code></pre>
  
  <p>But when the "package ifneeded" statements are run in a different order, the unstable version is loaded:</p>
  
  <pre><code>% package forget p
  % package prefer
  stable
  
  % package ifneeded p 1.4a1 {package provide p 1.4a1}
  % package ifneeded p 1.3 {package provide p 1.3}
  % package ifneeded p 1.2 {package provide p 1.2}
  % package ifneeded p 1.1 {package provide p 1.1}
  % package versions p
  1.4a1 1.3 1.2 1.1
  % package require p
  1.4a1
  </code></pre>
  
  <p>What is the explanation for this behavior? Can I prevent it, or do I simply have to be very careful when I append library paths to auto_path?</p>