Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2012-01-28
01:46 • Closed ticket [3480599fff]: pkg_mkIndex should generate deterministic pkgIndex.t... artifact: 3dbebfbe0b user: dgp
01:41 • Ticket [3480599fff]: 4 changes artifact: e685d7a188 user: bovineone
01:40 • Ticket [3480599fff]: 4 changes artifact: e5024d1183 user: bovineone
01:39 • Ticket [3480599fff]: 4 changes artifact: 8670e32b71 user: dgp
01:26 • Ticket [3480599fff]: 4 changes artifact: d5d575ff4e user: dgp
01:02 • Ticket [3480599fff]: 4 changes artifact: 1f17230645 user: bovineone
00:47 • Ticket [3480599fff]: 4 changes artifact: bee5189fbb user: sebres
00:35 • Ticket [3480599fff]: 4 changes artifact: c54f49a28e user: dgp
2012-01-27
17:03 • New ticket [3480599fff]. artifact: 6416a8ba78 user: bovineone

pkg_mkIndex should generate deterministic pkgIndex.tcl order

Closed / Accepted3480599fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2012-01-27 17:03:00
pkg_mkIndex currently generates the pkgIndex.tcl containing multiple "list source" commands with each file as they were encountered in the filesystem directory, which will usually vary from one computer to the next.

If multiple users are doing development on different directories/computers and committing work to a shared repository, the contents of the pkgIndex.tcl file will almost always be different and cause unnecessary file differences in pkgIndex.tcl.

Besides the inconvenience of revision control detecting unnecessary file differences, the programmatic behavior could theoretically be different if the listed order of files is unpredictable and there are unintentional interactions between the files.  This might lead to difficulties in diagnosing due to the unpredictable file loading order.

pkg_mkIndex should be changed to generate pkgIndex.tcl files that have a deterministic ordering that will be the same on different computers.  This could be done by simply sorting the list of filenames before writing them to the pkgIndex.tcl, if no other criteria is better suited for this.
dgpplain2012-01-28 00:35:04
My best advice is "Don't Do That".

If your package is contained in multiple
files for some reason, it's far better to make
only one of them the master file to be [source]d to
provide the package.  To bring the contents of
the other files in, you can do something as simple
as a [source] of them from the master file.  Or
you can set up in the master file for the auto-loading
of the other files as their contents are required.
This is best done with your own loading-stubs of
those commands, but you can also build on Tcl's
tclIndex scheme if you prefer.  An example is the
package "control" in tcllib.

If you really do have a package made up of parts with
order dependence, you really have no choice but to
take one of those steps.  Even if pkg_mkIndex produced
a deterministic order, there's no reason to believe it will
pick the order needed by your code.

If you have a package made up of parts that depend
less on an explicit order, but on interdependency constraints,
it may be that you actually have code that would be better
represented as multiple packages, instead of one, using
[package] to manage the dependency constraints.  In that situation,
my preference is to install each package into its own directory with
its own pkgIndex.tcl file.

Once you've reduced the provide script of your package
to the [source] of a single file, the contents of your
pkgIndex.tcl likely are near as simple as:

package ifneeded foo 1.0 [list source [file join $dir master.tcl]]

At that point, my advice is not to use [pkg_mkIndex] at all.
You can either hand-edit, or generate via much simpler tools.
[pkg_mkIndex] is simply too ambitious and complex, and tries
to take on problems that are simply unsolvable.  It's best abandoned.

Another issue you might consider is that if your build is set up
so that pkgIndex.tcl is a generated file, it arguably should not
be checked into your version control system.
sebresplain2012-01-28 00:47:58
pkg_mkIndex in 8.5 currently generate "pkgIndex.tcl" already sorted by :
... lsort [array names files] ...

perhaps, did you mean "auto_mkindex" to generate "tclIndex"?
This routine does not do (sort) this at the moment.

I have another solution, that in addition to sorting, accelerates auto loading and load the memory gently, example:

