ksh: namespaces don't work properly when defined within functions

From https://unix.stackexchange.com/questions/668176/problem-with-ksh-namespaces-defined-inside-functions

$ function f { a=0; namespace x { a=1; }; typeset -p .x a; }; f
namespace x
{
        :
}
a=1

Expected:

$ function f { a=0; namespace x { a=1; }; typeset -p .x a; }; f
namespace x
{
        a=1
}
a=0

See also:

$ namespace x { a=1; }; .x.b=2; typeset -p .x b
namespace x
{
        a=1
}
b=2

Expected:

$ namespace x { a=1; }; .x.b=2; typeset -p .x b
namespace x
{
        a=1
        b=2
}

I see namespaces were apparently removed in ksh2020, maybe they were deemed non-functional?

About this issue

Commits related to this issue

Most upvoted comments

I’m working on the new approach now. Removing the old stuff took some doing – you’d think everything is between SHOPT_NAMESPACE tags, but no, of course that code had little tentacles in various other places. But that’s done now, and I’ve started working on the new code and I’m making surprisingly quick progress. It’s not quite half working yet, but getting there. It’ll be much smaller, simpler and cleaner than the old way. And it’ll support nesting, because with the new approach, that’s trivial.

There will be no hooks into typeset at all. ksh already has the necessary functionality and we don’t need to duplicate it or make it fancier. To print out a namespace ‘foo’ you’ll be able to do typeset -p "${!.foo.@}" to get the names starting with .foo..

With what I have now, variable expansions work, assignments don’t yet. But this already works:

$ arch/*/bin/ksh -xc 'namespace sh { echo $version; }'
arch/darwin.i386-64/bin/ksh: warning: [DEBUG] entering namespace .sh
+ echo Version AJM 93u+m/1.1.0-alpha+dev 2021-11-24
Version AJM 93u+m/1.1.0-alpha+dev 2021-11-24
arch/darwin.i386-64/bin/ksh: warning: [DEBUG] exiting namespace .sh

…and that doesn’t even work on any released ksh version, though the manual says/implies it should!

Of course that would still not do anything for the second bug @stephane-chazelas reported at the top. As long as that cannot be fixed, then assigning to namespace variables from outside the namespace (as in .foo.bar=baz after namespace foo) should be blocked, too.

How about this: these things get blocked in the 1.0 branch only, as the next release will be from that. (And I want to have the next release out Real Soon Now™ – even Slackware got so desperate for a new ksh they’ve been packaging our development code in slackware-current!) We’ll leave it alone in the master branch so it can hopefully be fixed sometime.

Not sure ksh2020 souce code is going to be any help in respects to namespaces as its namespace code was diminished. It seems that the ksh93v- authors decided to not allow namespace commands inside function definitions on 2013-05-29–not to say what direction ksh93u+m will go but bringing this knowledge into the light.

However, I skimmed over ksh93v- beta release notes and they made lots of fixes and changes to namespaces. Here are just a few of the entries from https://github.com/att/ast/blob/2016-01-10-beta/src/cmd/ksh93/RELEASE.

13-08-19 A bug in which compound variables created in a namespace did not display correctly outside of the namespace has been fixed. 13-08-11 +namespace was modified so that namespace names can be a compound variable rather than just an identifier and namespace are no longer nested. 13-07-08 Fixed a namespace bug in which print -v did not work for printing a compound array of a namespace variable. 13-05-29 namespace commands are no longer allowed inside function definitions and now generate a syntax error. 12-04-02 Made some namespace changes and added a regression test.