Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 592474 - Document that ebuild global scope != bash global scope (was: `declare -r` in global context makes variable disappear in phase functions)
Summary: Document that ebuild global scope != bash global scope (was: `declare -r` in ...
Status: UNCONFIRMED
Alias: None
Product: Documentation
Classification: Unclassified
Component: Devmanual (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo Devmanual Team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-30 21:13 UTC by .
Modified: 2016-09-02 13:12 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description . 2016-08-30 21:13:17 UTC
In my overlay I have an eclass, which makes certain self-declared global variables readonly.

This works:
```
VAR="value"
readonly VAR
```

but this:
```
declare -r VAR="value"
```
does not, because in phase functions the variable is not defined.

From BASH's point of view those two declarations should be the same, so there must be something in Portage internals that makes the variable disappear.
Comment 1 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2016-08-30 21:27:01 UTC
(In reply to Jan Chren (rindeal) from comment #0)
> From BASH's point of view those two declarations should be the same, so
> there must be something in Portage internals that makes the variable
> disappear.

Did you actually confirm the bash behavior, or are just guessing? Because if you either tested or read the documentation, you would know that (in some versions of bash, since bash upstream doesn't care to keep the behavior stable) 'declare' defaults to setting variables local rather than global.

FOO=11 -- global variable
declare FOO=11 -- local variable

You are looking for 'declare -g'. I think it's available since bash-4.
Comment 2 . 2016-09-02 12:57:01 UTC
I know that(In reply to Michał Górny from comment #1)
> (In reply to Jan Chren (rindeal) from comment #0)
> > From BASH's point of view those two declarations should be the same, so
> > there must be something in Portage internals that makes the variable
> > disappear.
> 
> Did you actually confirm the bash behavior, or are just guessing?

Confirmed.

> Because if
> you either tested or read the documentation, you would know that (in some
> versions of bash, since bash upstream doesn't care to keep the behavior
> stable) 'declare' defaults to setting variables local rather than global.

Yes, I know that, but as BASH's global space doesn't have local variables 'declare var' still makes them global, unless global space in an ebuild is not as global as in a pure BASH script, because it's wrapped in some Portage internals.

I think that this fact should be mentioned in devmanual with big red letters.
Comment 3 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2016-09-02 13:12:20 UTC
CC-ing PMS people since PMS also uses the term 'global scope' a lot. We may consider avoiding it.