-----------------------------
ORG:
-----------------------------
....
set auto_index(::safe::DeleteHookName) [list source [file join $dir safe.tcl]]
set auto_index(::safe::TranslatePath) [list source [file join $dir safe.tcl]]
set auto_index(::safe::Log) [list source [file join $dir safe.tcl]]
set auto_index(::safe::CheckFileName) [list source [file join $dir safe.tcl]]
set auto_index(::safe::AliasSource) [list source [file join $dir safe.tcl]]
set auto_index(::safe::AliasLoad) [list source [file join $dir safe.tcl]]
set auto_index(::safe::FileInAccessPath) [list source [file join $dir safe.tcl]]
set auto_index(::safe::Subset) [list source [file join $dir safe.tcl]]
set auto_index(::safe::AliasSubset) [list source [file join $dir safe.tcl]]
set auto_index(::safe::AliasEncoding) [list source [file join $dir safe.tcl]]
....
set auto_index(::tcl::tm::add) [list source [file join $dir tm.tcl]]
set auto_index(::tcl::tm::remove) [list source [file join $dir tm.tcl]]
set auto_index(::tcl::tm::list) [list source [file join $dir tm.tcl]]
set auto_index(::tcl::tm::UnknownHandler) [list source [file join $dir tm.tcl]]
set auto_index(::tcl::tm::roots) [list source [file join $dir tm.tcl]]
set auto_index(::tcl::tm::path) [list source [file join $dir tm.tcl]]
....
set auto_index(::itclTestClass) [list source [file join $dir testing.itcl]]
....
set auto_index(::itclTestClass::proc1) [list source [file join $dir testing.itcl]]
set auto_index(::itclTestClass::proc2) [list source [file join $dir testing.itcl]]
set auto_index(::itclTestClass::proc3) [list source [file join $dir testing.itcl]]
....
-----------------------------
NEW:
-----------------------------
set auto_index(::safe::*) [list source [file join $dir safe.tcl]]
....
set auto_index(::tcl::tm::*) [list source [file join $dir tm.tcl]]
....
set auto_index(::itclTestClass) [list source [file join $dir testing.itcl]]
set auto_index(::itclTestClass::*) [list source [file join $dir testing.itcl]]
-----------------------------

If someone is interested, I can upload a patch for this.
bovineoneplain2012-01-28 01:02:13
I am referring to running pkg_mkIndex in this manner with tcl 8.5.10, which does not seem to generate a tclIndex.tcl file that is sorted in any deterministic way:

echo "pkg_mkIndex -- ." | $(TCLSH)

I agree that you should not intentionally introduce ordering dependencies within files in a single package, but it sometimes happens unintentionally and it would be nice to have predictable behavior.  (ie: redefining a proc or a variable in two files and getting different definitions depending on which file is loaded first by different users.)  Having deterministic behavior is generally a desirable feature of software, and arguing to support the opposite is highly irregular and unparliamentary.
dgpplain2012-01-28 01:26:36
I'm not really arguing to support arbitrary over deterministic.

I'm arguing that [pkg_mkIndex] is a tool better abandoned
than repaired.

That said, this seems like a simple enough change.  Looking
into generation of a quick patch... unless you already have one?
dgpplain2012-01-28 01:39:54
Patch for review and testing:

http://core.tcl.tk/tcl/info/1da688ab14
bovineoneplain2012-01-28 01:40:13
Here is a patch I just made:

diff --git a/library/package.tcl b/library/package.tcl
index 0a1e7bd..c61052d 100644
--- a/library/package.tcl
+++ b/library/package.tcl
@@ -401,7 +401,7 @@ proc pkg_mkIndex {args} {
     break
 }
 lappend cmd ::tcl::Pkg::Create -name $name -version $version
-foreach spec $files($pkg) {
+foreach spec [lsort -index 0 $files($pkg)] {
     foreach {file type procs} $spec {
 if {$direct} {
     set procs {}
bovineoneplain2012-01-28 01:41:51
Your patch looks identical to mine :)  That indeed corrects the issue in my testing.
dgpplain2012-01-28 01:46:51
allow_comments - 1

Applied for 8.5.12 and 8.6b3.
Log in (anonymous is fine) to add a comment.