Do ConTeXt products have isolated layouts?

Reading the docs, I came under the impression that products have isolated environments.

Experimentation suggests I misunderstand or am misusing them. Given the following project structure:

entry.tex
|- p1.tex
|    \- p1-env.tex
|- p2.tex
\- p2-env.tex


Where entry.tex includes two products (separated by a \page), I would expect p1 to include the p1-env but not p2-env. However it appears that p2-env.tex, being the last to load, dominants any other styles.

A solution/workaround would seem to be just not use environments but rather have macros/commands that set the style accordingly, on demand.

However, if indeed all environments end up collapsing on the last-loaded environment, what's what's the point of the ConTeXt product-structure and environments?

Here's a sample starter that one can use to illustrate.

p1-env.tex

\startenvironment *
\setuplayout[
leftmargin=0pt,
backspace=9em,
cutspace=3em,
leftmargindistance=1em,
leftedgedistance=1em,
leftmargin=7.5em,
]

\stopenvironment


p1.tex

\environment p1-env

\startproject *

Hello from p1 \inmargin{Left}

\stopproject


p2-env.tex

\startenvironment *
\setuplayout[
leftmargin=0pt,
backspace=3em,
cutspace=9em,
rightmargindistance=1em,
rightedgedistance=1em,
rightmargin=7.5em,
]

\setupmargindata[inmargin]
[style=normal, location=right, stack=yes, distance=0pt]

\stopenvironment


p2.tex

\environment p2-env

\startproject *

Hello from p2 \inmargin{Right}

\stopproject


entry.tex

% !TEX TS-program = ConTeXt (LuaTeX)
\startproduct *
\product p1
\page  % Should reset layout.
\product p2
\stopproduct

• Shouldn’t entry.tex have \start...\stopproject and p1.tex/p2.tex the \start...\stopproduct pair? – Henri Menke Mar 7 '17 at 20:17
• @HenriMenke Oddly enough, projects don't actually compile. The inner products could be components but from looking at the source it looks like they were designed to be peers as well. I stand to be corrected though. lol – Brian M. Hunt Mar 7 '17 at 23:36

The first thing I notice is that you are using the ConTeXt project structure incorrectly.

But before we discuss that, let us take a look at environment scoping. The general rule is:

Environments do not have a scope. Their definitions become active after calling \environment and cannot be “deactivated” after.

This is a very fundamental statement and is a consequence of the fact that \environment simply does \input under the hood. Similarly, there is no mechanism in Plain TeX to “uninput” (or “deinput” if you like) a file.

Let me demonstrate this with a simplified version of your document structure:

p1-env.tex

\startenvironment *
\def\foo{foo}
\stopenvironment


p2-env.tex

\startenvironment *
\def\baz{baz}
\stopenvironment


p1.tex

\startcomponent *
\environment p1-env
{\tt\meaning\foo\par\meaning\baz}
\stopproject


p2.tex

\startcomponent *
\environment p2-env
{\tt\meaning\foo\par\meaning\baz}
\stopcomponent


entry.tex

\startproduct *
\component p1
\par
\component p2
\stopproduct


Executing ConTeXt on entry.tex yields the following result:

You see that \foo remains defined even though we switch to a different file with different environment inputs. The reason is exactly what I explained before.

You can work around this behaviour by explicitly introducing scoping with \begingroup...\endgroup (or \start...\stop in ConTeXt). Keep in mind that this is not a good idea because TeX might run out of memory. I don't know how likely that is and what the exact limits are but there definitely exist issues.

\startproduct *
\start
\component p1
\stop
\par
\start
\component p2
\stop
\stopproduct


Next you noticed in a comment:

Oddly enough, projects don't actually compile.

Yes, and the explanation is that projects do not define a valid \starttext ... \stoptext structure. You can also see this from the log:

system          > invalid \starttext ... \stoptext structure


So the question is, what are projects actually? The answer is that projects are collections of environment files. Imagine you had a long list of environments for the page layout, the colour theme, the bibliography macros, and mathematical macros for your document. You wouldn't want to repeat the whole list in every component, which is why you collect this list in a project. For example

proj-mag.tex

\startproject *
\environemnt env-design
\environemnt env-layout
\environemnt env-biblio
\environemnt env-mathem
\stopproject


p-issue1.tex

\project proj-mag
\startproduct *
\component c-editorial
\stopproduct


The file that you run ConTeXt on is p-issue1.tex.