Consider the following code:

x = 1;

nb = NotebookPut@Notebook[
      BoxData@ToBoxes@Defer@{$Context, x = 2},
      CellContext -> Notebook

NotebookEvaluate[nb, InsertResults -> True];

This code first creates Global`x, then it creates a notebook with a cell with CellContext->Notebook. Evaluating the notebook using NotebookEvaluate creates the symbol Notebook$$…`x, everything works as expected.

But now, let's replace the last line with the following:

Block[{$ContextPath = $ContextPath},
 NotebookEvaluate[nb, InsertResults -> True];

Rerunning the code (including the creation of the notebook), now issues a shadowing warning for x.

Why is this happening? This seems to be a rather strange effect of Block...

  • 1
    $\begingroup$ Block with $ContextPath causes all sorts of weird shadowing messages where they shouldn't be. I've reported a few cases of this as bugs but my guess is WRI doesn't care about such a minor/cosmetic one when they've got so many other bugs to quash. $\endgroup$
    – b3m2a1
    Feb 13, 2019 at 22:22
  • $\begingroup$ NotebookEvaluate does not cope well with CellContext->Notebook: 135025 $\endgroup$
    – Kuba
    Feb 13, 2019 at 22:28
  • $\begingroup$ Also the interpretation of docs "The cells of the notebook are evaluated in a dialog subsession." can imply that it is strange it didn't happen with the first one. Don't know where a 'dialog subsession' is defined though. $\endgroup$
    – Kuba
    Feb 13, 2019 at 22:30
  • $\begingroup$ @b3m2a1 Thanks, good to know I'm not going crazy. Are you aware of any other symbols that cause similar problems when used with block? $\endgroup$
    – Lukas Lang
    Feb 13, 2019 at 22:31
  • 1
    $\begingroup$ @LukasLang is CellContext->Notebook in context of NotebookEvaluate working like Begin or BeginPackage? My point was that there are many not really precisely defined actions so who knows what really should happen. But yes, it is clearly unexpected. $\endgroup$
    – Kuba
    Feb 13, 2019 at 22:53


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.